Created
December 27, 2013 22:49
-
-
Save armanbilge/8153747 to your computer and use it in GitHub Desktop.
Downloads photos from a Flickr set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment