#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# fix_jxth_api.py — 用今晚验证过的实现替换 fetch_api
import ast, os, re, shutil, sqlite3, subprocess, time
D = '/root/gov_crawler/'
f = 'crawl_jxth.py'
p = D + f
src = open(p, encoding='utf-8').read()
m = re.search(r'(?ms)^def fetch_api\(.*?\n(?=\S)', src)
if not m:
    print('WARN 未找到 fetch_api')
    raise SystemExit(1)
print('=== 原 fetch_api ===')
print(m.group(0)[:600])
NEW = """def fetch_api(page):
    # QC20260926 重写：原实现请求失败即返回 None → 上层打印"第1页无数据"
    url = f"{BASE}/api-ajax_list-{page}.html"
    body = [("ajax_type[]", "9_xxgk"), ("ajax_type[]", "167445"), ("ajax_type[]", "9"),
            ("ajax_type[]", "xxgk"), ("ajax_type[]", "Y-m-d"), ("ajax_type[]", "50"),
            ("ajax_type[]", "20"), ("ajax_type[]", "is_top DESC"),
            ("ajax_type[]", "displayorder DESC"), ("ajax_type[]", "inputtime DESC"),
            ("ajax_type[]", ""), ("is_ds", "1")]
    hh = dict(H)
    hh.update({"X-Requested-With": "XMLHttpRequest",
               "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
               "Referer": BASE + "/"})
    for _try in range(3):
        try:
            r = requests.post(url, data=urlencode(body), headers=hh, timeout=30)
            if r.status_code == 200:
                r.encoding = 'utf-8'
                return json.loads(r.text)
        except Exception as e:
            print('  [RETRY %d/3] %s' % (_try + 1, e), flush=True)
        time.sleep(3)
    return None


"""
new = src[:m.start()] + NEW + src[m.end():]
adds = []
if not re.search(r'(?m)^import time\b', new):
    new = re.sub(r'(?m)^(import json[^\n]*\n)', r'\1import time\n', new, count=1)
    adds.append('time')
if 'urlencode' not in new:
    new = re.sub(r'(?m)^(import json[^\n]*\n)', r'\1from urllib.parse import urlencode\n', new, count=1)
    adds.append('urlencode')
print('补 import:', adds or '无')
try:
    ast.parse(new)
except SyntaxError as e:
    print('语法错（未落盘）:', e)
    raise SystemExit(1)
b = D + 'Archive/' + f + '.bak_20260926_api'
if not os.path.exists(b):
    shutil.copy2(p, b)
open(p, 'w', encoding='utf-8').write(new)
print('OK 已落盘（备份 %s）' % os.path.basename(b))
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()][-5:]:
        print('   out:', x[:110])
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 '未变')
