#!/usr/bin/env python3
"""Test jxlc search.jsp for col4795 and col1432 with correct https."""
import requests, re

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    "X-Requested-With": "XMLHttpRequest",
    "Referer": "https://www.jxlc.gov.cn/col/col4795/index.html?number=D00004D00004D00007",
    "Content-Type": "application/x-www-form-urlencoded"
}

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

# col4795 with D00004D00004
print("=== col4795: infotypeId=D00004D00004 ===")
data = "infotypeId=D00004D00004&jdid=5&area=&divid=div1432&vc_title=&vc_number=&currpage=1&vc_filenumber=&vc_all=&texttype=&fbtime="
r = requests.post(BASE + "/module/xxgk/search.jsp", data=data, headers=headers, timeout=30, verify=False)
r.encoding = "utf-8"
nTotal = re.search(r'nTotalCount.*?value="(\d+)"', r.text)
print("Length:", len(r.text), "nTotalCount:", nTotal.group(1) if nTotal else "NOT FOUND")
items = re.findall(r"href='([^']+)'[^>]*title=\"([^\"]+)\".*?<b>(.*?)</b>", r.text, re.DOTALL)
print(f"Items: {len(items)}")
for href, title, d in items[:3]:
    url = href if href.startswith("http") else BASE + href
    print(f"  {title[:40]:42s} {d.strip():15s} -> {url}")

# col1432 with D00004D00003
print("\n=== col1432: infotypeId=D00004D00003 ===")
data2 = "infotypeId=D00004D00003&jdid=5&area=&divid=div1432&vc_title=&vc_number=&currpage=1&vc_filenumber=&vc_all=&texttype=&fbtime="
headers2 = dict(headers)
headers2["Referer"] = "http://www.jxlc.gov.cn/col/col1432/"
r2 = requests.post(BASE + "/module/xxgk/search.jsp", data=data2, headers=headers2, timeout=30, verify=False)
r2.encoding = "utf-8"
nTotal2 = re.search(r'nTotalCount.*?value="(\d+)"', r2.text)
print("Length:", len(r2.text), "nTotalCount:", nTotal2.group(1) if nTotal2 else "NOT FOUND")
items2 = re.findall(r"href='([^']+)'[^>]*title=\"([^\"]+)\".*?<b>(.*?)</b>", r2.text, re.DOTALL)
print(f"Items: {len(items2)}")
for href, title, d in items2[:3]:
    url = href if href.startswith("http") else BASE + href
    print(f"  {title[:40]:42s} {d.strip():15s} -> {url}")

# Also test the original col1559 as baseline
print("\n=== col1559 (baseline): infotypeId=D00002D00004 ===")
data3 = "infotypeId=D00002D00004&jdid=5&area=&divid=div1432&vc_title=&vc_number=&currpage=1&vc_filenumber=&vc_all=&texttype=&fbtime="
headers3 = dict(headers)
headers3["Referer"] = "https://www.jxlc.gov.cn/col/col1559/index.html?number=D00002D00004"
r3 = requests.post(BASE + "/module/xxgk/search.jsp", data=data3, headers=headers3, timeout=30, verify=False)
r3.encoding = "utf-8"
nTotal3 = re.search(r'nTotalCount.*?value="(\d+)"', r3.text)
print("Length:", len(r3.text), "nTotalCount:", nTotal3.group(1) if nTotal3 else "NOT FOUND")
items3 = re.findall(r"href='([^']+)'[^>]*title=\"([^\"]+)\".*?<b>(.*?)</b>", r3.text, re.DOTALL)
print(f"Items: {len(items3)}")
