#!/usr/bin/env python3
"""Extract jpage ajaxParam and search.jsp parameters from jxlc pages."""
import requests, re, html as html_mod

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

urls_info = [
    ("col1559_现有", "https://www.jxlc.gov.cn/col/col1559/index.html?number=D00002D00004"),
    ("col4795", "https://www.jxlc.gov.cn/col/col4795/index.html?number=D00004D00004D00007"),
    ("col1611", "http://www.jxlc.gov.cn/col/col1611/index.html"),
    ("col1432", "http://www.jxlc.gov.cn/col/col1432/"),
    ("col1384", "http://www.jxlc.gov.cn/col/col1384/index.html"),
]

for name, url in urls_info:
    print(f"\n{'='*60}")
    print(f"=== {name} ===")
    r = requests.get(url, timeout=30, headers=headers, verify=False)
    html = r.text
    print(f"URL: {url}")
    print(f"Length: {len(html)}")

    # Find all param_XXXXX assignments
    for m in re.finditer(r'param_(\d+)\s*=\s*{', html):
        start = m.start()
        # extract the entire JSON-like object
        depth = 0
        obj_start = m.end() - 1
        i = obj_start
        while i < len(html):
            if html[i] == '{':
                depth += 1
            elif html[i] == '}':
                depth -= 1
                if depth == 0:
                    obj_str = html[m.start():i+1]
                    # decode HTML entities
                    obj_str = html_mod.unescape(obj_str)
                    print(f"\n  param_{m.group(1)}:")
                    print(f"    {obj_str[:300]}")
                    break
            i += 1

    # Find form data / search params
    print("\n  --- search.jsp params ---")
    for m in re.finditer(r'(\w+)\s*[:=]\s*["\']([^"\']+)["\']', html):
        k, v = m.group(1), m.group(2)
        if k in ('infotypeId', 'jdid', 'divid', 'area', 'currpage', 'vc_title', 'vc_number', 'texttype'):
            print(f"    {k} = {v}")

    # Find inline search URL formation
    for m in re.finditer(r'(?:url|searchUrl|urlPath)\s*[:=]\s*["\']([^"\']+)["\']', html, re.I):
        v = m.group(1)
        if 'search' in v.lower():
            print(f"    searchUrl = {v}")
