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

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

def fix_content(content):
    if not content or "\\u" not in content:
        return None
    # JS字符串中的 \\uXXXX 在Python中表现为 \\uXXXX (两个反斜杠+u)
    # 先替换双反斜杠为单反斜杠，再解码 \uXXXX
    new = content.replace("\\\\", "\\")
    new = re.sub(r'\\u([0-9a-fA-F]{4})', lambda m: chr(int(m.group(1), 16)), new)
    return new if new != content else None

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

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

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

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