#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""复验：6 个 A 类脚本修后重跑，看库内是否真的增加。"""
import sqlite3
import subprocess

D = "/root/gov_crawler"
CASES = ["crawl_ningguo_sthj.py", "crawl_hld_tzgg.py", "crawl_changji.py",
         "crawl_hky.py", "crawl_kecheng.py", "crawl_lanshantunhe.py"]

c = sqlite3.connect("/root/search.db", timeout=180)
c.execute("PRAGMA busy_timeout=180000")


def stat(fn):
    r = c.execute("SELECT COUNT(*), MAX(publish_date) FROM gov_raw WHERE script_name=?", (fn,)).fetchone()
    return r[0], (r[1] or "-")


for fn in CASES:
    b_n, b_d = stat(fn)
    tail = ""
    try:
        p = subprocess.run(["python3", fn], cwd=D, capture_output=True, text=True, timeout=180)
        lines = [l for l in ((p.stdout or "") + (p.stderr or "")).split("\n") if l.strip()]
        tail = lines[-1] if lines else ""
    except subprocess.TimeoutExpired:
        tail = "(超时)"
    a_n, a_d = stat(fn)
    flag = "✅ 修复生效" if a_n > b_n else "❌ 仍为 0"
    print("%-28s %5d -> %5d (%+d)   最新 %s -> %s  %s" % (fn, b_n, a_n, a_n - b_n, b_d, a_d, flag))
    print("      脚本末行: %s" % tail[:90])
c.close()
