"""
GLAMWalk -- Volume 2: The Gauntlet
03_headless_browser_fetch.py

A real Playwright headless-browser fetch: loading a JavaScript-rendered page, waiting for it to finish building, then extracting data with both a regex (matching a known URL shape) and a BeautifulSoup CSS selector. Requires: pip install playwright beautifulsoup4, then `playwright install chromium` once.

Source: Volume Two, Sections 1.3-1.4
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.
It's safe to run as-is, but requires an internet connection.
"""

import requests

html = requests.get("https://collections.artsmia.org/art/17/").text
print(html)
# ...returns a nearly-empty shell. The scripts that would have
# built the actual image content never ran, because a plain
# HTTP request doesn't execute JavaScript at all.


import re
from playwright.sync_api import sync_playwright

# The real, working pattern this project's image URLs actually follow —
# used to pull them straight out of the rendered page's raw HTML.
IMG_PATTERN = re.compile(r"https://img\.artsmia\.org/web_objects_cache/[^\s\"'<>]+\.jpg")

def fetch_images_via_browser(obj_id):
    """
    Only called as a fallback, for the objects where the live API
    (Section 1.1) didn't return an image on its own.
    """
    url = f"https://collections.artsmia.org/art/{obj_id}/"

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)   # a real engine, no window
        page = browser.new_page()

        # "networkidle" is a different waiting strategy than picking one
        # specific element to wait for: it waits until the page has gone
        # a short stretch with no new network activity at all — a
        # reasonable proxy for "everything has probably finished loading"
        # when you don't know in advance exactly which element will appear.
        page.goto(url, wait_until="networkidle", timeout=30_000)
        rendered_html = page.content()
        browser.close()

    # Pull every matching image URL out of the now-fully-built page,
    # and dict.fromkeys() removes duplicates while preserving order —
    # a real, common idiom, since a plain set() would lose the order
    # the images actually appeared in.
    return list(dict.fromkeys(IMG_PATTERN.findall(rendered_html)))

images = fetch_images_via_browser(17)
print(images)


from bs4 import BeautifulSoup   # pip install beautifulsoup4

def extract_title(rendered_html):
    """rendered_html is whatever page content you already fetched —
    from fetch_images_via_browser()'s page.content(), or any other
    source. This is a separate extraction technique from the regex
    approach above, shown on its own small example page here."""
    soup = BeautifulSoup(rendered_html, "html.parser")
    title_el = soup.select_one(".artwork-detail .artwork-title")
    return title_el.get_text(strip=True) if title_el else "UNKNOWN"

_example_html = '<div class="artwork-detail"><h1 class="artwork-title">Sunflowers</h1></div>'
print(extract_title(_example_html))
