I'll spare you the boring "Hello World" post (which I typed at least twice) and jump straight into things. Chances are, if you've ever tried calling into the Flickr API from python, you've run across FlickrClient and flickr.py. I tried both, and both worked as advertised. The former, while very cleverly implemented, felt un-pythonic from the underscores in all the calls. Also, the use of xmltramp seemed like overkill. The latter was basically a lot of [IMHO] unnecessary wrapper code.
Enter FlickrAPI
Building on ideas from FlickrClient, I put together a small python module for calling into Flickr that just feels right. For example, to get a list of public picture URLs for a user specified in USER_ID:
from flickr import FlickrAPI, get_photo_url
flickr = FlickrAPI( API_KEY )
photos = flickr.people.getPublicPhotos( user_id=USER_ID )
urls = [get_photo_url(p) for p in photos]
Notice that you can call the API using dot notation. No underscores. The module also has a couple classes for dealing with the XML results from Flickr API calls in an intuitive manner, much like xmltramp. Both tag attributes and text node children are accessible through dot notation. Child nodes are also accessible through bracket (list) notation. Consider the following (abbreviated) response to flickr.people.getInfo
:
<person nsid="12037949754@N01" isadmin="0" ispro="0" iconserver="3">
<username>bees</username>
<realname>Cal Henderson</realname>
<location>Vancouver, Canada</location>
<photos>
<firstdate>1071510391</firstdate>
<count>449</count>
</photos>
</person>
This would be encapsulated in the returned XMLElement
(we'll call it person
) and could be accessed as follows:
person.nsid # would return 12037949754@N01
person.username # would return bees
person.photos.count # would return 449
Best of all, the module is entirely self-contained. One small file to install. Give it a try, and feel free to leave feedback in the comments.
For others with the same setup as me, if you get:
AttributeError: 'module' object has no attribute 'DefaultHandler'
when trying the above example you need to install PyXML from http://pyxml.sourceforge.net.
Thanks for sharing your work Dan.
Thanks, Peter. I've added a note at the top of the file about PyXML.
Dan, I've been using your flickr.py (thankyou!) to import my photos into a Django database. I have small problem however with parsing photo descriptions that are empty.
I get a set of photos using
flickr.photos.search
and then to get the description for each photos I'm callingflickr.photos.getInfo
.All works fine except when there is no description for a photo. In the xml returned by Flickr an empty description comes back as
<description/>
. Your flickr.py script seems to parse this incorrectly so that calling something likephoto_info.description
returns an xml node instead of an expected empty string.Is there any way to deal with these empty nodes that you can suggest?
I managed to work it out. I'm using
str(photo_info.description)
to get the text element of the<description>
node and it works fine now :)Hi, I'd love to use your Flickr API Kit (I'm also following your directions on how to use it with Django), but the file does not seem to be available. Where can I download it? Thanks!
On the off chance anyone sees this again, I've attached the file to this post.
Leave a comment