#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""verify_b6_dl.py —— 附件真实下载实测（逐个带超时）"""
import json, sqlite3, urllib.request
CASES = [("宾阳","宾阳县-建设项目环境影响评价审批"),("青阳","青阳县-建设项目环境影响评价审批"),
         ("缙云","缙云县-行政许可（受理）公示"),("缙云B","缙云县-建设项目环境影响评价信息公示"),
         ("大足","大足区古龙镇-其他公文")]
db = sqlite3.connect("/root/search.db", timeout=30)
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": "http://127.0.0.1:8000/"})
    try:
        r = urllib.request.urlopen(req, timeout=25)
        d = r.read(400000)
        m = d[:4]
        kind = ("PDF" if m[:4]==b"%PDF" else "ZIP/OOXML" if m[:2]==b"PK" else "JPG" if m[:2]==b"\xff\xd8" else "PNG" if m[:4]==b"\x89PNG" else "其他")
        print("  %-6s HTTP%s %8d字节 | %s | %s" % (tag, r.getcode(), len(d), kind, att["title"][:32]))
    except Exception as e:
        print("  %-6s ❌ %s" % (tag, str(e)[:70]))
db.close()
