#!/usr/bin/env python3
"""Analyze jxlc.gov.cn sub-columns to find search API parameters."""
import requests, re, json

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

urls_info = [
    ("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"=== {name} ===")
    r = requests.get(url, timeout=30, headers=headers, verify=False)
    html = r.text
    
    # Find infotypeId
    for m in re.finditer(r'infotypeId\s*[=:]\s*["\']([^"\']+)["\']', html):
        print(f"  infotypeId = {m.group(1)}")
    
    # Find number parameter
    for m in re.finditer(r'number\s*[=:]\s*["\']([^"\']+)["\']', html):
        v = m.group(1)
        if len(v) > 3 and "D" in v:
            print(f"  number = {v}")

    # Find divid
    for m in re.finditer(r'divid\s*[=:]\s*["\']([^"\']+)["\']', html):
        print(f"  divid = {m.group(1)}")
    
    # Find area
    for m in re.finditer(r'area\s*[=:]\s*["\']([^"\']+)["\']', html):
        print(f"  area = {m.group(1)}")
    
    # Find jdid
    for m in re.finditer(r'jdid\s*[=:]\s*["\']?(\d+)["\']?', html):
        print(f"  jdid = {m.group(1)}")

    # Find currpage
    for m in re.finditer(r'currpage', html):
        idx = m.start()
        print(f"  currpage found at...{html[max(0,idx-30):idx+30]}")

    # Find the page title
    for m in re.finditer(r'<title>([^<]+)</title>', html):
        print(f"  title = {m.group(1)}")

    # look for AJAX calls
    for m in re.finditer(r'(ajax|\.load\(|\.post\(|getJSON|fetch\()', html, re.I):
        idx = m.start()
        print(f"  AJAX at...{html[max(0,idx-30):idx+80]}")
    
    print()
