#!/usr/bin/env python3
"""探测 gddrc: playwright 过 TLS 指纹 + 列表结构"""
import asyncio, json, re, sys

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"

async def main():
    from playwright.async_api import async_playwright
    from playwright_stealth import Stealth
    async with async_playwright() as p:
        browser = await p.chromium.launch(
            headless=True,
            executable_path='/root/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome',
            args=['--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled']
        )
        stealth = Stealth()
        ctx = await browser.new_context(user_agent=UA, locale='zh-CN',
                                        viewport={'width': 1440, 'height': 900}, timezone_id='Asia/Shanghai')
        await stealth.apply_stealth_async(ctx)
        page = await ctx.new_page()

        url = "https://drc.gd.gov.cn/gdsnyj/gkmlpt/index#3869"
        print(f"[gddrc] {url}")
        try:
            r = await page.goto(url, wait_until='domcontentloaded', timeout=45000)
            await page.wait_for_timeout(8000)
            t = await page.title()
            html = await page.content()
            print(f"  status={r.status if r else '?'} title={t[:60]} len={len(html)}")
            # 列表项
            items = await page.evaluate("""() => {
                const out = [];
                document.querySelectorAll('a').forEach(a => {
                    const t = (a.innerText||'').trim();
                    if (t && t.length > 8 && /20\\d{2}/.test(t)) out.push({t: t.slice(0,50), h: a.href});
                });
                return out.slice(0,12);
            }""")
            for it in items:
                print(f"  item: [{it['t']}] {it['h'][:100]}")
            # 栏目名
            m = re.search(r'<title>([^<]*)</title>', html)
            print(f"  title: {m.group(1) if m else '?'}")
        except Exception as e:
            print(f"  ERR: {str(e)[:100]}")

        await browser.close()

asyncio.run(main())
