Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Downloading Harvard ART API records
import requests
import json
with open('key.json') as keyfile:
key_data = json.load(keyfile)
api_key = key_data['key']
#mediums_i_want = ["2028333","2028206","2028902","2035812","2028183","2035306"]
mediums_i_want = ["2028216"]
medium_ids_string = "|".join(mediums_i_want)
print(medium_ids_string)
url = "https://api.harvardartmuseums.org/object"
params = {
'apikey' : api_key,
'medium' : medium_ids_string,
'size': 100
}
r = requests.get(url, params=params)
data = json.loads(r.text)
print(data['info'])
counter = 1
max_pages = 0
max_pages = data['info']['pages']
while counter <= max_pages:
print("Doing page",counter)
params = {
'apikey' : api_key,
'medium' : medium_ids_string,
'size': 100,
'page' : counter
}
r = requests.get(url, params=params)
data = json.loads(r.text)
for rec in data['records']:
# filename = str(rec['id']) + '.json'
filename = f"{rec['id']}.json"
json.dump(rec, open('records/'+filename,'w'),indent=2)
print(filename)
counter = counter + 1
import json
import glob
all_dates = []
for file in glob.glob('records/*.json'):
with open(file) as jsonfile:
data = json.load(jsonfile)
if 'datebegin' in data:
if data['datebegin'] != 0:
all_dates.append(data['datebegin'])
print('we have',len(all_dates), 'total number of dates')
total_number_dates = len(all_dates)
total_date_value = 0
for date in all_dates:
total_date_value = total_date_value + date
print('avg is:', total_date_value / total_number_dates)
{
"key": "YOUR KEY HERE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment