"""Open each page in Lightpanda through agent-browser, and reopen it in Chrome when the page has almost no text.
Set LIGHTPANDA to the Lightpanda binary. Run with: python3 fallback.py https://pypi.org/project/httpx/ https://bsky.app/"""
import os, subprocess, sys

ENGINES = {"lightpanda": {"AGENT_BROWSER_ENGINE": "lightpanda", "AGENT_BROWSER_EXECUTABLE_PATH": os.environ["LIGHTPANDA"],
                          "LIGHTPANDA_DISABLE_TELEMETRY": "true"},
           "chrome": {}}
MIN_WORDS = 100  # fewer words than this counts as an empty page (textContent works without layout)
WORDS = """(() => { const b = document.body?.cloneNode(true); if (!b) return 0;
  b.querySelectorAll('script, style, noscript, template').forEach(e => e.remove());
  return b.textContent.split(/\\s+/).filter(Boolean).length })()"""


def load(url, engine):
    env, session = {**os.environ, **ENGINES[engine]}, f"fallback-{engine}-{abs(hash(url))}"  # one session per page
    ab = lambda *args: subprocess.run(["agent-browser", "--session", session, *args], env=env,
                                      capture_output=True, text=True, timeout=90).stdout.strip()
    try:
        ab("open", url)
        ab("wait", "--load", "networkidle")
        words = ab("eval", WORDS)
        return int(words) if words.isdigit() else 0, ab("snapshot", "-i")
    finally:
        ab("close")


for url in sys.argv[1:]:
    words, snap = load(url, "lightpanda")
    note = f"lightpanda {words:>5,} words"
    if words < MIN_WORDS:
        words, snap = load(url, "chrome")
        note += f" -> chrome {words:>5,} words"
    print(f"{url:<34} {note}  snapshot {len(snap):,} chars")
