#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""adapt_jxst_blzt.py —— 从 crawl_jxsthjt_nslx.py 派生 crawl_jxsthjt_blzt.py (col42221 办理状态)"""
import ast
import io
import os
import re
import shutil

D = "/root/gov_crawler/"
SRC, DST = "crawl_jxsthjt_nslx.py", "crawl_jxsthjt_blzt.py"
s = io.open(D + SRC, encoding="utf-8").read()

reps = [
    ("江西省生态环境厅 - 拟受理项目公示", "江西省生态环境厅 - 办理状态"),
    ("col42169", "col42221"),
    ('SITE_NAME = "江西省生态环境厅-拟受理项目公示"', 'SITE_NAME = "江西省生态环境厅-办理状态"'),
    ('SCRIPT_NAME = "crawl_jxsthjt_nslx.py"', 'SCRIPT_NAME = "crawl_jxsthjt_blzt.py"'),
    ('CHANNEL_CODE = "col42169"', 'CHANNEL_CODE = "col42221"'),
    ("响应 data.total=338 条", "响应 data.total=1323 条"),
]
n = 0
for a, b in reps:
    if a in s:
        cnt = s.count(a)
        s = s.replace(a, b)
        n += cnt
print("替换 %d 处" % n)
print("剩余 col42169 出现次数:", s.count("col42169"))
try:
    ast.parse(s)
except SyntaxError as e:
    raise SystemExit("❌ 语法错误: %s" % e)
if os.path.exists(D + DST):
    shutil.copy2(D + DST, D + "Archive/%s.bak_20260925" % DST)
io.open(D + DST, "w", encoding="utf-8").write(s)
print("✅ 已生成", DST)

# 库路径核查（⚠️脚本默认 /mnt/data/search.db，须确认与 /root/search.db 是同一份）
print("\n=== 库路径核查 ===")
for p in ["/mnt/data/search.db", "/root/search.db"]:
    if os.path.exists(p):
        st = os.stat(p)
        real = os.path.realpath(p)
        print("  %-24s 存在 %12d 字节 inode=%s realpath=%s" % (p, st.st_size, st.st_ino, real))
    else:
        print("  %-24s ❌ 不存在" % p)
import subprocess
print("  挂载:", subprocess.run(["df", "-h", "/mnt/data", "/root"], capture_output=True).stdout.decode()[:300].replace("\n", " | "))

# 冒烟
print("\n=== 冒烟：--pages=1 ===")
r = subprocess.run(["python3", D + DST, "--pages=1"], capture_output=True, timeout=300, cwd=D)
out = (r.stdout + r.stderr).decode("utf-8", "ignore")
print(out[-1200:])
