#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""核实：仙桃生态环境局列表页的条目，在库里究竟挂在哪个 script_name / site_name 下。"""
import re
import sqlite3
import sys

import requests
from bs4 import BeautifulSoup

BASE = "https://www.xiantao.gov.cn"
LIST_URL = BASE + "/bmxxgk/shbj/zfxxgk/fdzdgknr/qtzdgknr_37950/gsgg/index.shtml"
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36"}

r = requests.get(LIST_URL, headers=HEADERS, timeout=30)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
urls = []
for a in soup.find_all("a", href=True):
    h = a["href"]
    if "/shbj/" not in h or "/gsgg/" not in h:
        continue
    if h.rstrip("/").endswith("/gsgg"):
        continue
    if not h.startswith("http"):
        h = BASE + h
    if h not in urls:
        urls.append(h)

print("列表页条目 %d 个\n" % len(urls))
db = sqlite3.connect("/root/search.db", timeout=120)
c = db.cursor()
print("%-58s %-26s %-12s %s" % ("URL 尾部", "script_name", "site_name", "publish_date"))
print("-" * 118)
hit = miss = 0
for u in urls:
    row = c.execute("""SELECT COALESCE(script_name,'(空)'), COALESCE(site_name,''),
                              COALESCE(publish_date,'(无)')
                       FROM gov_raw WHERE page_url=?""", (u,)).fetchone()
    tail = u.split("/gsgg/")[-1]
    if row:
        hit += 1
        print("%-58s %-26s %-12s %s" % (tail, row[0], row[1][:12], row[2]))
    else:
        miss += 1
        print("%-58s %-26s %-12s %s" % (tail, "❌ 不在库里", "", ""))
print()
print("在库 %d / 不在库 %d" % (hit, miss))
db.close()
