#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""probe_jxth_cols.py —— 用候选栏目号打 api-ajax_list，找环评栏目"""
import json, ssl, urllib.request, urllib.parse
ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE
B = "https://www.jxth.gov.cn"
H = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/141.0.0.0 Safari/537.36",
     "X-Requested-With": "XMLHttpRequest",
     "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
     "Referer": B + "/"}
def try_col(cid):
    body = [("ajax_type[]", "9_xxgk"), ("ajax_type[]", str(cid)), ("ajax_type[]", "9"),
            ("ajax_type[]", "xxgk"), ("ajax_type[]", "Y-m-d"), ("ajax_type[]", "50"),
            ("ajax_type[]", "20"), ("ajax_type[]", "is_top DESC"),
            ("ajax_type[]", "displayorder DESC"), ("ajax_type[]", "inputtime DESC"),
            ("ajax_type[]", ""), ("is_ds", "1")]
    data = urllib.parse.urlencode(body).encode()
    try:
        r = urllib.request.urlopen(urllib.request.Request(B + "/api-ajax_list-1.html", data=data, headers=H), timeout=20, context=ctx)
        t = r.read(200000).decode("utf-8", "ignore")
        try:
            j = json.loads(t)
            n = len(j.get("data") or j.get("list") or []) if isinstance(j, dict) else (len(j) if isinstance(j, list) else 0)
            first = ""
            if isinstance(j, dict):
                arr = j.get("data") or j.get("list") or []
                if isinstance(arr, list) and arr:
                    first = str(arr[0].get("title") or arr[0].get("name") or arr[0])[:60]
            return "HTTP %s len=%d 解析=✅ 条数=%s | %s" % (r.status, len(t), n, first)
        except Exception:
            return "HTTP %s len=%d 非JSON | %s" % (r.status, len(t), t[:70].replace("\n", " "))
    except Exception as e:
        return "ERR %s" % type(e).__name__
for cid in (167445, 165834, 105532, 105535, 165833, 165835):
    print("  栏目号 %-7s → %s" % (cid, try_col(cid)[:130]))
