Skip to content

Instantly share code, notes, and snippets.

@DazWorrall
Created November 26, 2012 13:59
Show Gist options
  • Save DazWorrall/4148338 to your computer and use it in GitHub Desktop.
Save DazWorrall/4148338 to your computer and use it in GitHub Desktop.
Find a random image on flickr
#!/usr/bin/env python
'''
$ pip install flickrapi
....
$ FLICKR_API_KEY=mykey ./flick.py tag1[,tag2,tag3...]
'''
# http://www.flickr.com/services/api/flickr.photos.search.html
import flickrapi
import os
import sys
import random
api_key = os.environ['FLICKR_API_KEY']
url_template = 'http://farm%(farm_id)s.staticflickr.com/%(server_id)s/%(photo_id)s_%(secret)s.jpg'
def url_for_photo(p):
return url_template % {
'server_id': p.get('server'),
'farm_id': p.get('farm'),
'photo_id': p.get('id'),
'secret': p.get('secret'),
}
flickr = flickrapi.FlickrAPI(api_key)
print url_for_photo(random.choice(flickr.photos_search(tags=sys.argv[1], per_page=20)[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment