#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""verify_batch6.py —— 第 9 步：FTS 两路验证 + 详情页渲染 + 附件真实下载"""
import json
import re
import sqlite3
import urllib.parse
import urllib.request

BASE = "http://127.0.0.1:8000"
CASES = [
    ("宾阳", "宾阳县-建设项目环境影响评价审批", ["再生塑料颗粒", "胶合板", "腻子粉"]),
    ("青阳", "青阳县-建设项目环境影响评价审批", ["精密铸件", "黄精益生菌"]),
    ("缙云", "缙云县-行政许可（受理）公示", ["工模具材料", "液化石油气"]),
    ("缙云B", "缙云县-建设项目环境影响评价信息公示", ["萤石矿", "电镀产业园"]),
    ("大足", "大足区古龙镇-其他公文", ["天青石", "行政执法委托"]),
]

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

print("=== ① DB 层 gov_search MATCH（站点写进 SQL）===")
for tag, site, words in CASES:
    for w in words:
        try:
            n = db.execute("SELECT COUNT(*) FROM gov_search g JOIN gov_raw r ON r.id=g.rowid "
                           "WHERE gov_search MATCH ? AND r.site_name=?", (w, site)).fetchone()[0]
            print("  %-6s MATCH %-12s → %d 命中 %s" % (tag, w, n, "✅" if n else "⚠️"))
        except Exception as e:
            print("  %-6s MATCH %-12s → ❌ %s" % (tag, w, str(e)[:50]))

print("\n=== ④ gov_bigram（新数据次日 04:25 才进）===")
for tag, site, _ in CASES:
    n = db.execute("SELECT COUNT(*) FROM gov_bigram WHERE rowid IN (SELECT id FROM gov_raw WHERE site_name=?)", (site,)).fetchone()[0]
    print("  %-6s 本站行数 %d" % (tag, n))

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

for tag, site, _ in CASES:
    row = db.execute("SELECT id, title FROM gov_raw WHERE site_name=? ORDER BY publish_date DESC LIMIT 1", (site,)).fetchone()
    if not row:
        continue
    try:
        h = opener.open(BASE + "/crawler/project/%s" % row[0], timeout=30).read().decode("utf-8", "ignore")
        noise = any(x in h for x in ["索引号：", "发布机构：", "点击查看源文件", "扫一扫", "字体【"])
        print("  %-6s HTTP200 %5d字节 | <p>=%d | 噪声=%s | %s" % (
            tag, len(h), len(re.findall(r"<p[ >]", h)), noise, row[1][:34]))
    except Exception as e:
        print("  %-6s ❌ %s" % (tag, str(e)[:60]))

print("\n=== ③ 附件真实下载实测 ===")
for tag, site, _ in CASES:
    a = db.execute("SELECT attachments FROM gov_raw WHERE site_name=? AND attachments IS NOT NULL "
                   "AND attachments!='' LIMIT 1", (site,)).fetchone()
    if not a:
        print("  %-6s （无附件）" % tag)
        continue
    att = json.loads(a[0])[0]
    req = urllib.request.Request(att["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()
        magic = data[:4]
        kind = ("PDF" if magic[:4] == b"%PDF" else "ZIP/OOXML" if magic[:2] == b"PK"
                else "JPG" if magic[:2] == b"\xff\xd8" else "PNG" if magic[:4] == b"\x89PNG" else "其他")
        print("  %-6s HTTP%s %8d字节 | %s | %s" % (tag, r.getcode(), len(data), kind, att["title"][:34]))
    except Exception as e:
        print("  %-6s ❌ %s" % (tag, str(e)[:60]))
db.close()
