#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""patch_jinyun2.py —— 缙云支持双 URL 格式（新 /col/colNNN/art/… + 旧 /art/Y/M/D/art_<col>_N.html）"""
import ast
import io
import os
import shutil
import sys

P = "/root/gov_crawler/crawl_jinyun_hjgs.py"
BAK = "/root/gov_crawler/Archive/crawl_jinyun_hjgs.py.bak_20260924_dualurl"
src = io.open(P, encoding="utf-8").read()

old = '''        if COL_PATH not in href or not href.lower().endswith(".html"):
            continue'''
new = '''        # ⚠️ 双 URL 格式（洞头同款）：新版 /col/colNNN/art/YYYY/art_HEX.html
        #    旧版 /art/YYYY/M/D/art_<colId>_<num>.html —— 路径里带本栏目 ID，同属本栏目
        if not href.lower().endswith(".html"):
            continue
        if COL_PATH not in href and ("art_%s_" % _col) not in href:
            continue'''
n = src.count(old)
if n != 1:
    print("❌ 锚点命中 %d 次" % n)
    sys.exit(1)
src = src.replace(old, new)
try:
    ast.parse(src)
except SyntaxError as e:
    print("❌ 语法错误:", e)
    sys.exit(1)
os.makedirs(os.path.dirname(BAK), exist_ok=True)
if not os.path.exists(BAK):
    shutil.copy2(P, BAK)
io.open(P, "w", encoding="utf-8").write(src)
print("✅ 已支持双 URL 格式（备份 %s）" % os.path.basename(BAK))
