#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""qc_disabled_recheck.py —— 复检「因站点故障而禁用」的 4 条：现在恢复了吗"""
import json, os, re, socket, ssl, urllib.request
D = "/root/gov_crawler/"
d = json.load(open(D + "qc_out/qc_disabled.json", encoding="utf-8"))
targets = [x for x in d if re.search(r"NXDOMAIN|站点下线|502|故障", x.get("note") or "")]
print("复检目标: %d 条" % len(targets))
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 Chrome/141.0.0.0 Safari/537.36"}
for x in targets:
    f = x["script"]; p = D + f
    src = open(p, encoding="utf-8", errors="ignore").read()[:80000] if os.path.exists(p) else ""
    us = [u for u in re.findall(r"https?://[^\s\"'<>(){}]+", src) if "{" not in u]
    host = ""
    if us:
        host = us[0].split("/")[2]
    print("\n── %s | %s" % (f, x["name"][:22]))
    print("   脚本 URL 主机: %s" % host)
    try:
        ip = socket.gethostbyname(host)
        print("   DNS 解析: ✅ %s" % ip)
    except Exception as e:
        print("   DNS 解析: ❌ %s" % type(e).__name__)
    for u in us[:2]:
        try:
            r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=10, context=ctx)
            b = r.read(60000)
            print("   实测 %-46s → HTTP %s / %d 字节 %s" % (u[:46], r.status, len(b), "✅可能已恢复" if len(b) > 3000 else "⚠️内容过小"))
        except urllib.error.HTTPError as e:
            print("   实测 %-46s → HTTP %s" % (u[:46], e.code))
        except Exception as e:
            print("   实测 %-46s → %s" % (u[:46], type(e).__name__))
