"""
GLAMWalk -- Volume 2: The Gauntlet
06_rijksmuseum_facet_crawl.py

Crawling the Rijksmuseum's full collection facet-by-facet (one output file per object type), the pattern this project used for any collection too large to fetch in one query.

Source: Volume Two, Section 2.4 -- Facet-by-Facet Crawling
Targets Python 3.11.4 -- see Volume One, Chapter 1.5/1.6.

NOTE: This script makes real network requests to a live public API.
Targets Python 3.11.4 -- see Volume One, Chapter 1.5/1.6.
"""

import requests


def search_rijksmuseum(object_type, page=0, page_size=50):
    """Same function as in 05_rijksmuseum_decoy_and_real_api.py —
    included here too so this file runs standalone."""
    url = "https://data.rijksmuseum.nl/search/collection"
    params = {"type": object_type, "p": page, "ps": page_size}
    response = requests.get(url, params=params, timeout=20)
    response.raise_for_status()
    return response.json()


OBJECT_TYPES = [
    "painting", "print", "drawing", "sculpture",
    "photograph", "furniture", "ceramic",
    # ...dozens more, discovered by exploring the API's own facet listing
]

def crawl_all_types(output_dir):
    for object_type in OBJECT_TYPES:
        page = 0
        all_results = []
        while True:
            batch = search_rijksmuseum(object_type, page=page)
            items = batch.get("artObjects", [])
            if not items:
                break
            all_results.extend(items)
            page += 1

        print(f"{object_type}: {len(all_results)} objects")
        # ...write all_results to its own CSV, one file per type,
        # so a later import step can discover and process each file
        # independently, and a partial crawl is trivially resumable.
