Welcome to ChaosZone!
Prasenjeet Dutta's Home Page.

About this Post
Archives

Using the Flickr API with Python’s FlickrAPI

Python FlickrAPI is an easy-to-use library for accessing Flickr from Python apps. Here’s a simple app prints the thumbnail URL for a photo, give a list of photo URLs.

from __future__ import with_statement
import re, flickrapi, xml.etree.ElementTree

photo_ids = []
filename = 'photo-urls.txt'
my_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
api_secret = 'xxxxxxxxxxxxxxxx'
photoUrlRegex = re.compile('.+http\:\/\/www\.flickr\.com\/photos\/your_user_name\/([0-9]+)')

with open(filename) as f:
    for line in f:
        m = photoUrlRegex.match(line)
        if m: photo_ids.append(m.group(1))

flickr = flickrapi.FlickrAPI(my_api_key, api_secret)
(token, frob) = flickr.get_token_part_one(perms='read')
if not token:
    raw_input("Press ENTER after you authorized this program.")
flickr.get_token_part_two((token, frob))

# see http://www.flickr.com/services/api/flickr.photos.getSizes.html
# for an example of XML returned by photos_getSizes(...)

# print the URL of the Thumbnail image for each Photo ID we have
for my_photo_id in photo_ids:
    resultxml = flickr.photos_getSizes(api_key=my_api_key, photo_id=my_photo_id)
    imgurl = resultxml.find("sizes").findall("size")[1].get('source')
    print '<' + 'img src="' + imgurl + '" /' + '>'

Comments Off

5 June 2009 11:51 pm

Comments are closed.

 

Copyright © 2001-2006, Prasenjeet Dutta. Terms of Use.

RSS Subscription Icon Subscribe

Powered by WordPress