Skip to content

Instantly share code, notes, and snippets.

@AnthonyBloomer
Created April 11, 2017 16:09
Show Gist options
  • Save AnthonyBloomer/1ff701e6bae81e11b60935e6480d6c66 to your computer and use it in GitHub Desktop.
Save AnthonyBloomer/1ff701e6bae81e11b60935e6480d6c66 to your computer and use it in GitHub Desktop.
Download photosets on Flickr.
import requests
import urllib
import pprint
import os
import argparse
import sys
def main(photoset):
try:
os.mkdir(photoset)
print 'Created directory - ' + photoset
except OSError:
print 'Directory already exists. Continuing. '
pass
os.chdir(photoset)
get_set = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=9211be81eef759b191584bd4f929148f&photoset_id=%s&format=json&nojsoncallback=1' % photoset
resp = requests.get(get_set)
results = resp.json()
pprint.pprint(results)
for photo in results['photoset']['photo']:
ep = 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=9211be81eef759b191584bd4f929148f&photo_id=%s&format=json&nojsoncallback=1' % photo['id']
r = requests.get(ep)
v = r.json()
for i in v['sizes']['size']:
pprint.pprint(i)
if i['label'] == 'Original':
image_source = i['source']
fname = image_source.rsplit('/', 1)[-1]
if not os.path.exists(fname):
urllib.urlretrieve(image_source, fname)
os.chdir('..')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('sets', nargs='+')
args = parser.parse_args()
for s in args.sets:
main(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment