#!/usr/bin/env python3
"""Debug col1384 and col4795 data formats."""
import requests, re
from urllib.parse import quote

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
BASE = "http://www.jxlc.gov.cn"

r = requests.get("http://www.jxlc.gov.cn/col/col1384/index.html",
                 timeout=30, headers=headers, verify=False)
html = r.text

# Get the recordset content
m = re.search(r'70893.*?<recordset>(.*?)</recordset>', html, re.DOTALL)
if m:
    content = m.group(1)
    print(f"Recordset ({len(content)} chars):")
    # Show raw content first
    print(content[:500])
    print("\n---")
    # Try different regex for articles
    arts = re.findall(r'<a[^>]*href="([^"]+)"[^>]*title="([^"]+)"', content)
    print(f"\nLink+title pairs: {len(arts)}")
    for a, t in arts[:5]:
        print(f"  {t[:45]:47s} -> {a}")
    
    # Look for date patterns
    for m2 in re.finditer(r'(\d{4}-\d{2}-\d{2})', content):
        ctx = content[max(0,m2.start()-20):m2.end()+20]
        print(f"  Date: {m2.group(1)} context: {ctx.strip()}")

# Now try the dataproxy again with proper URL encoding
from urllib.parse import unquote
next_url = "/module/web/jpage/dataproxy.jsp?page=1&appid=1&appid=1&webid=5&path=/&columnid=1384&unitid=70893&webname=%25E4%25B8%25B4%25E5%25B7%259D%25E5%258C%25BA%25E4%25BA%25BA%25E6%25B0%2591%25E6%2594%25BF%25E5%25BA%259C&permissiontype=0"
# Double URL decode (webname is double-encoded)
decoded = unquote(unquote(next_url))
print(f"\nDecoded URL: {decoded}")
r2 = requests.get(BASE + decoded, timeout=30, headers=headers, verify=False)
r2.encoding = 'utf-8'
print(f"Direct decoded GET: {len(r2.text)} bytes")
if len(r2.text) > 100:
    print(r2.text[:500])

# Also try with the webname as a POST parameter
webname_decoded = unquote("%25E4%25B8%25B4%25E5%25B7%259D%25E5%258C%25BA%25E4%25BA%25BA%25E6%25B0%2591%25E6%2594%25BF%25E5%25BA%259C")
r3 = requests.post(BASE + "/module/web/jpage/dataproxy.jsp", data={
    "page": "1", "appid": "1", "webid": "5", "path": "/",
    "columnid": "1384", "unitid": "70893",
    "webname": webname_decoded, "permissiontype": "0"
}, headers=headers, timeout=30, verify=False)
r3.encoding = 'utf-8'
print(f"\nPOST with webname: {len(r3.text)} bytes")
if len(r3.text) > 100:
    print(r3.text[:500])

# And try with the raw double-encoded webname
r4 = requests.post(BASE + "/module/web/jpage/dataproxy.jsp", data={
    "page": "1", "appid": "1", "webid": "5", "path": "/",
    "columnid": "1384", "unitid": "70893",
    "webname": "%25E4%25B8%25B4%25E5%25B7%259D%25E5%258C%25BA%25E4%25BA%25BA%25E6%25B0%2591%25E6%2594%25BF%25E5%25BA%259C",
    "permissiontype": "0"
}, headers=headers, timeout=30, verify=False)
r4.encoding = 'utf-8'
print(f"POST with encoded webname: {len(r4.text)} bytes")
if len(r4.text) > 100:
    print(r4.text[:500])
