#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""fqlook 专项：用列表页建 tid→日期 映射，回填无日期记录。

列表页已验证能取到日期（parse_list 返回 (title, tid, author, date)）。
只做 UPDATE publish_date WHERE id=?，写入前备份。
用法: python3 backfill_fqlook_dates.py [--dry-run]
"""
import importlib.util
import os
import re
import sqlite3
import sys

D = "/root/gov_crawler"
os.chdir(D)
sys.path.insert(0, D)
DB = "/root/search.db"
DRY = "--dry-run" in sys.argv

spec = importlib.util.spec_from_file_location("fq", os.path.join(D, "crawl_fqlook.py"))
m = importlib.util.module_from_spec(spec)
try:
    spec.loader.exec_module(m)
except SystemExit:
    pass

# 建 tid → 日期 映射（多抓几页）
tid2date = {}
for pg in range(1, 7):
    url = "https://www.fqlook.cn/forum-120-%d.html" % pg
    try:
        html = m.fetch(url)
        items = m.parse_list(html)
    except Exception as e:
        print("  页面 %d 失败: %s" % (pg, str(e)[:70]))
        continue
    got = 0
    for it in items:
        try:
            title, tid, author, date = it
        except Exception:
            continue
        if tid and date and tid not in tid2date:
            tid2date[tid] = date
            got += 1
    print("  列表页 %d: %d 条, 新增日期 %d" % (pg, len(items), got))

print("映射表: %d 个 tid" % len(tid2date))

db = sqlite3.connect(DB, timeout=300)
db.execute("PRAGMA busy_timeout=300000")
c = db.cursor()
db.execute("""CREATE TABLE IF NOT EXISTS bak_datefix_20260921 (
                id INTEGER PRIMARY KEY, script_name TEXT, old_date TEXT, new_date TEXT)""")

rows = list(c.execute("""SELECT id, COALESCE(publish_date,''), COALESCE(page_url,'')
                         FROM gov_raw WHERE script_name='crawl_fqlook.py'"""))
bad = [(i, p, u) for i, p, u in rows
       if not re.match(r"^\d{4}-\d{1,2}-\d{1,2}$", p.strip())]
print("fqlook 总 %d 条，日期异常 %d 条" % (len(rows), len(bad)))

fixed = 0
for rid, old, url in bad:
    mm = re.search(r"thread-(\d+)-", url or "")
    if not mm:
        continue
    date = tid2date.get(mm.group(1), "")
    mm2 = re.match(r"(\d{4})-(\d{1,2})-(\d{1,2})", date or "")
    if not mm2:
        continue
    new = "%04d-%02d-%02d" % (int(mm2.group(1)), int(mm2.group(2)), int(mm2.group(3)))
    if new != old:
        if not DRY:
            c.execute("INSERT OR REPLACE INTO bak_datefix_20260921 VALUES (?,?,?,?)",
                      (rid, "crawl_fqlook.py", old, new))
            c.execute("UPDATE gov_raw SET publish_date=? WHERE id=?", (new, rid))
        fixed += 1
        if fixed <= 5:
            print("   ✓ tid=%s  %r → %s" % (mm.group(1), old, new))

if not DRY:
    db.commit()
print()
print("fqlook 修复 %d 条%s" % (fixed, "  [DRY-RUN]" if DRY else ""))
print("备份表累计 %d 行" % c.execute("SELECT COUNT(*) FROM bak_datefix_20260921").fetchone()[0])
db.close()
