#!/usr/bin/env python3
"""Fix liuzhi detail page - clean up share/nav/related junk from content"""
with open('/root/gov_crawler/crawl_liuzhi_gsgg.py', 'r') as f:
    content = f.read()

old = """    # Content - use div.container div.detail or trs_editor_view
    body_html = \"\"
    for sel in [\"div.container div.detail\", \"div.detail\", \"div.container\",
                \"div.trs_editor_view\", \"div.lzzf-mainbox\"]:
        div = soup.select_one(sel)
        if div:
            body_html = str(div)
            break"""

new = """    # Content - use div.container div.detail or trs_editor_view
    body_html = \"\"
    for sel in [\"div.container div.detail\", \"div.detail\", \"div.container\",
                \"div.trs_editor_view\", \"div.lzzf-mainbox\"]:
        div = soup.select_one(sel)
        if div:
            body_html = str(div)
            break
    
    # Clean up unwanted elements at bottom (share buttons, nav, related info)
    if body_html:
        ts = BeautifulSoup(body_html, \"html.parser\")
        for tag in ts.select(\".yjfx-btn-span, .windShare, .sxp-ul, .information, script, link\"):
            tag.decompose()
        body_html = str(ts)"""

if old in content:
    content = content.replace(old, new, 1)
    with open('/root/gov_crawler/crawl_liuzhi_gsgg.py', 'w') as f:
        f.write(content)
    import py_compile
    py_compile.compile('/root/gov_crawler/crawl_liuzhi_gsgg.py', doraise=True)
    print("Patch applied, syntax OK")
else:
    print("ERROR: Marker not found!")
