#!/usr/bin/env python
# FlickrSetDownloadr.py
# Downloads photos from a Flickr set
# Usage: python FlickrSetDownloadr.py [PHOTOSET_ID]
import sys
from collections import deque
try:
from xml.etree.cElementTree import fromstring
except:
from xml.etree.ELementTree import fromstring
from urllib import urlencode, urlretrieve
from urllib2 import urlopen
API = 'http://api.flickr.com/services/rest/'
API_KEY = 'f354be9d986de18bb2125c81b438b660'
PHOTOSET_ID = sys.argv[1]
pages = int(fromstring(urlopen(API, urlencode({'method' : 'flickr.photosets.getPhotos',
'api_key' : API_KEY,
'photoset_id' : PHOTOSET_ID})).read()).find('photoset').attrib['pages'])
for i in xrange(1,pages + 1):
set = fromstring(urlopen(API, urlencode({'method' : 'flickr.photosets.getPhotos',
'api_key' : API_KEY,
'photoset_id' : PHOTOSET_ID,
'page' : i})).read()).find('photoset')
for photo in set.iter('photo'):
url = (deque(fromstring(urlopen(API, urlencode({'method' : 'flickr.photos.getSizes',
'api_key' : API_KEY,
'photo_id' : photo.attrib['id']})).read())
.find('sizes').iter('size'), maxlen=1)
.pop().attrib['source'])
urlretrieve(url, url.split('/')[-1])