#!/usr/bin/env python3
"""Test jpage dataproxy API for col1384 and col1611."""
import requests, re

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}

BASE = "http://www.jxlc.gov.cn"

# Get the page HTML to find the colId/unitId mapping for jpage
pages = [
    ("col1384", "http://www.jxlc.gov.cn/col/col1384/index.html"),
    ("col1611", "http://www.jxlc.gov.cn/col/col1611/index.html"),
    ("col4795", "https://www.jxlc.gov.cn/col/col4795/index.html?number=D00004D00004D00007"),
]

for name, url in pages:
    print(f"\n=== {name} ===")
    r = requests.get(url, timeout=30, headers=headers, verify=False)
    html = r.text
    
    # Find all param objects
    for m in re.finditer(r'param_(\d+)\s*=\s*\{([^}]+)\}', html):
        params = m.group(2)
        # Extract unitid
        uid = re.search(r"unitid['\"]?\s*:\s*['\"]([^'\"]+)['\"]", params)
        colid = re.search(r"columnid['\"]?\s*:\s*['\"]?(\d+)", params)
        sct = re.search(r"sourceContentType['\"]?\s*:\s*(\d+)", params)
        webid = re.search(r"webid['\"]?\s*:\s*(\d+)", params)
        print(f"  param_{m.group(1)}: col={colid.group(1) if colid else '?'}, webid={webid.group(1) if webid else '?'}, unitid={uid.group(1) if uid else '?'}, sourceContentType={sct.group(1) if sct else '?'}")
    
    # Find jpage calls
    for m in re.finditer(r"jpage\(\{([^}]+)\}\)", html):
        print(f"  jpage: {m.group(1)[:200]}")
