Created
July 31, 2023 20:50
-
-
Save ethanholda/a6a856d6a326e980f08b5a8aa816568a to your computer and use it in GitHub Desktop.
EH answer to his own code test.
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
import json | |
import requests | |
from operator import itemgetter | |
from io import BytesIO | |
def get_exhibition_by_name(s): | |
url = "https://openaccess-api.clevelandart.org/api/exhibitions/" | |
params = { | |
"title": s | |
} | |
r = requests.get(url, params=params) | |
data = r.json() | |
return data['data'] if (data and len(data['data'])) else None | |
def get_exhibition_by_id(ex_id): | |
url = f"https://openaccess-api.clevelandart.org/api/exhibitions/{ex_id}" | |
r = requests.get(url) | |
data = r.json() | |
return data['data'] if (data and data.get('data')) else None | |
def get_artwork_by_id(art_id): | |
url = f"https://openaccess-api.clevelandart.org/api/artworks/{art_id}" | |
r = requests.get(url) | |
data = r.json() | |
return data['data'] if (data and data.get('data')) else None | |
def get_image_file_object(image_url): | |
r = requests.get(image_url) | |
f = BytesIO(r.content) | |
return f | |
def get_image_matches_from_url(image_url): | |
image_file = get_image_file_object(image_url) | |
# image_file = open("/Users/ethanholda/temp/1954.387_web.jpg", "rb") | |
url = "https://openaccess-api.clevelandart.org/api/artworks/" | |
file_data = { | |
'file': image_file | |
} | |
r = requests.post(url, params={"limit":6}, files=file_data) # get 6 because first match will be your work | |
data = r.json() | |
return data['data'] | |
def make_similarity_exhibition(exname): | |
exhibition_results = get_exhibition_by_name(exname) | |
ex_id = exhibition_results[0]["id"] | |
exhibition = get_exhibition_by_id(ex_id) | |
artworks = [] | |
for a in exhibition['artworks']: | |
artwork = get_artwork_by_id(a['id']) | |
artworks.append(artwork) | |
artworks_sorted = sorted(artworks, key=itemgetter('creation_date_earliest')) | |
my_artwork = artworks_sorted[0] # OLDEST | |
image_matches = get_image_matches_from_url(my_artwork["images"]["web"]["url"]) | |
return image_matches | |
def post_data_to_mongo_db(record): | |
url = "https://us-east-2.aws.data.mongodb-api.com/app/data-wkbgg/endpoint/data/v1" | |
testkey = "YOUR API KEY" | |
headers = { | |
"api-key": testkey | |
} | |
data = { | |
"dataSource": "Cluster0", | |
"database": "testdb", | |
"collection": "test_collection", | |
"document": record | |
} | |
method = "/action/insertOne" | |
api_url = f"{url}{method}" | |
r = requests.post(api_url, headers=headers, json=data) | |
print(r.status_code) | |
return r.status_code == 200 | |
""" | |
Get an artwork from an exhibition based on arbitrary criteria (in this case oldest) | |
and get the top 5 image similarity matches of that work. | |
Post results to Mongo Atlas collection. | |
""" | |
#exname = "Egyptomania: Fashion's Conflicted Obsession" | |
exname = "Riemenschneider and Late Medieval Alabaster" | |
mini_exhibition = make_similarity_exhibition(exname) | |
for artwork in mini_exhibition: | |
record = { | |
"artwork_id": artwork["id"], | |
"accession_number": artwork["accession_number"], | |
"tombstone": artwork["tombstone"], | |
"creation_date_earliest": artwork["creation_date_earliest"], | |
"image_url": artwork["images"]["web"]["url"] | |
} | |
if post_data_to_mongo_db(record): | |
print(f"INSERTED: {record}") | |
else: | |
print(f"FAILED: {record}") | |
#print(json.dumps(image_matches, indent=2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment