#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""扫描：哪些脚本手动 INSERT 进 gov_search（与 AFTER INSERT 触发器重复 → IntegrityError
→ 连带 gov_raw 那条被回滚 = 静默丢数据）。同时修复 crawl_xiantao.py。"""
import io
import os
import re
import shutil
import sys

D = "/root/gov_crawler"
pat = re.compile(r"INSERT\s+(?:OR\s+REPLACE\s+)?INTO\s+gov_search", re.I)

print("=" * 92)
print("扫描：手动写 gov_search 的脚本")
print("=" * 92)
hits = []
for fn in sorted(os.listdir(D)):
    if not fn.endswith(".py"):
        continue
    try:
        src = io.open(os.path.join(D, fn), encoding="utf-8", errors="ignore").read()
    except Exception:
        continue
    if pat.search(src):
        n = len(pat.findall(src))
        hits.append((fn, n))
print("命中 %d 个脚本：" % len(hits))
for fn, n in hits:
    print("   %-48s %d 处" % (fn, n))

# ── 修复 crawl_xiantao.py ─────────────────────────────────────────────
print()
print("=" * 92)
print("修复 crawl_xiantao.py：移除手动 gov_search 插入")
print("=" * 92)
fn = "crawl_xiantao.py"
p = os.path.join(D, fn)
src = io.open(p, encoding="utf-8").read()
old = '''                rid = cur.lastrowid
                c.execute("INSERT INTO gov_search (rowid, title, site_name, summary) VALUES (?,?,?,?)",
                    (rid, title, SITE, summary))
                conn.commit()'''
new = '''                # 2026-09-22 修复：删掉这里手动的 gov_search 插入。
                #   库上已有 AFTER INSERT 触发器 trg_gov_raw_fts_ins 自动维护 FTS，
                #   手动再插同一 rowid 会 IntegrityError → 连带上面那条 gov_raw 一起回滚
                #   → 被 except IntegrityError 记成「重复」，表现为「每天成功但 new=0」。
                conn.commit()'''
if src.count(old) == 1:
    src = src.replace(old, new)
    import ast
    ast.parse(src)
    os.makedirs(os.path.join(D, "Archive"), exist_ok=True)
    bak = os.path.join(D, "Archive", fn + ".bak_20260922_ftsdup")
    if not os.path.exists(bak):
        shutil.copy2(p, bak)
    io.open(p, "w", encoding="utf-8").write(src)
    print("  ✅ 已修复（备份 Archive/%s.bak_20260922_ftsdup）" % fn)
else:
    print("  ⚠️ 锚点命中 %d 次，未改动" % src.count(old))
