#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""fix_zhcqhj_gsl.py —— 补修兄弟脚本的 http->https"""
import ast, os, shutil, subprocess
D = "/root/gov_crawler/"; BAK = D + "Archive/"
f = "crawl_zhcqhj_gsl.py"; dom = "www.zhcqhj.com"
p = D + f
src = open(p, encoding="utf-8").read()
n = src.count("http://" + dom)
lines = src.split("\n")
# 只改代码行（非注释）
out = []
for ln in lines:
    if "http://" + dom in ln and not ln.strip().startswith("#"):
        ln = ln.replace("http://" + dom, "https://" + dom)
    out.append(ln)
s2 = "\n".join(out)
c2 = sum(1 for a, b in zip(src.split("\n"), s2.split("\n")) if a != b)
try: ast.parse(s2)
except SyntaxError as e: raise SystemExit("语法错: %s" % e)
if not os.path.exists(BAK + f + ".bak_20260925_https"): shutil.copy2(p, BAK + f + ".bak_20260925_https")
open(p, "w", encoding="utf-8").write(s2)
print("✅ %s：共 %d 处 http://%s，改了 %d 行代码（注释保留）" % (f, n, dom, c2))
r = subprocess.run(["python3", p], capture_output=True, text=True, timeout=150, cwd=D)
outs = [x for x in (r.stdout or "").strip().split("\n") if x.strip()][-3:]
errs = [x for x in (r.stderr or "").strip().split("\n") if x.strip()][-2:]
print("实跑 EXIT=%d" % r.returncode)
for x in outs: print("   out:", x[:110])
for x in errs: print("   ERR:", x[:110])
