#!/usr/bin/env python3
import os
"""修复曹县已入库记录中的 \\uXXXX Unicode转义序列为实际中文字符"""
import re, sqlite3

DB_PATH = os.getenv("SEARCH_DB", "/root/search.db")

def fix_unicode(content):
    if not content:
        return content
    new_content = re.sub(r'\\u([0-9a-fA-F]{4})', lambda m: chr(int(m.group(1), 16)), content)
    return new_content if new_content != content else None

conn = sqlite3.connect(DB_PATH)
c = conn.cursor()

# Find records with \\u sequences
rows = c.execute("SELECT rowid, content FROM gov_raw WHERE site_name LIKE '%曹县%' AND content LIKE '%\\\\u%'").fetchall()
print("Records with \\u sequences: %d" % len(rows))

fixed = 0
for rowid, content in rows:
    new_content = fix_unicode(content)
    if new_content:
        c.execute("UPDATE gov_raw SET content = ? WHERE rowid = ?", (new_content, rowid))
        fixed += 1

conn.commit()
conn.close()
print("Fixed: %d records" % fixed)
