import requests

BASE_URL = "https://collectionapi.metmuseum.org/public/collection/v1"

def get_object(object_id):
    """Fetch one object's full metadata from the Met's public API."""
    url = f"{BASE_URL}/objects/{object_id}"
    response = requests.get(url, timeout=15)
    response.raise_for_status()   # crash loudly if the server returned an error
    return response.json()

def search_objects(query, has_images=True):
    """Search the Met's collection for a keyword, returning matching object IDs."""
    params = {"q": query, "hasImages": has_images}
    response = requests.get(f"{BASE_URL}/search", params=params, timeout=15)
    response.raise_for_status()
    return response.json().get("objectIDs", []) or []

if __name__ == "__main__":
    ids = search_objects("sunflowers")
    print(f"Found {len(ids)} matching objects.")

    for oid in ids[:5]:            # just look at the first 5 for now
        obj = get_object(oid)
        print("-", obj.get("title"), "by", obj.get("artistDisplayName") or "Unknown")