def __init__(self, read_command_line=True): self._flickr = Flickr(read_command_line)
class API(object): _flickr = None def __init__(self, read_command_line=True): self._flickr = Flickr(read_command_line) def request(self, *attr, **kargs): return self._flickr.request(*attr, **kargs) # simple methods def getPhotoInfo(self, photo_id): rsp_json = self.request('photos.getInfo', "photo info for %s", [photo_id], photo_id=photo_id) return rsp_json['photo'] if rsp_json else None def getPhotosetInfos(self, photoset_id): rsp_json = self.request('photosets.getInfo', "photoset %s informations", [photoset_id], photoset_id=photoset_id) return rsp_json['photoset'] if rsp_json else None def getCollectionInfo(self, collection_id): rsp_json = self.request('collections.getInfo', "informations for collection %s", [collection_id], collection_id=collection_id) return rsp_json['collection'] if rsp_json else None def getUserFromID(self, user_id, sign=False): rsp_json = self.request('people.getInfo', "user informations for %s", [user_id], user_id=user_id, sign=sign) return rsp_json['person'] if rsp_json else None def getUserFromUsername(self, user_name): rsp_json = self.request('people.findByUsername', "user %s from username", [user_name], username=user_name, sign=False) return rsp_json['user'] if rsp_json else None def searchGroupByUrl(self, group_url): rsp_json = self.request('urls.lookupGroup', " info for group %s from url", [group_url], url=group_url, content_type=7) return rsp_json['group'] if rsp_json else [] def searchGroup(self, group_name): return self.searchGroupByUrl( 'http://www.flickr.com/groups/%s' % group_name) def getPhotoExif(self, photo_id): rsp_json = self.request('photos.getExif', "photo EXIF for %s", [photo_id], photo_id=photo_id) return rsp_json['photo']['exif'] if rsp_json else None def getPhotoSize(self, photo_id): rsp_json = self.request('photos.getSizes', "photo size for %s", [photo_id], photo_id=photo_id) return rsp_json['sizes']['size'] if rsp_json else None def getUserGroups(self, user_id, page=1): rsp_json = self.request('people.getPublicGroups', "user %s groups, page %i", [user_id, page], page=page, user_id=user_id, content_type=7, invitation_only=1) return rsp_json['groups']['group'] if rsp_json else [] def countGroupPhotos(self, group_id): rsp_json = self.request('groups.pools.getPhotos', "photos from group %s", [group_id], per_page=1, group_id=group_id) return rsp_json['photos']['total'] if rsp_json else 0 def getUserPhotosets(self, user_id): rsp_json = self.request('photosets.getList', "photosets for user %s", [user_id], user_id=user_id) return rsp_json['photosets']['photoset'] if rsp_json else None def getContactsLatestPhotos(self, page=1): rsp_json = self.request('photos.getContactsPhotos', 'the contacts photos', page=page, count=50) return rsp_json['photos']['photo'] if rsp_json else [] def getCollectionPhotosets(self, collection_id, user_id): rsp_json = self.request('collections.getTree', "photosets for user (%s) collection %s", [user_id, collection_id], collection_id=collection_id, user_id=user_id, content_type=7) if rsp_json: return rsp_json['collections']['collection'][0]['set'] # loop methods def _loop(self, func, path, *attr, **kargs): ret = [] path.reverse() page = kargs.pop('page', 1) # FIXME page are limited to 500 while page < 500: kargs['page'] = page rsp_json = func(*attr, **kargs) if not rsp_json: break l_path = list(path) while l_path: rsp_json = rsp_json.get(l_path.pop(), {}) if not rsp_json: break ret.extend(rsp_json) page += 1 return ret def _getPhotosetPhotos(self, photoset_id, page): rsp_json = self.request('photosets.getPhotos', "photos from %s photoset, page %i", [photoset_id, page], page=page, per_page=DEFAULT_PERPAGE, photoset_id=photoset_id, content_type=7) return rsp_json or [] def getPhotosetPhotos(self, photoset_id, page=1): return self._loop(self._getPhotosetPhotos, ['photoset', 'photo'], photoset_id, page=page) def _getUserLastPhotos(self, user_id, since, page): rsp_json = self.request('photos.recentlyUpdated', "last %s's photos page %i", [user_id, page], page=page, per_page=DEFAULT_PERPAGE, user_id=user_id, content_type=7, min_date=since) return rsp_json or [] def getUserLastPhotos(self, user_id, since, page=1): return self._loop(self._getUserLastPhotos, ['photos', 'photo'], user_id, since, page=page) def _getPhotosByTag(self, user_id, tags, page): rsp_json = self.request('photos.search', "searched photos", user_id=user_id, tags=tags, content_type=7, page=page) return rsp_json or [] def getPhotosByTag(self, user_id, tags, page=1): return self._loop(self._getPhotosByTag, ['photos', 'photo'], user_id, tags, page=page) def _searchPhotos(self, user_id, search, page): rsp_json = self.request('photos.search', "searched photos", user_id=user_id, text=search, content_type=7, page=page) return rsp_json or [] def searchPhotos(self, user_id, search, page=1): return self._loop(self._searchPhotos, ['photos', 'photo'], user_id, search, page=page) def _getContactList(self, page): rsp_json = self.request('contacts.getList', 'the contact list', page=page, sort='time') return rsp_json or [] def getContactList(self, page=1): return self._loop(self._getContactList, ['contacts', 'contact'], page=page) def _getUserFavorites(self, user_id, min_fave_date, page): rsp_json = self.request('favorites.getList', '%s favorites', [user_id], user_id=user_id, page=page, content_type=7, min_fave_date=min_fave_date) return rsp_json or [] def getUserFavorites(self, user_id, min_fave_date=None, page=1): return self._loop(self._getUserFavorites, ['photos', 'photo'], user_id, min_fave_date, page=page) # complicated stuff def getGroupPhotosFromScratch(self, group_id, batch=0, page_in_batch=100, per_page=500): Logger().info("getGroupPhotosFromScratch %s %s" % (group_id, batch)) content = [] for spage in range(1, page_in_batch + 1): page = spage + batch * page_in_batch rsp_json = self.request('groups.pools.getPhotos', "photos from group %s, page %i", [group_id, page], page=page, per_page=per_page, group_id=group_id, content_type=7) if not rsp_json: break content.extend(rsp_json['photos']['photo']) if page * per_page > int(rsp_json['photos']['total']): break return content def groupFromScratch(self, group_id): rsp_json = self.request('groups.pools.getPhotos', "photos from group %s", [group_id], per_page=1, group_id=group_id) total = int(rsp_json['photos']['total']) Logger().info("groupFromScratch %s %s" % (group_id, total)) def _get_path(batch): return os.path.join(OPT.groups_full_content_dir, "%s_%s" % (group_id, batch)) page_in_batch = 100 per_page = 500 maxbatch = 1 + total / (page_in_batch * per_page) for batch in range(0, maxbatch): content = self.getGroupPhotosFromScratch(group_id, batch, page_in_batch, per_page) file_dump(_get_path(batch), content) def getUserFromUrl(self, url): rsp_json = self.request('urls.lookupUser', "lookup for user url %s", [url], url=url, sign=False) return rsp_json['user'] if rsp_json else {} def getUserFromNick(self, nick): try: return self.getUserFromUrl(getUserURL(nick)) except Exception, e: Logger().error("while looking up for user %s (%s)" % ( nick, e.message))