FlickrSetDownloadr.py


SUBMITTED BY: Guest

DATE: Jan. 19, 2014, 5:11 p.m.

FORMAT: Python

SIZE: 1.8 kB

HITS: 887

  1. #!/usr/bin/env python
  2. # FlickrSetDownloadr.py
  3. # Downloads photos from a Flickr set
  4. # Usage: python FlickrSetDownloadr.py [PHOTOSET_ID]
  5. import sys
  6. from collections import deque
  7. try:
  8. from xml.etree.cElementTree import fromstring
  9. except:
  10. from xml.etree.ELementTree import fromstring
  11. from urllib import urlencode, urlretrieve
  12. from urllib2 import urlopen
  13. API = 'http://api.flickr.com/services/rest/'
  14. API_KEY = 'f354be9d986de18bb2125c81b438b660'
  15. PHOTOSET_ID = sys.argv[1]
  16. pages = int(fromstring(urlopen(API, urlencode({'method' : 'flickr.photosets.getPhotos',
  17. 'api_key' : API_KEY,
  18. 'photoset_id' : PHOTOSET_ID})).read()).find('photoset').attrib['pages'])
  19. for i in xrange(1,pages + 1):
  20. set = fromstring(urlopen(API, urlencode({'method' : 'flickr.photosets.getPhotos',
  21. 'api_key' : API_KEY,
  22. 'photoset_id' : PHOTOSET_ID,
  23. 'page' : i})).read()).find('photoset')
  24. for photo in set.iter('photo'):
  25. url = (deque(fromstring(urlopen(API, urlencode({'method' : 'flickr.photos.getSizes',
  26. 'api_key' : API_KEY,
  27. 'photo_id' : photo.attrib['id']})).read())
  28. .find('sizes').iter('size'), maxlen=1)
  29. .pop().attrib['source'])
  30. urlretrieve(url, url.split('/')[-1])

comments powered by Disqus