Created
November 8, 2017 06:49
-
-
Save tomy0000000/cc979d0372c6fbdbf04e4adb1000bd82 to your computer and use it in GitHub Desktop.
Download Flickr Photos
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
| import requests | |
| import bs4 | |
| import json | |
| apiKey = input("API Key: ") | |
| List_of_PhotoIDs = input().split(" ") | |
| Dict_of_Links = {} | |
| for each in List_of_PhotoIDs: | |
| link = "https://api.flickr.com/services/rest/" | |
| PhotoPara = { | |
| "method":"flickr.photos.getSizes", | |
| "api_key":apiKey, | |
| "photo_id":each, | |
| "format":"json", | |
| "nojsoncallback":"1" | |
| } | |
| PhotoPage = requests.get(link, params=PhotoPara) | |
| PhotoSoup = bs4.BeautifulSoup(PhotoPage.text, "lxml") | |
| PhotoData = json.loads(PhotoPage.text) | |
| Dict_of_Photo = {} | |
| for eachDimen in range(len(PhotoData["sizes"]["size"])): | |
| Dict_of_Photo[int(PhotoData["sizes"]["size"][eachDimen]["width"])*int(PhotoData["sizes"]["size"][eachDimen]["height"])] = [PhotoData["sizes"]["size"][eachDimen]["label"], PhotoData["sizes"]["size"][eachDimen]["source"]] | |
| Dict_of_Links[each] = Dict_of_Photo[max(Dict_of_Photo)][1] | |
| print("Photo Link Found: ", each, "Dimension:", Dict_of_Photo[max(Dict_of_Photo)][0]) | |
| print() | |
| print("Links Fetched, Now Downloading") | |
| print() | |
| for each in Dict_of_Links: | |
| downPage = requests.get(Dict_of_Links[each]) | |
| with open(each+".jpg", "wb") as f: | |
| f.write(downPage.content) | |
| print("Photo Saved:", each) | |
| print() | |
| print("Successfully Saved", len(Dict_of_Links), "Photos") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment