#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""register_jxst_blzt.py —— 注册 江西省生态环境厅-办理状态 (col42221)"""
import json
import os
import shutil

CFG = "/root/gov_crawler/daily_crawl_config.json"
BAK = "/root/gov_crawler/Archive/daily_crawl_config.json.bak_20260925_jxst"
E = dict(name="江西省生态环境厅-办理状态", script="crawl_jxsthjt_blzt.py", group="江西",
         args=["--pages=5"],
         note="江西厅 TRS jpage 动态列表: POST /queryList (form current/unitid=380055/"
              "webSiteCode=jxssthjt/channelCode=col42221/perPage/pageSize) → data.total=1323 / 15条每页; "
              "正文直接从接口 results[].source.content.content 取(含表格) 无需二次请求; "
              "⚠️ 必须 http:// (https 握手被拒 TLS alert); 派生自 crawl_jxsthjt_nslx.py")

cfg = json.load(open(CFG, encoding="utf-8"))
if any((c.get("script") or "") == E["script"] for c in cfg):
    print("⚠️ 已存在，跳过")
else:
    tmpl = next((c for c in cfg if (c.get("group") or "") == "江西"), cfg[-1])
    ent = dict(tmpl)
    ent.update({"name": E["name"], "display_name": E["name"], "script": E["script"],
                "args": E["args"], "group": E["group"], "note": E["note"],
                "incremental": True, "enabled": True, "sync_mode": "direct_server"})
    cfg.append(ent)
    os.makedirs(os.path.dirname(BAK), exist_ok=True)
    if not os.path.exists(BAK):
        shutil.copy2(CFG, BAK)
    with open(CFG, "w", encoding="utf-8") as f:
        json.dump(cfg, f, ensure_ascii=False, indent=2)
    print("✅ 已注册")
cfg2 = json.load(open(CFG, encoding="utf-8"))
print("条目数:", len(cfg2))
idx = [i + 1 for i, c in enumerate(cfg2) if (c.get("script") or "") == E["script"]]
print("该脚本配置编号:", idx)
missing = [c.get("script") for c in cfg2 if c.get("script")
           and not os.path.exists("/root/gov_crawler/" + c["script"])]
print("config 中 script 磁盘缺失:", len(missing), missing[:5])
