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:

  1. http://www.stuvel.eu/projects/flickrapi
  2. http://code.google.com/p/flickrpy/

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 http://code.google.com/p/ilkinulassandbox/. 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')
>>> api.search({'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 = api.search({'tags':'python', 'per_page':'3'})
>>> result['photos']['photo'][0]['id']
'3911751961'


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])
'http://farm4.static.flickr.com/3462/3911751961_43d409cf7c_m.jpg'
>>> api.build_photo_url(result['photos']['photo'][1], size='s')
'http://farm3.static.flickr.com/2613/3911666353_ac0cc98871_s.jpg'


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

>>> pictures = api.search({'lat':'41.034046', 'lon':'28.980217', 'radius':'10', 'radius_units':'km','tags':'fun'})
>>> api.build_photo_url(fun_pictures['photos']['photo'][20])
'http://farm1.static.flickr.com/116/293251976_ce5337908d_m.jpg'



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 http://code.google.com/p/ilkinulassandbox/. 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)

5 comments:

James praker said...

Hi there!
i have seen your blog and found its very useful and i like your post you really did a great job!
i am a Web Designing
specialist and you really did a great job!

İlkin Ulaş BALKANAY said...

Thanks James, i am glad you liked the post.

Web Solutions said...

That will a good show for us to use such a great tool while using these social networking websites!

Anonymous said...

motherboard http://cciworldwide.org/members/Omeprazole.aspx http://cciworldwide.org/members/Vacuum-Cleaners.aspx http://cciworldwide.org/members/Annuity-Calculator.aspx http://cciworldwide.org/members/Bariatric-Surgery.aspx http://cciworldwide.org/members/Electric-Blankets.aspx

Unknown said...

I am amazed to see such an amazing post. Keep up the good work.parking at gatwick