from playwright.sync_api import sync_playwright

URL = "https://www.metal-archives.com/lists/FR"

def main():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)  # IMPORTANT: visible
        context = browser.new_context()
        page = context.new_page()

        print("Ouvre la page. Si Cloudflare challenge apparaît, résous-le dans la fenêtre.")
        page.goto(URL, wait_until="domcontentloaded")

        print("J'attends qu'un appel /browse/ajax-country/ passe en 200 (preuve que l'accès est OK)...")

        def ok_ajax(resp):
            return ("/browse/ajax-country/" in resp.url) and resp.status == 200

        # Attends que DataTables charge réellement (XHR 200)
        page.wait_for_response(ok_ajax, timeout=180_000)

        context.storage_state(path="ma_state.json")
        print("✅ ma_state.json sauvegardé. Tu peux fermer le navigateur.")
        browser.close()

if __name__ == "__main__":
    main()
