#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""fix_zhcqhj_gsl_re.py —— 列表正则兼容 http/https"""
import ast, os, shutil, subprocess, sqlite3, re
D = "/root/gov_crawler/"; f = "crawl_zhcqhj_gsl.py"; p = D + f
src = open(p, encoding="utf-8").read()
OLD = r"""m = re.match(r'http://www\.zhcqhj\.com/html/news/gsl/(\d+)\.html$', href)"""
NEW = r"""m = re.match(r'https?://(?:www\.)?zhcqhj\.com/html/news/gsl/(\d+)\.html$', href)   # QC20260926 兼容 https"""
if OLD not in src:
    print("⚠️ 原样锚点未命中，当前 L50 实际内容：")
    for i, ln in enumerate(src.split("\n"), 1):
        if "re.match" in ln: print("   L%d %s" % (i, ln.strip()[:130]))
    raise SystemExit
src = src.replace(OLD, NEW, 1)
try: ast.parse(src)
except SyntaxError as e: raise SystemExit("语法错（未落盘）: %s" % e)
b = D + "Archive/" + f + ".bak_20260926_re_https"
if not os.path.exists(b): shutil.copy2(p, b)
open(p, "w", encoding="utf-8").write(src)
print("✅ 正则已改为 https?://(?:www\.)? 且已落盘（备份 %s）" % os.path.basename(b))
db = sqlite3.connect("file:/root/search.db?mode=ro", uri=True, timeout=30)
n0 = db.execute("SELECT COUNT(*) FROM gov_raw WHERE page_url LIKE '%zhcqhj%'").fetchone()[0]
print("跑前行数: %d" % n0)
try:
    r = subprocess.run(["python3", p], capture_output=True, text=True, timeout=150, cwd=D)
    print("EXIT=%d" % r.returncode)
    for x in [y for y in (r.stdout or "").split("\n") if y.strip()][:8]:
        print("   out:", x[:105])
except subprocess.TimeoutExpired:
    print("⏱ >150s（在正常抓详情）")
db2 = sqlite3.connect("file:/root/search.db?mode=ro", uri=True, timeout=30)
n1 = db2.execute("SELECT COUNT(*) FROM gov_raw WHERE page_url LIKE '%zhcqhj%'").fetchone()[0]
print("跑后行数: %d (%s)" % (n1, "✅+%d" % (n1 - n0) if n1 > n0 else "未变"))
