Flickr REST API Basics

It's been a while since I've played with the Flickr API and I was pleasantly surprised by how much easier it's gotten to use. I don't believe the REST interface was available last time I tried (although it's possible I was just dumb and missed it) so I ended up using somebody's PHP XML-RPC class which was a bit clunky.

The REST API is dead-simple to use. The basic url format is

~~~~ {.html name="code"} http://api.flickr.com/services/rest/?method=&name=value... wzxhzdk:0

The above call returns the 100 most recently uploaded photos matching the tag monkey. The flickr.photos.search method has lots of other options that let you customize the search.

You get back a big chunk of XML (there's a parameter to return JSON and several other formats instead) that looks like this:

~~~~ {.xml name="code"} wzxhzdk:1

It takes an API method name a dictionary of parameters, then uses urllib2 to get the XML response. The XML response is parsed using minidom and the DOM object is returned.

Since this is just for fun, the code basically just glosses over the Unicode aspect of the response by ignoring any Unicode errors. This lets us parse the XML at the expense of possible mangling some characters (generally only in the image title).

This is how the above function would be used to do a simple search:

~~~~ {.python name="code"} tags = ["monkey", "chimp"]searchParams = { 'api_key': FLICKR_API_KEY, 'tags': ','.join(tags), 'tag_mode': 'any', 'content_type': 1, 'page': 1 'sort': 'date-posted-desc', }dom = flickrRequest('flickr.photos.search', searchParams) wzxhzdk:2

And that's the basics of using the Flickr REST api. My ultimate goal with learning the API is to write some sort of script that will let me backup all of the meta-information (tags, sets, collections, descriptions) of my Flickr account.