Skip to content

Instantly share code, notes, and snippets.

@canuludag
Last active January 19, 2016 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canuludag/ec5a5af5dcf81a1d9199 to your computer and use it in GitHub Desktop.
Save canuludag/ec5a5af5dcf81a1d9199 to your computer and use it in GitHub Desktop.
# 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.
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')
# 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
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'
import GetNYPLCollectionImagesHelper as helper
if __name__ == '__main__':
helper.get_search_results('query_keyword', 'true', 100)
@canuludag
Copy link
Author

Lütfen kişisel kullanım amaçlı kullanın.

@canuludag
Copy link
Author

Please use it for personal use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment