#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""verify_jcx_fts.py —— 第 9 步 FTS 验证（两条路 + 附件真实下载）"""
import json
import re
import sqlite3
import urllib.parse
import urllib.request

BASE = "http://127.0.0.1:8000"
SITE = "江城县人民政府-通知公告"
WORDS = ["小额信贷", "殡仪馆", "职业技能培训"]

db = sqlite3.connect("/root/search.db", timeout=20)
db.execute("PRAGMA busy_timeout=20000")

print("=== ① DB 层 gov_search MATCH ===")
for w in WORDS:
    try:
        rows = list(db.execute(
            "SELECT g.rowid, g.title FROM gov_search g JOIN gov_raw r ON r.id=g.rowid "
            "WHERE gov_search MATCH ? AND r.site_name=? LIMIT 3", (w, SITE)))
        print("  MATCH %-10s → %d 命中 %s" % (w, len(rows), [x[1][:26] for x in rows]))
    except Exception as e:
        print("  MATCH %-10s → ❌ %s" % (w, str(e)[:60]))

n = db.execute("SELECT COUNT(*) FROM gov_bigram WHERE rowid IN (SELECT id FROM gov_raw WHERE site_name=?)", (SITE,)).fetchone()[0]
print("\n=== ④ gov_bigram 预计算索引 ===")
print("  本站行数:", n, "（0 = 尚未重建，次日 04:25 增量重建，属预期）")

print("\n=== ③ 登录 + HTTP ===")
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
try:
    r = opener.open(urllib.request.Request(
        BASE + "/login", data=b"username=admin",
        headers={"Content-Type": "application/x-www-form-urlencoded"}), timeout=20)
    print("  POST /login → HTTP", r.getcode())
except Exception as e:
    print("  ❌ 登录:", str(e)[:70])

w = WORDS[0]
try:
    h = opener.open(BASE + "/crawler/?q=" + urllib.parse.quote(w), timeout=40).read().decode("utf-8", "ignore")
    m = re.search(r"window\.__BOOT__ = (\{.*?\});</script>", h, re.S)
    if m:
        boot = json.loads(m.group(1))
        items = boot.get("items") or []
        mine = [it for it in items if "江城" in (it.get("site") or "")]
        print("  /crawler/?q=%s → 首屏 %d 条，本站 %d 条" % (w, len(items), len(mine)))
        for it in items[:4]:
            print("     ·", (it.get("site") or "?")[:22], "|", (it.get("title") or "")[:34])
    else:
        print("  ⚠️ 未拿到 BOOT")
except Exception as e:
    print("  ❌ /crawler/:", str(e)[:70])

print("\n=== ③ 详情页渲染 ===")
row = db.execute("SELECT id, title FROM gov_raw WHERE site_name=? ORDER BY publish_date DESC LIMIT 1", (SITE,)).fetchone()
if row:
    try:
        h = opener.open(BASE + "/crawler/project/%s" % row[0], timeout=30).read().decode("utf-8", "ignore")
        print("  id=%s HTTP 200 字节=%d" % (row[0], len(h)))
        print("  含标题:", row[1][:16] in h, "| 含 <p>:", "<p" in h, "| 含附件链接:", "virtual_attach_file" in h)
        print("  含噪声(已下载/下一条/nextList):", any(x in h for x in ["已下载", "下一条", "nextList"]))
    except Exception as e:
        print("  ❌ 详情:", str(e)[:70])

print("\n=== ③ 附件真实下载实测（Chrome UA + Referer）===")
arow = db.execute("SELECT attachments FROM gov_raw WHERE site_name=? AND attachments IS NOT NULL AND attachments!='' LIMIT 1", (SITE,)).fetchone()
if arow:
    a = json.loads(arow[0])[0]
    req = urllib.request.Request(a["url"], headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                      "(KHTML, like Gecko) Chrome/120.0 Safari/537.36",
        "Referer": BASE + "/", "Accept": "*/*"})
    try:
        r = urllib.request.urlopen(req, timeout=60)
        data = r.read()
        print("  %s" % a["title"][:52])
        print("  HTTP %s | %d 字节 | Content-Type: %s" % (r.getcode(), len(data), r.headers.get("Content-Type")))
        print("  Content-Disposition:", (r.headers.get("Content-Disposition") or "(无)")[:90])
        print("  xlsx 魔数(PK):", data[:2] == b"PK")
    except Exception as e:
        print("  ❌ 下载失败:", str(e)[:90])
db.close()
