Spaces:
Sleeping
Sleeping
Merge pull request #6 from MrinalGoel643/feature/error-handling
Browse files- met_api.py +42 -21
met_api.py
CHANGED
|
@@ -11,28 +11,45 @@ Need to add error handling
|
|
| 11 |
|
| 12 |
# gets information on a single object
|
| 13 |
def get_object(objectID):
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# gets all the objects that have some sort of image
|
| 17 |
def get_objectsWithImages():
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# gets the urls for random objects with images
|
| 24 |
def get_images(totalObjects, objectIDs, limit):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def department_counts(q="*", max_ids=200):
|
| 38 |
"""
|
|
@@ -69,10 +86,14 @@ def list_met_departments():
|
|
| 69 |
"""
|
| 70 |
Return a DataFrame of all Met departments (id + name) to help you choose.
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
"""
|
| 78 |
returns a dataframe of list of objects with images and metadata that matches search term
|
|
@@ -185,4 +206,4 @@ def main():
|
|
| 185 |
print(results.to_string(index=False))
|
| 186 |
|
| 187 |
if __name__ == '__main__':
|
| 188 |
-
main()
|
|
|
|
| 11 |
|
| 12 |
# gets information on a single object
|
| 13 |
def get_object(objectID):
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get(f"{URL}/objects/{objectID}")
|
| 16 |
+
response.raise_for_status()
|
| 17 |
+
return response.json()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Error fetching object {objectID}: {e}")
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
|
| 23 |
# gets all the objects that have some sort of image
|
| 24 |
def get_objectsWithImages():
|
| 25 |
+
try:
|
| 26 |
+
response = requests.get(f"{URL}search?hasImages=true&q=*")
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
data = response.json()
|
| 29 |
+
total = data.get("total", 0)
|
| 30 |
+
objectIDs = data.get("objectIDs", [])
|
| 31 |
+
return total, objectIDs
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error fetching objects with images: {e}")
|
| 34 |
+
return 0, []
|
| 35 |
|
| 36 |
# gets the urls for random objects with images
|
| 37 |
def get_images(totalObjects, objectIDs, limit):
|
| 38 |
+
try:
|
| 39 |
+
images = []
|
| 40 |
+
# grabbing extra in case a primary image is blank (Works best on small limits)
|
| 41 |
+
rand_indexes = random.sample(range(totalObjects), limit + 20)
|
| 42 |
+
for i in rand_indexes:
|
| 43 |
+
obj = get_object(objectIDs[i])
|
| 44 |
+
if obj and obj.get("primaryImage"):
|
| 45 |
+
images.append((obj["primaryImage"], obj.get("title", "Untitled")))
|
| 46 |
+
if len(images) == limit:
|
| 47 |
+
break
|
| 48 |
+
|
| 49 |
+
return images
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Error in get_images: {e}")
|
| 52 |
+
return []
|
| 53 |
|
| 54 |
def department_counts(q="*", max_ids=200):
|
| 55 |
"""
|
|
|
|
| 86 |
"""
|
| 87 |
Return a DataFrame of all Met departments (id + name) to help you choose.
|
| 88 |
"""
|
| 89 |
+
try:
|
| 90 |
+
r = requests.get(f"{URL}/departments")
|
| 91 |
+
r.raise_for_status()
|
| 92 |
+
depts = r.json().get("departments", [])
|
| 93 |
+
return pd.DataFrame(depts)[["departmentId", "displayName"]]
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"Error fetching departments: {e}")
|
| 96 |
+
return pd.DataFrame(columns=["departmentId", "displayName"])
|
| 97 |
|
| 98 |
"""
|
| 99 |
returns a dataframe of list of objects with images and metadata that matches search term
|
|
|
|
| 206 |
print(results.to_string(index=False))
|
| 207 |
|
| 208 |
if __name__ == '__main__':
|
| 209 |
+
main()
|