#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""福泉站「<p> 碎片」历史残留重抓回写（2026-09-11）。

背景：该站旧版脚本把每个碎片各包一个 <p> 入库（`<p>提取码：</p>\\n\\n<p>pwaj</p>…`），
      详情页一段一行碎片。显示端**不做**启发式合并（会把表格单元格/落款/附件名粘成一坨，
      实测已回退），正确做法是**用修好的爬虫重抓**。

用法：
    python3 fix_fuquan_reflow_20260911.py            # 干跑：列出将回写的行
    python3 fix_fuquan_reflow_20260911.py --apply    # 落盘（先建回滚表）
"""
import sys, os, time, sqlite3, importlib.util

APPLY = '--apply' in sys.argv
DB = '/root/search.db'
SITE = '福泉市-生态环境'
BAK = 'gov_raw_content_bak_20260911_fq'
SLEEP = 0.6

spec = importlib.util.spec_from_file_location('cf', '/root/gov_crawler/crawl_fuquan.py')
cf = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cf)

conn = sqlite3.connect(DB, timeout=60)
phys = os.environ.get('SEARCH_DB') or DB
conn.row_factory = sqlite3.Row

rows = conn.execute(
    "SELECT id, page_url, title, content FROM gov_raw "
    "WHERE (script_name='crawl_fuquan.py' OR site_name IN (?, '福泉市人民政府 - 通知公告')) "
    "AND (content LIKE '%<p>%' OR (IFNULL(script_name,'')='' AND IFNULL(content,'')<>'')) "
    "ORDER BY id", (SITE,)).fetchall()
print("候选 %d 行" % len(rows))
if not APPLY:
    for r in rows[:8]:
        print("  id=%s script=%s len=%d | %s" % (r['id'], '', len(r['content'] or ''), (r['title'] or '')[:48]))
    print("  … 共 %d 行（干跑，未落盘）" % len(rows))
    sys.exit(0)

conn.execute('DROP TABLE IF EXISTS ' + BAK)
conn.execute('CREATE TABLE %s AS SELECT id, content, summary, script_name FROM gov_raw WHERE site_name=?' % BAK, (SITE,))
conn.commit()
print("回滚表 %s 已建（%d 行）" % (BAK, conn.execute('SELECT COUNT(*) FROM ' + BAK).fetchone()[0]))

ok = fail = skip = 0
for r in rows:
    url = r['page_url']
    if not url or 'gov.cn' not in url:
        skip += 1
        continue
    try:
        html = cf.fetch(url)
        if not html:
            fail += 1
            print("  [FAIL-fetch] %s" % url)
            continue
        d = cf.parse_detail(html, url)
        new = (d.get('content') or '').strip()
        if len(new) < 20:
            fail += 1
            print("  [FAIL-empty] %s" % url)
            continue
        summary = new[:200].replace('\n', ' ').strip()
        conn.execute("UPDATE gov_raw SET content=?, summary=?, script_name=? WHERE id=?",
                     (new, summary, 'crawl_fuquan.py', r['id']))
        conn.commit()
        ok += 1
        if ok <= 3:
            print("  --- id=%s" % r['id'])
            print("      BEFORE: %r" % (r['content'] or '')[:150])
            print("      AFTER : %r" % new[:150])
    except Exception as e:
        fail += 1
        print("  [FAIL] %s : %s" % (url, str(e)[:60]))
    time.sleep(SLEEP)

print()
print("回写成功 %d / 失败 %d / 跳过 %d" % (ok, fail, skip))
print("回滚：CREATE TABLE ... AS SELECT * FROM %s（服务器保留）" % BAK)
