1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
One the first and most comprehensive APIs of web 2.0, 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 the API developers begin building tool and extending the site far beyond the capabilities of others.
The Flickr API exposes just about every piece of data that the site store and offer near limitless possibilities for mashups, data scraping, tracking friends and just about anything else you can think of.
Some of the more popular applications leveraging the API are the various desktop uploaders available on all platforms, endless mapping mashups and more. Perhaps the most prolific of Flickr API users is John Watson (Flickr user fd) who has an [http://bighugelabs.com/flickr/ extensive collection] of tools and mashups available.
== Getting Started ==
The nice thing about Flickr is that the API is mature and there are already client libraries available for most languages (several libraries in some cases). That mean 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 [http://flickrapi.sourceforge.net/ Beej's Python Flickr API library] and install it on your Python path (instructions on how to do that can be found in the [http://flickrapi.sourceforge.net/flickrapi.html documentation]). I like Beej's library because it handles the XML parsing without being dependent on any other libraries.
== Writing the Code ==
Now let's write some code. 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 flickrapo client:
flickr = flickrapi.FlickrAPI(api_key)
Okay, 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')
So 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 (of course you *can* DIY if you like). But what if you wanted to plot all the Favorites we just retrieved on a Google Map?
That's exactly the sort of mashup that the Flickr API excels at. To do that, we would just need to add a parameter to our original method call, 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 and grab the coordinates:
for photo in favs.photos[0].photo:
print photo['latitude'] + photo['longitude']
Then we can pass that over to the Google Maps API and plot the images. Note that in this particular case only a couple of the returned photos actually have lat/long info so it would be a good idea to test for non-zero values before passing the data to the Google Maps API.
I should also point out that the Flickr API will return other formats besides XML. For instance we coud use this method to get a JSON response:
favs = flickr.favorites_getPublicList(user_id='85322932@N00', extras='geo', format='JSON')
== Conclusion ==
The Flickr Maps API exposes nearly every aspect of the site, which makes it both limitless and daunting, but 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..."
|