Last active
January 19, 2016 07:39
-
-
Save peakode/b680dee00a3b7ba1db00 to your computer and use it in GitHub Desktop.
This file contains 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
# Written by Can Uludag | |
# Co-Founder at Peakode & Lead Android Engineer | |
# www.peakode.com | |
# We are using below libraries | |
import requests | |
import json | |
import urllib | |
# Base public search api | |
base_api_url = 'http://api.repo.nypl.org/api/v1/items/search?q=' | |
# Your token from NYPL API page. You need to sign up in order to get. Replace this with yours. | |
nypl_auth_token = 'your_authentication_token' | |
# Gets search results | |
def get_search_results(query, is_public_domain_only, pagination): | |
print('Getting search results...\n\n') | |
# Generating the final api link | |
# We searched 'Public Domain Only' collections | |
search_url = base_api_url + query + '&publicDomainOnly=' + is_public_domain_only + '&per_page=' + str(pagination) | |
# Creating custom headers for GET request | |
# This Authorization headers is required for access to api | |
auth_headers = {'Authorization': 'Token token=' + nypl_auth_token} | |
# GET request with using Requests library | |
req = requests.get(search_url, headers=auth_headers) | |
binary = req.content | |
jsonData = json.loads(binary) | |
# Getting result json array from the response json | |
results_array = jsonData['nyplAPI']['response']['result'] | |
# Downloading images | |
for result in results_array: | |
download_images_with_api(result['imageID']) | |
# This method is little bit different. | |
# It's not using the API but still can get photos from the url | |
# There is no query options. It's based on image_id | |
# Downloads images starting from an image id number | |
# I'm not sure it is legal. Not advising to use | |
def download_images_with_public_url(): | |
print('Download started...') | |
image_id = 0 | |
for x in range(0, 50): | |
print 'Downloading image no ' + str(image_id) | |
photo_url = 'http://images.nypl.org/index.php?id=' + str(image_id) + '&t=w&download=0' | |
# Method for downloading images | |
urllib.urlretrieve(photo_url, str(image_id) + '.jpg') | |
image_id += 1 | |
print 'Image saved to file ' + str(image_id) + '.jpg' | |
# This method download images with using public api | |
# Downloads photo with the given image_id | |
def download_images_with_api(image_id): | |
print('Download started...') | |
photo_url = 'http://images.nypl.org/index.php?id=' + str(image_id) + '&t=w&download=0' | |
# Method for downloading images | |
urllib.urlretrieve(photo_url, str(image_id) + '.jpg') | |
print 'Image saved to file ' + str(image_id) + '.jpg' |
This file contains 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
# Written by Can Uludag | |
# Co-Founder at Peakode & Lead Android Engineer | |
# www.peakode.com | |
import GetNYPLCollectionImagesHelper as helper | |
if __name__ == '__main__': | |
helper.get_search_results('query_keyword', 'true', 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please use it for personal use. In the example above, we used 'Public Domain Only' search criteria and downloaded these type images.