#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""qc_vacuum_batch.py —— 18 个真·空转逐个定性（第 1 批 10 个）"""
import json, os, re, sqlite3, ssl, subprocess, urllib.request
D = "/root/gov_crawler/"
d = json.load(open(D + "qc_out/qc_d5_recheck_20260925.json", encoding="utf-8"))
db = sqlite3.connect("file:/root/search.db?mode=ro", uri=True, timeout=30)
tgt = []
for s, n, h in d["no_data"]:
    r = db.execute("SELECT IFNULL(SUM(new_count),0) FROM run_logs WHERE script_name=? AND run_date>=date('now','-30 day')", (s,)).fetchone()
    if r[0] == 0: tgt.append((s, n, h))
print("真·空转 %d 个，本批 10 个" % len(tgt))
ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE
UA = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36"}
def probe(u):
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=10, context=ctx)
        return r.status, len(r.read(300000))
    except urllib.error.HTTPError as e: return e.code, 0
    except Exception as e: return type(e).__name__, 0
for s, n, h in tgt[:10]:
    p = D + s
    src = open(p, encoding="utf-8", errors="ignore").read() if os.path.exists(p) else ""
    us = [u for u in re.findall(r"https?://[^\s\"'<>)]+", src) if "{" not in u and "}" not in u]
    lu = ""
    for u in us:
        if re.search(r"list|index|tzgg|gsgg|hpsp|notice|tz", u, re.I): lu = u; break
    lu = lu or (us[0] if us else "")
    code, ln = probe(lu) if lu else ("NO-URL", 0)
    # 脚本输出里的"跳过"关键字
    skipw = re.findall(r"(Skipped|跳过|已存在|skip)", src)[:1]
    print("\n── %s | %s" % (s, n[:20]))
    print("   URL: %s\n   探测: %s 字节%s | 跳过关键字: %s" % (lu[:70], code, ln, skipw))
print("\n\n=== 实跑 4 个（看最后一行：跳过全跳 还是 报错）===")
for s, n, h in tgt[:4]:
    try:
        r = subprocess.run(["python3", D + s], capture_output=True, text=True, timeout=55, cwd=D)
        tail = [x for x in (r.stdout or "").strip().split("\n") if x.strip()][-2:]
        print("  %-28s EXIT=%d | %s" % (s[:28], r.returncode, " / ".join(x[:60] for x in tail)))
        if r.returncode != 0:
            print("      stderr: %s" % (r.stderr or "").strip().split("\n")[-1][:110])
    except subprocess.TimeoutExpired:
        print("  %-28s ⏱ 超过 55s（慢性慢脚本）" % s[:28])
