#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""probe_jxst_api.py —— 用已有脚本的接口方式测 col42221"""
import json
import re
import subprocess

import requests

requests.packages.urllib3.disable_warnings()
BASE = "http://sthjt.jiangxi.gov.cn"
UA = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
      "(KHTML, like Gecko) Chrome/124.0 Safari/537.36")

# 先看已有脚本怎么拼 POST data
src = open("/root/gov_crawler/crawl_jxsthjt_nslx.py", encoding="utf-8").read()
i = src.find("def fetch")
print("=== 已有脚本的请求构造 ===")
for m in re.finditer(r"(?m)^\s*(data\s*=|payload\s*=|params\s*=|HEADERS\s*=).{0,200}", src):
    print("  ", re.sub(r"\s+", " ", m.group(0))[:190])
print()
m = re.search(r"(?s)def fetch[^\n]*\n(.{0,700})", src)
if m:
    print("=== fetch() 片段 ===")
    print(m.group(1)[:700])
print()

for code in ["col42221", "col42169"]:
    print("=" * 96)
    print("【channelCode=%s】" % code)
    for unitid in ["380055", ""]:
        data = {"current": 1, "unitid": unitid, "webSiteCode": "jxssthjt",
                "channelCode": code, "perPage": 20, "pageSize": 20}
        hdr = {"User-Agent": UA, "Referer": "%s/jxssthjt/col/%s/index.html" % (BASE, code),
               "X-Requested-With": "XMLHttpRequest", "Accept": "application/json, text/plain, */*"}
        try:
            r = requests.post(BASE + "/queryList", data=data, headers=hdr, timeout=30, verify=False)
            txt = r.text
            print("  unitid=%-8s → HTTP %s | %d 字节 | %s" % (unitid or "(空)", r.status_code, len(txt), txt[:150].replace("\n", " ")))
            try:
                d = r.json()
                keys = list(d.keys()) if isinstance(d, dict) else "(list)"
                print("     JSON keys:", keys)
                arr = None
                for k in ("data", "rows", "list", "result", "records"):
                    v = d.get(k) if isinstance(d, dict) else None
                    if isinstance(v, list):
                        arr = v; break
                    if isinstance(v, dict):
                        for k2 in ("list", "rows", "records", "data"):
                            if isinstance(v.get(k2), list):
                                arr = v[k2]; break
                    if arr:
                        break
                if arr:
                    print("     条目数:", len(arr))
                    for it in arr[:5]:
                        t = it.get("title") or it.get("name") or it.get("docTitle") or ""
                        dt = it.get("publishDate") or it.get("date") or it.get("pubDate") or ""
                        print("        %-12s %s" % (str(dt)[:10], str(t)[:60]))
                else:
                    print("     (未找到数组) 片段:", json.dumps(d, ensure_ascii=False)[:300])
            except Exception as e:
                print("     非 JSON:", str(e)[:50])
        except Exception as e:
            print("  unitid=%-8s → ❌ %s" % (unitid or "(空)", str(e)[:80]))
