#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# fix_jxth_import.py — 补 from urllib.parse import urlencode
import ast, os, re, shutil, sqlite3, subprocess
D = '/root/gov_crawler/'
f = 'crawl_jxth.py'
p = D + f
src = open(p, encoding='utf-8').read()
print('已有该 import:', bool(re.search(r'(?m)^from urllib\.parse import .*urlencode', src)))
if not re.search(r'(?m)^from urllib\.parse import .*urlencode', src):
    src2 = re.sub(r'(?m)^(import json[^\n]*\n)', r'\1from urllib.parse import urlencode\n', src, count=1)
    if src2 == src:
        src2 = 'from urllib.parse import urlencode\n' + src
    ast.parse(src2)
    b = D + 'Archive/' + f + '.bak_20260926_imp2'
    if not os.path.exists(b):
        shutil.copy2(p, b)
    open(p, 'w', encoding='utf-8').write(src2)
    print('OK 已补 import（备份 %s）' % os.path.basename(b))
else:
    print('无需处理')
con = sqlite3.connect('file:/root/search.db?mode=ro', uri=True, timeout=30)
n0 = con.execute("SELECT COUNT(*) FROM gov_raw WHERE page_url LIKE '%jxth.gov.cn%'").fetchone()[0]
print('跑前行数:', n0)
try:
    r = subprocess.run(['python3', p], capture_output=True, text=True, timeout=200, cwd=D)
    print('EXIT=%d' % r.returncode)
    for x in [y for y in (r.stdout or '').split('\n') if y.strip()][-6:]:
        print('   out:', x[:115])
except subprocess.TimeoutExpired:
    print('>200s（在抓详情：说明列表终于拿到了）')
con2 = sqlite3.connect('file:/root/search.db?mode=ro', uri=True, timeout=30)
n1 = con2.execute("SELECT COUNT(*) FROM gov_raw WHERE page_url LIKE '%jxth.gov.cn%'").fetchone()[0]
print('跑后行数:', n1, ('OK +%d' % (n1 - n0)) if n1 > n0 else '未变')
