· 6 years ago · Apr 21, 2020, 07:28 PM
1# this lacks a TON of exception handling, I'll be updating it as I go along
2
3import flickrapi
4import json
5import urllib.request
6
7
8def downloader():
9 api_key = input("what is your api key?") # Get this from Flickr
10 api_secret = input("what is your api secret key?") # Get this from Flickr
11 photoset = input("what is the id of the photoset you're trying to download from?")
12 user = input("what is the user id you're trying to access?")
13 as_of_date = input("as of what date are you trying to download the pictures? Please use the following "
14 "format YYYY-MM-DD") # 2020-04-15
15 flickr = flickrapi.FlickrAPI(api_key, api_secret)
16 pass1 = flickr.photosets.getPhotos(api_key=api_key, photoset_id=photoset, user_id=user
17 , format='json', page=1) # there's definitely a better way to do this, the flickrapi only returns a max of 500 pictures per, since the album currently has 836 pictures, I did two calls. I'll need to fix this as more pictures are added
18 pass2 = flickr.photosets.getPhotos(api_key=api_key, photoset_id=photoset, user_id=user
19 , format='json', page=2)
20 dict1 = json.loads(pass1) # my dog was not in the first 500 pictures, so dict1 is not used anywhere
21 dict2 = json.loads(pass2)
22 i = 1
23 for j in dict2["photoset"]["photo"]: # for every photo in dict2
24 date = flickr.photos.getInfo(api_key=api_key, photo_id=j["id"], format='json') # this call gets the info of each picture, this has the date captured attribute that I need
25 picture = flickr.photos.getSizes(api_key=api_key, photo_id=j["id"], format='json') # for some reason, the folks at flickr thought it a good idea to add the URL for the picture in a .getSizes() function? Very confusing naming
26 dict_date = json.loads(date)
27 dict_pic = json.loads(picture)
28 og_pic_url = dict_pic["sizes"]["size"][15]["source"] # the original size is always the last element of the size list, and it's always 15
29 taken = dict_date["photo"]["dates"]["taken"]
30 if taken[0:10] == as_of_date: # I need to put some error handling here
31 print(dict_date["photo"])
32 fullname = dict_date["photo"]["title"]["_content"] + ".jpg" # I need to add a directory path, saving a bunch of 10MB+ pictures to my small SSD is a no go
33 url = og_pic_url
34 urllib.request.urlretrieve(url, fullname) # this actually downloads the pictures
35 i += 1
36 else:
37 print(str(i) + " " + taken[0:10])
38 i += 1
39 pass