#!/usr/bin/env python3
"""Fix yushui date extraction"""
with open('/root/gov_crawler/crawl_yushui_zwgg.py', 'r') as f:
    content = f.read()

old = '    # 日期\n    date = ""\n    d_span = soup.find("span", class_="date")\n    if d_span:\n        date = d_span.get_text(strip=True)'
new = '    # 日期\n    date = ""\n    d_span = soup.find("span", class_="date")\n    if d_span:\n        date_raw = d_span.get_text(strip=True)\n        # 清理：去掉"发表日期："前缀，提取YYYY-MM-DD\n        m = re.search(r"(\\d{4}[-/]\\d{1,2}[-/]\\d{1,2})", date_raw)\n        date = m.group(1) if m else date_raw[:10]'

if old in content:
    content = content.replace(old, new, 1)
    with open('/root/gov_crawler/crawl_yushui_zwgg.py', 'w') as f:
        f.write(content)
    import py_compile
    py_compile.compile('/root/gov_crawler/crawl_yushui_zwgg.py', doraise=True)
    print("yushui fixed, syntax OK")
else:
    print("ERROR: pattern not found in yushui!")
    # show context
    idx = content.find('span class_="date"')
    if idx >= 0:
        print(content[idx-50:idx+150])
