#!/usr/bin/env python3
"""Fix liuzhi - use detail-nr selector first"""
PATH = '/root/gov_crawler/crawl_liuzhi_gsgg.py'

with open(PATH, 'r') as f:
    content = f.read()

old = '    for sel in ["div.container div.detail", "div.detail", "div.container",\n                "div.trs_editor_view", "div.lzzf-mainbox"]:'
new = '    for sel in ["div.detail-nr", "div.container div.detail", "div.detail", "div.container",\n                "div.trs_editor_view", "div.lzzf-mainbox"]:'

if old in content:
    content = content.replace(old, new, 1)
    with open(PATH, 'w') as f:
        f.write(content)
    import py_compile
    py_compile.compile(PATH, doraise=True)
    print("Patch applied: detail-nr first in selector chain")
else:
    print("ERROR: old text not found!")
    idx = content.find('div.container div.detail')
    if idx >= 0:
        print(f"Found at {idx}: ...{content[idx:idx+150]}...")
