The favorites mosaic shown in my
previous post was a good excuse for me to try out
Flickr's API, and it turned out to be a lot easier than I thought. So if, like me, you feel like playing with 2.5 billion photos (and 5,000 new uploads every minute), here's a quick guide.
All you need is an API key (get one
here) and a set of bindings for your favorite language. I'm a Python person so I had two choices:
Beej's Flickr API and
flickr.py; the latter seemed to have everything I needed in a single file, so I went with it. It provides classes directly mapped to most of Flickr's concepts: photos, tags, sets, groups, users, etc.
To get started, let's see what Flickr knows about me (if you don't know your Flickr user id, get it from the RSS link in your photo page):
>>> me = flickr.User('99746300@N00')
>>> me.username, me.ispro, me.location, me.photos_count
(u'orebokech', u'1', u'Lyon, France', u'254')
>>>
Sounds right. Now, to create my favorites mosaic I need to get the list of my favorites, actually I only need 36 of them. Easy:
>>> faves = me.getPublicFavorites(per_page=36)
>>> len(faves)
36
>>>
You can iterate over the list and get
flickr.Photo objects which have all the information you need, like the available sizes of the image:
>>> photo = faves[0] # let's just get one for this example
>>> for size in photo.getSizes():
... print "%s: %dx%d" % (size['label'], size['width'], size['height'])
Square: 75x75
Thumbnail: 100x67
Small: 240x160
Medium: 500x333
Original: 1024x683
>>>
And for each available size you can get the corresponding photo page, or the URL to the image itself:
>>> photo.getURL() # photo page, default Medium
u'http://www.flickr.com/photos/o0off/2363681897/sizes/m/'
>>> photo.getURL(size='Square', urlType='source') # img
u'http://farm3.static.flickr.com/2107/2363681897_f6528a14a4_s.jpg'
>>>
Add a little bit of HTML formatting around this, and you get a neat mosaic. The concept can trivially be extended to sets, tag searches, etc. It's all very straightforward.
Phew, this post is getting pretty long, so I think I'll leave you to explore the rest for yourself. Have fun!