matthileo

Flickr Set Downloader

Oct 28th, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. #!/usr/bin/python
  2. import flickrapi
  3. import urllib2
  4. from optparse import OptionParser
  5. import sys
  6.  
  7.  
  8. api_key  =  'THISISANAPIKEY' #get your own!
  9. userID='00000000@N06' #This is a user ID, you'll want to use your own
  10. setID=''
  11.  
  12. parser = OptionParser()
  13. parser.add_option ("-s","--setid",type="string",dest="setID",help="The Set ID for the set to download.")
  14. parser.add_option ("-S" , "--size", type="string", dest="photoSize",default="b",help="The size of photo you want to download. Size must fit the following: s - 75x75, q - 150x150, t - 100 on the longest side, m - 240 on the longest side, n - 320 on the longest side, z - 640 on the longest side, c - 800 on the longest side, \nb - 1024 on the longest side (default), o - original")
  15.  
  16. (options, args) = parser.parse_args()
  17.  
  18. setID = options.setID
  19. photoSize = options.photoSize
  20.  
  21. if options.setID is None:
  22.     print "You must specify a photo set to download. \nSee python-download -h for examples."
  23.     sys.exit()
  24.    
  25. flickr = flickrapi.FlickrAPI(api_key)
  26. photoSet = flickr.photosets_getPhotos(photoset_id=setID)
  27. photoSet = photoSet[0]
  28.  
  29. def download( url ):
  30.     file_name = url.split('/')[-1]
  31.     u = urllib2.urlopen(url)
  32.     f = open(file_name, 'wb')
  33.     meta = u.info()
  34.     file_size = int(meta.getheaders("Content-Length")[0])
  35.     print "Downloading: %s Bytes: %s" % (file_name, file_size)
  36.  
  37.     file_size_dl = 0
  38.     block_sz = 8192
  39.     while True:
  40.         buffer = u.read(block_sz)
  41.         if not buffer:
  42.         break
  43.  
  44.         file_size_dl += len(buffer)
  45.         f.write(buffer)
  46.         status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
  47.         status = status + chr(8)*(len(status)+1)
  48.         print status,
  49.  
  50.     f.close()
  51.     return
  52.  
  53. for photo in photoSet:
  54.     photoID = photo.attrib['id']
  55.     photoInfo = flickr.photos_getInfo(photo_id=photoID)
  56.     photoInfo = photoInfo[0]
  57.     secret=photoInfo.attrib['secret']
  58.     oSecret=photoInfo.attrib['originalsecret']
  59.    
  60.     if photoSize == "o":
  61.         download("http://farm%s.static.flickr.com/%s/%s_%s_o.jpg" % (photo.attrib['farm'], photo.attrib['server'], photo.attrib['id'],oSecret))
  62.     else:
  63.         download("http://farm%s.static.flickr.com/%s/%s_%s_%s.jpg" % (photo.attrib['farm'], photo.attrib['server'], photo.attrib['id'],secret,photoSize))
Add Comment
Please, Sign In to add comment