Saturday, September 12, 2009

Python API for Flickr Services - Part I

These days I am working on a facebook application that mashes up servises from Facebook and Flickr. The application is hosted by Google Appengine and I am developing on Django web application framework.

Flickr has an easy to use API which is called Flickr Services. Here are some interesting things you can do with the Flickr Services:

  • You can upload photos from anywhere (web application, desktop application or mobile application)
  • You can search for images. Flickr Services lets you search images by
    - tags
    - geo location
    - photo taken time / photo upload time
    - and more...
  • execute methods that require authentication
    - create, edit, remove tags
    - create, edit, remove photo sets
    - replace, remove photos

There are two Python APIs listed in the Flickr Services page:


I have implemented Flickr services in Python. Don't think this as "reinventing the wheel" because it was a coding practice for me. Plus, i have my own, customized, shortcut methods ;)

You can download the library from There are also some other javascript and python code snippets there.

Here is a basic usage of the API:

>>> import flickr
>>> api = flickr.FlickrAPI('API_KEY','SECRET_KEY')
>>>{'tags':'python', 'per_page':'3'})
{'photos': {'page': 1,
'pages': 8326,
'perpage': 3,
'photo': [{'farm': 4,
'id': '3911963732',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'owner': '30041312@N03',
'secret': '44aae23e98',
'server': '3090',
'title': 'Centralian Carpet Python (Apophis)'},
{'farm': 3,
'id': '3911184515',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'owner': '30041312@N03',
'secret': '0aae3c8127',
'server': '2513',
'title': 'Centralian Carpet Python (Apophis)'},
{'farm': 3,
'id': '3911127131',
'isfamily': 0,
'isfriend': 0,
'ispublic': 1,
'owner': '31355375@N02',
'secret': '2921fea364',
'server': '2564',
'title': 'Sendra Python Nails 122 01'}],
'total': '24977'},
'stat': 'ok'}

Methods of the API returns a dictionary instance. Caller must handle the returned result.

>>> result ={'tags':'python', 'per_page':'3'})
>>> result['photos']['photo'][0]['id']

Flickr photo url construction is described here in details. You can easily get the url of a returned photo search result by using build_photo_url. Default image size is 'm' which means 'normal'

>>> api.build_photo_url(result['photos']['photo'][0])
>>> api.build_photo_url(result['photos']['photo'][1], size='s')

Lets search for pictures tagged 'fun' around point (lat=41.070293, lon=28.24894)

>>> pictures ={'lat':'41.034046', 'lon':'28.980217', 'radius':'10', 'radius_units':'km','tags':'fun'})
>>> api.build_photo_url(fun_pictures['photos']['photo'][20])

The next post of "Python API for Flickr Services" will show how you can call methods that require authentication. For the curious readers the api is ready But it lacks documentation :(

NOTE : I use this python API in my facebook application "Türkiye'yi ne kadar iyi tanıyorsun?". Application is a simple guessing game. You see a geo tagged picture from flickr and you answer which city this picture belongs.

UPDATE : This library returns native Python datatypes rather than XML trees or unparsed JSON, which the other Flickr APIs have a tendency to do. (Thanks martian)