#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""audit_para_damage.py —— 体检：三个新站里有多少行受「段落被拍平」影响

判据：content 里 <p> 数 远小于 段落数（\n\n 分隔）→ 说明大量段落没被 <p> 包住
      = 浏览器会把它们折叠成一坨
"""
import re
import sqlite3

SITES = ["颍泉区人民政府-公告公示", "丹寨县人民政府-环境执法监管", "江城县人民政府-通知公告"]

db = sqlite3.connect("/root/search.db", timeout=30)
db.execute("PRAGMA busy_timeout=30000")
print("=" * 96)
bad_total = 0
for s in SITES:
    rows = list(db.execute("SELECT id, page_url, title, content FROM gov_raw WHERE site_name=?", (s,)))
    bad = []
    for rid, url, title, c in rows:
        c = c or ""
        segs = [x for x in c.split("\n\n") if x.strip()]
        n_p = len(re.findall(r"<p[ >]", c, re.I))
        if len(segs) >= 3 and n_p < len(segs) * 0.7:
            bad.append((rid, url, title, len(segs), n_p))
    print("【%s】共 %d 行，其中分段受损 %d 行" % (s, len(rows), len(bad)))
    for rid, url, title, segs, n_p in bad[:6]:
        print("   id=%-20s 段=%-3d <p>=%-3d  %s" % (rid, segs, n_p, (title or "")[:44]))
    if len(bad) > 6:
        print("   … 其余 %d 行" % (len(bad) - 6))
    bad_total += len(bad)
print("=" * 96)
print("三站合计受损: %d 行" % bad_total)
print()
print("=== 抽样看受损行的 content 开头（确认是裸文本）===")
for s in SITES:
    r = db.execute("SELECT title, content FROM gov_raw WHERE site_name=? AND content NOT LIKE '%<p>%' LIMIT 1", (s,)).fetchone()
    if r:
        print("【%s】%s" % (s, (r[0] or "")[:40]))
        print("   ", (r[1] or "")[:160].replace("\n", "⏎"))
db.close()
