#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""用 crawl_xiantao.py 的真实数据链路复现插入，打印原始异常。"""
import importlib.util
import os
import re
import sqlite3
import sys
import traceback

D = "/root/gov_crawler"
os.chdir(D)
sys.path.insert(0, D)

spec = importlib.util.spec_from_file_location("xt", os.path.join(D, "crawl_xiantao.py"))
m = importlib.util.module_from_spec(spec)
try:
    spec.loader.exec_module(m)
except SystemExit:
    pass

items = m.crawl_page(1)
print("列表条目 %d" % len(items))
it = None
for x in items:
    # 取一个不在库的
    c0 = sqlite3.connect("/root/search.db", timeout=60)
    if not c0.execute("SELECT 1 FROM gov_raw WHERE page_url=?", (x["url"],)).fetchone():
        it = x
        break
print("选中的新条目: %s" % it["url"])
print()

title, date, content = m.extract_detail(it["url"])
if not title:
    title = it["title"]
if not date:
    date = it["date"]
print("title   = %r" % title[:60])
print("date    = %r" % date)
print("content 长度 = %d" % (len(content) if content else 0))
summary = re.sub(r"<[^>]+>", "", content or "")[:200]
summary = re.sub(r"\s+", " ", summary).strip()
print("summary = %r" % summary[:80])
print()

c = sqlite3.connect("/root/search.db", timeout=60)
c.execute("PRAGMA journal_mode=WAL")
cur = c.cursor()
print("=== 直接执行脚本那条 INSERT（用 SAVEPOINT，不污染库）===")
cur.execute("SAVEPOINT xt")
try:
    r = cur.execute(
        "INSERT INTO gov_raw (title, summary, content, page_url, source_url, publish_date, site_name, script_name, group_name) VALUES (?,?,?,?,?,?,?,?,?)",
        (title, summary, content, it["url"], it["url"], date, m.SITE, m.SCRIPT, m.GROUP))
    rid = r.lastrowid
    print("  ✅ gov_raw 插入成功 id=%s" % rid)
    try:
        cur.execute("INSERT INTO gov_search (rowid, title, site_name, summary) VALUES (?,?,?,?)",
                    (rid, title, m.SITE, summary))
        print("  ✅ gov_search 插入成功")
    except Exception as e2:
        print("  ❌ gov_search 插入失败 → %s: %s" % (type(e2).__name__, e2))
except Exception as e:
    print("  ❌ gov_raw 插入失败 → %s: %s" % (type(e).__name__, e))
    print()
    traceback.print_exc()
finally:
    cur.execute("ROLLBACK TO xt")
    cur.execute("RELEASE xt")
    c.commit()
c.close()
