Skip to content

Instantly share code, notes, and snippets.

@dnywh
Created January 3, 2023 00:13
Show Gist options
  • Save dnywh/ada4ec631a7c4d0b32e0e00b7b91cb0e to your computer and use it in GitHub Desktop.
Save dnywh/ada4ec631a7c4d0b32e0e00b7b91cb0e to your computer and use it in GitHub Desktop.
An example of searching the Art Institute of Chicago's API in Python
import requests
import math
limit = 90
fields = "id,image_id,title,artist_id,medium_display"
url = f"https://api.artic.edu/api/v1/artworks/search?limit={limit}&fields={fields}"
headers = {"user-agent": "test-app/[email protected]"}
searchQuery = {
"query": {
"bool": {
"must": [
{"term": {"is_public_domain": True}},
{"match": {"term_titles": "woodcut"}},
# {"match": {"classification_titles": "etching"}},
# {"match": {"subject_titles": "geometric"}},
# {"range": {"color.h": {"lt": 2}}},
],
"should": [
{"term": {"is_boosted": True}},
],
# "must_not": [
# {"match": {"medium_display": "Ceramic and pigment"}},
# {"match": {"medium_display": "Plant fibers"}},
# {"match": {"term_titles": "metalwork"}},
# ],
}
}
}
# Post request
r = requests.post(url, headers=headers, json=searchQuery)
# Parse results
art = r.json()
resultSize = art["pagination"]["total"]
pages = int(math.ceil(resultSize / 10))
print("Total results:", resultSize, "Limited to:", len(art["data"]))
# Prepare image format
imageParams = "/full/843,/0/default.jpg"
# Loop through and show URL for each image within the limit
# Nest this within a `range(1, pages)` loop first if you'd like to access each item within each page
for i in range(len(art["data"])):
imageId = art["data"][i]["image_id"]
print(f"{art['data'][i]['title']}, {art['data'][i]['id']}:")
print(f"https://www.artic.edu/iiif/2/{imageId}{imageParams}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment