#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# fix_crawler_route2.py — 找 handle_db_list 的出口并插入过滤
import ast, os, re, shutil
D = '/root/gov_crawler/'
p = D + 'search_app.py'
src = open(p, encoding='utf-8').read()
lines = src.split('\n')
m = re.search(r'^    def handle_db_list\(self, db_type, params\):', src, re.M)
start = src[:m.end()].count('\n')
end = len(lines)
for i in range(start + 1, len(lines)):
    if re.match(r'^    def |^    @|^class ', lines[i]):
        end = i
        break
print('=== handle_db_list 里的出口候选（L%d-5777）===' % (start + 1))
cands = []
for i in range(start, end):
    l = lines[i]
    if re.search(r'\breturn\b', l) and 'rows' in l:
        cands.append((i, l))
        print('  L%-5d %s' % (i + 1, l.strip()[:110]))
    elif re.search(r'render_list_shell\(|self\.render_[a-z_]+\(', l) and 'rows' in l:
        cands.append((i, l))
        print('  L%-5d %s [渲染调用]' % (i + 1, l.strip()[:104]))
if not cands:
    print('  (没有含 rows 的 return/render —— 打印所有 self.render_ 调用)')
    for i in range(start, end):
        if 'render_' in lines[i] or 'return ' in lines[i]:
            print('  L%-5d %s' % (i + 1, lines[i].strip()[:104]))
    raise SystemExit(0)

FILTER = [
    '# QC20261008 (/crawler) 排除词过滤（只排标题，出口前）',
    'try:',
    '    _ei, _ee = parse_google_query((getattr(self, "_orig_q", None) or q) or "")',
    '    if _ee and rows:',
    '        _bk = len(rows)',
    '        def _ttl(_r):',
    '            try:',
    '                return (_r.get("title") if isinstance(_r, dict) else _r["title"]) or ""',
    '            except Exception:',
    '                return ""',
    '        rows = [_r for _r in rows if not any(_t in _ttl(_r) for _t in _ee)]',
    '        if len(rows) != _bk:',
    '            try:',
    '                total = max(0, int(total or 0) - (_bk - len(rows)))',
    '            except Exception:',
    '                pass',
    'except Exception:',
    '    pass',
]
# 只插最后一处（出口）
i, l = cands[-1]
ind = l[:len(l) - len(l.lstrip())]
new = lines[:i] + [(ind + x) if x.strip() else x for x in FILTER] + lines[i:]
src2 = '\n'.join(new)
try:
    ast.parse(src2)
except SyntaxError as e:
    print('语法错，未落盘:', e)
    raise SystemExit(1)
b = D + 'Archive/search_app.py.bak_20261008_crawlerexit'
if not os.path.exists(b):
    shutil.copy2(p, b)
open(p, 'w', encoding='utf-8').write(src2)
print('OK 已在 L%d 之前插入过滤（备份 search_app.py.bak_20261008_crawlerexit）' % (i + 1))
