Last active
November 13, 2020 18:41
-
-
Save thisismattmiller/19d13e5238158198f066bae702b934eb to your computer and use it in GitHub Desktop.
Use the met API to get object information
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 json | |
| # the search end point | |
| search_end_point = 'https://collectionapi.metmuseum.org/public/collection/v1/search' | |
| # the object endpoint | |
| object_end_point = 'https://collectionapi.metmuseum.org/public/collection/v1/objects/' | |
| # let's make a place for all of our object data to live | |
| object_data = [] | |
| # lets do our search | |
| # setup our search paramaters | |
| params = { | |
| 'q' : 'cats', | |
| 'hasImages' : 'true' | |
| } | |
| # use the search endpoint with the params we wanted | |
| search_results = requests.get(search_end_point, params=params) | |
| # turn the results into data | |
| search_data = json.loads(search_results.text) | |
| # this looks like this: | |
| # {'total': 143, 'objectIDs': [551786, 472562, 54474, ...]} | |
| # its an dictonary and the ids live in objectIDs | |
| # so we can loo pthrough them | |
| for object_id in search_data['objectIDs']: | |
| print(object_id) | |
| # lets make a request to the object endpoint for this object | |
| # put together the two piece, the base end point and our | |
| # object nubmer, but we gotta convert it to a string to be able to add it to the url string | |
| url = object_end_point + str(object_id) | |
| # make that request | |
| # no params, because the object number id part of the URL | |
| # in this case, for example it is not "http://.../public/collection/v1/objects/?object=123456" it is "http://.../public/collection/v1/objects/123456" | |
| object_result = requests.get(url) | |
| # make it data | |
| obj_data = json.loads(object_result.text) | |
| # add it to our big list | |
| object_data.append(obj_data) | |
| #once we've done all the objects write it all out | |
| with open('met_objects.json','w') as out: | |
| json.dump(object_data,out,indent=2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment