Skip to content

Instantly share code, notes, and snippets.

@jeremylow
Created September 26, 2015 21:25
Show Gist options
  • Save jeremylow/5acb4df1be930f7c0d75 to your computer and use it in GitHub Desktop.
Save jeremylow/5acb4df1be930f7c0d75 to your computer and use it in GitHub Desktop.
import os
from random import randint
import flickrapi
api_key = os.environ['FLICKR_API_KEY']
api_secret = os.environ['FLICKR_API_SECRET']
''' Caching is not going to speed anything up within the utility functions, but
it might help if more than one user has the same tag and it is for public
photos. Probably a wash, but whatevs.
'''
flickr = flickrapi.FlickrAPI(
api_key,
api_secret,
format='parsed-json',
cache=True)
class NoSuitableSizeError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def get_photo_from_tag(tag=None):
if tag.aboutdotme:
return get_rando_user_photo_from_tag(tag=tag)
else:
return get_rando_public_photo_from_tag(tag=tag)
def get_rando_public_photo_from_tag(tag=None):
''' Return a random **public** photo for a given tag object.
'''
photos = flickr.photos.search(
tags=tag.tag_name,
safe_search=1,
per_page=50,
media='photos')
total_photos_available = int(photos['photos']['total'])
random_photo = randint(0, total_photos_available)
page_num_of_photo = int(random_photo / 50)
photos = flickr.photos.search(
tags=tag.tag_name,
safe_search=1,
per_page=50,
page=page_num_of_photo,
media='photos')
num_photos_returned = len(photos['photos']['photo'])
return photos['photos']['photo'][randint(0, num_photos_returned - 1)]
def get_rando_user_photo_from_tag(tag=None):
''' Return a random photo from within a user's set of flickr images
for a given tag object.
'''
photos = flickr.photos.search(
user_id=tag.user.flickr_username,
tags=tag.tag_name,
safe_search=1,
per_page=50,
media='photos')
total_photos_available = int(photos['photos']['total'])
random_photo = randint(0, total_photos_available)
page_num_of_photo = int(random_photo / 50)
photos = flickr.photos.search(
user_id=tag.user.flickr_username,
tags=tag.tag_name,
safe_search=1,
per_page=50,
page=page_num_of_photo,
media='photos')
num_photos_returned = len(photos['photos']['photo'])
return photos['photos']['photo'][randint(0, num_photos_returned - 1)]
def get_photo_links(photo_id=None):
''' For a given photo id, return the medium size url of the photo
(src link) and the link to the flickr page for the photo. If the
large size doesn't exist, then go down through the medium sizes'''
photo_links = flickr.photos.getSizes(photo_id=photo_id)
for photo_link in photo_links['sizes']['size']:
if 'Medium 800' in photo_link['label']:
return photo_link['source'], photo_link['url']
elif 'Medium 600' in photo_link['label']:
return photo_link['source'], photo_link['url']
elif 'Medium' in photo_link['label']:
return photo_link['source'], photo_link['url']
else:
raise NoSuitableSizeError(photo_id)
def get_info_to_email(tag):
''' Inputs should be a tag object (FlickrTag) and an int
of a user's id. Outputs a random photo from the tag and,
if the user_id is spec'd and the tag is aboutdotme, then
the random photo should only be from the user's collection
on Flickr.'''
photo = get_photo_from_tag(tag)
src_link, flickr_url = get_photo_links(photo['id'])
return photo['title'], src_link, flickr_url
def get_nsid_from_username(username):
try:
return flickr.people.findByUsername(username=username)['user']['id']
except KeyError:
return 'Unable to locate that username'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment