#!/usr/bin/env python3
"""Find the actual API endpoint in commonApi.js and try rendering with playwright"""
import requests, re, json, sys
sys.stdout.reconfigure(encoding='utf-8')

# Read the full commonApi.js to find the API URL
r = requests.get('https://www.xiangfuqu.gov.cn/default/libs/commonApi.js', 
                 headers={'User-Agent': 'Mozilla/5.0'}, timeout=30)
js = r.text

# Find all potential API URLs
for m in re.finditer(r'["\']((?:https?://)?[^"\']*(?:api|list|article|content)[^"\']*\.(?:json|do|action|html)[^"\']*)["\']', js):
    url = m.group(1)
    if len(url) > 15:
        print(f"API URL in JS: {url[:120]}")

# Find any post/get paths
for m in re.finditer(r"(?:url|path|api)[\s]*[:=][\s]*['\"]([^'\"]+)['\"]", js, re.IGNORECASE):
    path = m.group(1)
    if 'api' in path.lower() or 'list' in path.lower() or 'article' in path.lower() or 'content' in path.lower():
        if len(path) > 8:
            print(f"Route: {path[:120]}")

# Try Playwright approach
print("\n\nTrying Playwright...")
try:
    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as pw:
        browser = pw.chromium.launch(headless=True)
        page = browser.new_page()
        page.goto('https://www.xiangfuqu.gov.cn/kfsxfqwz/c00511/pc/list.html', wait_until='networkidle', timeout=30000)
        content = page.content()
        
        # Parse rendered content
        from bs4 import BeautifulSoup
        soup = BeautifulSoup(content, 'html.parser')
        
        # Find list items
        for a in soup.find_all('a', href=True):
            href = a.get('href', '')
            txt = a.get_text(strip=True)
            if len(txt) > 15 and 'content_' in href:
                print(f"  {txt[:60]} -> {href[:80]}")
        
        browser.close()
except ImportError:
    print("Playwright not available")
except Exception as e:
    print(f"Error: {e}")
