本文整理汇总了Python中flickrapi.FlickrAPI.photos_getFavorites方法的典型用法代码示例。如果您正苦于以下问题:Python FlickrAPI.photos_getFavorites方法的具体用法?Python FlickrAPI.photos_getFavorites怎么用?Python FlickrAPI.photos_getFavorites使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flickrapi.FlickrAPI
的用法示例。
在下文中一共展示了FlickrAPI.photos_getFavorites方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: importFromFlickr
# 需要导入模块: from flickrapi import FlickrAPI [as 别名]
# 或者: from flickrapi.FlickrAPI import photos_getFavorites [as 别名]
def importFromFlickr():
if g.user is None:
return jsonify(result = False, error = "You need to be logged in to import from Flickr")
if not g.user.flickr_auth:
return jsonify(result = False, error = "Your account has not been authenticated with Flickr")
try: # Yes yes, a massive try block, the horror! But almost every single line in here throws an error from FlickrAPI
photoID = request.form.get('photoID')
api_key = os.environ['PARAM1']
api_secret = os.environ['PARAM2']
flickr = FlickrAPI(api_key, api_secret, store_token = False)
# Get original photo's URL
sizes = flickr.photos_getSizes(photo_id = photoID).find('sizes')[-1]
photo_url = sizes.attrib['source']
img_width = int(sizes.attrib['width']) # necessary to correctly scale notes
img_height = int(sizes.attrib['height'])
# Pull a blob of most of the photo's metadata
photo_info = flickr.photos_getInfo(photo_id = photoID).find('photo')
# Check if the person importing this photo actually owns it
flickr_screen_name = photo_info.find('owner').attrib['username']
if flickr_screen_name.lower() != g.user.name.lower():
return jsonify(result = False, error = 'You dog! You don\'t own this photo! %s does. For shame.' % flickr_screen_name)
# Pull photo's title, desc, timestamps from metadata blob
flickr_owner_id = photo_info.find('owner').attrib['nsid'] # used to retrieve views
title = photo_info.find('title').text
desc = photo_info.find('description').text
time_taken = photo_info.find('dates').attrib['taken'] # '2013-06-22 11:16:32' ... wtf?
time_posted = photo_info.find('dates').attrib['posted'] # '1372279163'
# flickr notes are in a 0..500px coordinate space, where 500 maps to max(img_width, img_height)
# brickr notes are normalized to a 0..100 % coordinate space, regardless of image aspect ratio (because I'm smarter)
# flickr notes don't have timestamp info
scale_w = 500 if img_width >= img_height else (500 / img_height * img_width)
scale_h = 500 if img_width < img_height else (500 / img_width * img_height)
notes = []
for note in photo_info.find('notes'):
notes.append({
'user_id': note.attrib['author'],
'screen_name': note.attrib['authorname'],
'text': note.text,
'x': int(note.attrib['x']) / scale_w * 100,
'y': int(note.attrib['y']) / scale_h * 100,
'w': int(note.attrib['w']) / scale_w * 100,
'h': int(note.attrib['h']) / scale_h * 100
})
# Photo tags are easy
tags = []
for tag in photo_info.find('tags'):
if tag.attrib['machine_tag'] != '1': # Ignore ugly automatically created inivisible-to-users tags
tags.append(tag.attrib['raw'])
# Import comments - needs its own Flickr API call
comments = []
if int(photo_info.find('comments').text) > 0:
comment_rsp = flickr.photos_comments_getList(photo_id = photoID).find('comments')
for comment in comment_rsp:
comments.append({
'user_id': comment.attrib.get('author'),
'screen_name': comment.attrib.get('authorname'),
'timestamp': comment.attrib.get('datecreate'),
'iconfarm': comment.attrib.get('iconfarm'),
'iconserver': comment.attrib.get('iconserver'),
'text': comment.text
})
# Import Favorites. These come in at most 50 per request. Another dedicated Flickr API call
favorites = []
favorite_rsp = flickr.photos_getFavorites(photo_id = photoID, per_page = '50').find('photo')
for fav in favorite_rsp:
favorites.append({
'user_id': fav.attrib.get('nsid'),
'screen_name': fav.attrib.get('username'),
'timestamp': fav.attrib.get('favedate'),
'iconfarm': comment.attrib.get('iconfarm'),
'iconserver': comment.attrib.get('iconserver')
})
fav_page_count = int(favorite_rsp.attrib['pages'])
if fav_page_count > 1:
for i in range(2, fav_page_count + 1):
favorite_rsp = flickr.photos_getFavorites(photo_id = photoID, page = str(i), per_page = '50').find('photo')
for fav in favorite_rsp:
favorites.append({
'user_id': fav.attrib['nsid'],
'screen_name': fav.attrib.get('username'),
'timestamp': fav.attrib.get('favedate'),
'iconfarm': comment.attrib.get('iconfarm'),
'iconserver': comment.attrib.get('iconserver')
})
# View count
# There's no direct flickr API to get a photo's view count (weird)
#.........这里部分代码省略.........