#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""决定性核对：这几个站点的当前条目，到底在不在 gov_raw 里、挂在谁名下。"""
import importlib.util
import os
import sqlite3
import sys

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

CASES = ["crawl_lanshantunhe.py", "crawl_changji.py", "crawl_kecheng.py", "crawl_hky.py", "crawl_hld_tzgg.py"]
c = sqlite3.connect("/root/search.db", timeout=180)
c.execute("PRAGMA busy_timeout=180000")

for fn in CASES:
    print("=" * 96)
    print("【%s】" % fn)
    # 取该脚本自己写入 site_name
    sn = c.execute("SELECT site_name, COUNT(*) FROM gov_raw WHERE script_name=? GROUP BY 1 ORDER BY 2 DESC LIMIT 1",
                   (fn,)).fetchone()
    if not sn:
        print("  （库内无该脚本记录）")
        continue
    site = sn[0]
    tot, mx = c.execute("SELECT COUNT(*), MAX(publish_date) FROM gov_raw WHERE site_name=?", (site,)).fetchone()
    print("  site_name=%s  库内 %d 条  最新 %s" % (site, tot, mx))
    # 该 site_name 下最近 5 条
    for r in c.execute("""SELECT COALESCE(script_name,'(空)'), publish_date, substr(title,1,40)
                          FROM gov_raw WHERE site_name=? ORDER BY id DESC LIMIT 5""", (site,)):
        print("     %-26s %-11s %s" % (r[0], r[1], r[2]))
    # 最近 7 天入库的
    n7 = c.execute("""SELECT COUNT(*) FROM gov_raw WHERE site_name=?
                      AND inserted_at >= datetime('now','-7 days')""", (site,)).fetchone()[0]
    print("  近 7 天入库: %d 条" % n7)
c.close()
