#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""probe_heshan.py —— heshan 容错取证"""
import json, re, sqlite3
D = "/root/gov_crawler/"; f = "crawl_heshan_gkmlpt.py"
cfg = json.load(open(D + "daily_crawl_config.json", encoding="utf-8"))
lst = cfg if isinstance(cfg, list) else cfg.get("tasks", cfg.get("scripts", []))
ent = [c for c in lst if c.get("script") == f]
print("config 条目:", json.dumps(ent[0], ensure_ascii=False) if ent else "（不在 config）")
print("\n--- 调度器 timeout 相关 ---")
sched = open(D + "crawl_scheduler_v2.py", encoding="utf-8").read()
for i, ln in enumerate(sched.split("\n"), 1):
    if re.search(r"task_timeout|SCRIPT_TIMEOUT\s*=|\"timeout\"|get\(\"timeout", ln):
        print("  L%-4d %s" % (i, ln.strip()[:110]))
print("\n--- heshan 抓取/重试代码 ---")
src = open(D + f, encoding="utf-8", errors="ignore").read()
for i, ln in enumerate(src.split("\n"), 1):
    if re.search(r"def fetch|requests\.|retry|for .*range\(|time\.sleep|timeout=|raise|except", ln):
        print("  L%-4d %s" % (i, ln.strip()[:108]))
print("\n--- 最近失败记录原文 ---")
c = sqlite3.connect("file:/root/search.db?mode=ro", uri=True, timeout=30)
for d, st, el, er in c.execute("SELECT run_date, status, elapsed_seconds, substr(IFNULL(error_detail,''),1,150) FROM run_logs WHERE script_name=? AND status!='成功' ORDER BY run_date DESC LIMIT 5", (f,)):
    print("  %s %-10s %5.0fs | %s" % (d, st, el or 0, (er or "").replace(chr(10), " ")))
