Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Get Started With the Flickr API

/skill level/
/viewed/
0 Times

Current revision (15:22, 19 May 2008) (edit) (undo)
 
Line 91: Line 91:
The Flickr API exposes nearly every aspect of the site, which makes it both limitless and daunting. Thankfully Flickr has excellent documentation. As for what you can do with the Flickr API, the best mashups seem to start with the thought, "you know what would be cool..."
The Flickr API exposes nearly every aspect of the site, which makes it both limitless and daunting. Thankfully Flickr has excellent documentation. As for what you can do with the Flickr API, the best mashups seem to start with the thought, "you know what would be cool..."
-
== Introduction ==
 
- 
-
== What you'll need ==
 
- 
- 
-
== Steps ==
 
- 
- 
-
== Alternate methods ==
 
- 
- 
-
== Suggested readings ==
 

Current revision

One the first and most comprehensive application interfaces of the Web 2.0 era, the Flickr API was in no small part responsible for the site's success.

There were dozens of photo sharing sites clamoring for attention when Flickr first launched, but thanks its flexible API, developers began building and extending the site far beyond the capabilities of others.

The Flickr API exposes almost every piece of data stored on the site, and it offers near limitless possibilities for mashups, data scraping, tracking friends -- just about anything you can think of.

For examples of some of the more popular applications using Flickr's API, check out the various desktop uploaders available on all platforms. Also have a look at some of the tag-based Flickr mashups people have created, like Earthalbum and Flickrmania. Perhaps the most prolific of Flickr API users is John Watson (Flickr user fd) who has an extensive collection of tools and mashups available.


Getting Started

The nice thing about Flickr is that the API is very mature. There are already client libraries available for most languages -- several libraries in some cases. That means you don't need to sit down and work out the details of every method in the API, you just need to grab the library for your favorite programming language.

For the sake of example, I'm going to use a Python library to retrieve all the photos I've marked as favorites on Flickr.

First grab Beej's Python Flickr API library and install it on your Python path (instructions on how to do that can be found in the documentation). I like Beej's library because it handles the XML parsing without being dependent on any other libraries.

Now let's write some code.


Accessing Flickr with Python

Fire up a terminal and start Python. Now import the flickrapi and set your API key:

import flickrapi
api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

Now we're going to create an instance of the flickrapi client:

flickr = flickrapi.FlickrAPI(api_key)

OK, now let's grab all the photos we've marked as favorites. Accessing all the methods of the Flickr API takes the general form:

flickr.method_name(params)

So to grab our favorites we'll use:

favs = flickr.favorites_getPublicList(user_id='85322932@N00')

The favs variable now holds our list of images as parsed XML data. To print it, we just loop through and pull out what we want:

for photo in favs.photos[0].photo:
    print photo['title']

To access the actual images, for instance to generate some HTML, we just need to build a URL:

for photo in favs.photos[0].photo:
    print '<img src="'+"http://farm%s.static.flickr.com/%s/%s_%s_m.jpg" % (photo['farm'], photo['server'], photo['id'], photo['secret']) +'" />'


Mashups

If all you want to do is put images on your website, there's probably already a widget that can handle the task. And of course you can DIY if you like by extending the method above.

But what if you want to plot all the Favorites we just retrieved on a Google Map? That's exactly the sort of mashup task where the Flickr API excels.

To make our retrieved photos mappable, we would just need to add a parameter to our original method call. We'd need to tell Flickr to include the photos' geo coordinates, for instance:

favs = flickr.favorites_getPublicList(user_id='85322932@N00', extras='geo')

Now we can parse through our set and grab the coordinates:

for photo in favs.photos[0].photo:
    print photo['latitude'] + photo['longitude']

From there, we can pass that over to the Google Maps API and plot the images.

In this particular case, only a couple of the returned photos actually have lat/long info. Flickr's tagging system lets users add geo coordinates to photos, but most casual users don't go to the trouble to add the location info to their images. Because of this, it's a good idea to test for non-zero values before passing the data to the Google Maps API.

Tip: The Flickr API will return data formats other than XML. For instance we could use this method to get a JSON response:

favs = flickr.favorites_getPublicList(user_id='85322932@N00', extras='geo', format='JSON')

The Flickr API exposes nearly every aspect of the site, which makes it both limitless and daunting. Thankfully Flickr has excellent documentation. As for what you can do with the Flickr API, the best mashups seem to start with the thought, "you know what would be cool..."

  • This page was last modified 15:22, 19 May 2008.
Edit this article
Reddit Digg
 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year