#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""fix_https_7.py —— 按域名精确替换 http->https（7 条 URL / 8 个脚本）"""
import ast, os, shutil, datetime
D = "/root/gov_crawler/"; BAK = D + "Archive/"
CASES = [
    ("crawl_anlu.py", "www.anlu.gov.cn"),
    ("crawl_jxdy_hjypj.py", "www.jxdy.gov.cn"),
    ("crawl_jxln.py", "www.jxln.gov.cn"),
    ("crawl_shaowu.py", "www.shaowu.gov.cn"),
    ("crawl_xunwu.py", "www.xunwu.gov.cn"),
    ("crawl_xunwu_tzgg.py", "www.xunwu.gov.cn"),
    ("crawl_yantai.py", "www.yantai.gov.cn"),
    ("crawl_zhcqhj.py", "www.zhcqhj.com"),
]
for f, dom in CASES:
    p = D + f
    if not os.path.exists(p):
        print("❌ 缺文件 %s" % f); continue
    src = open(p, encoding="utf-8").read()
    n = src.count("http://" + dom)
    if n == 0:
        print("  ⚠️ %s 无 http://%s（已改过？）" % (f, dom)); continue
    s2 = src.replace("http://" + dom, "https://" + dom)
    try: ast.parse(s2)
    except SyntaxError as e: print("  ❌ %s 语法错 %s" % (f, e)); continue
    os.makedirs(BAK, exist_ok=True)
    b = BAK + f + ".bak_20260925_https"
    if not os.path.exists(b): shutil.copy2(p, b)
    open(p, "w", encoding="utf-8").write(s2)
    print("  ✅ %-26s 替换 %d 处 http->https" % (f, n))
