#!/usr/bin/env python3
"""Update yanshougs config in daily_crawl_config.json"""
import json

CONFIG_PATH = "/root/daily_crawl_config.json"

with open(CONFIG_PATH) as f:
    cfg = json.load(f)

# Fix existing yanshougs entry: swap script to real crawler
found_main = False
for c in cfg["crawlers"]:
    if c["name"] == "工程建设验收公示网":
        c["script"] = "crawl_yanshougs_sbys.py"
        c["args"] = ""
        c.pop("note", None)
        c["note"] = "yanshougs.com 水保验收(id=2), 日跑5页增量"
        c["timeout"] = 300
        found_main = True
        break

if not found_main:
    cfg["crawlers"].append({
        "name": "工程建设验收公示网",
        "script": "crawl_yanshougs_sbys.py",
        "args": "",
        "incremental": True,
        "sync_mode": "direct_server",
        "enabled": True,
        "group": "全国",
        "note": "yanshougs.com 水保验收(id=2), 日跑5页增量",
        "timeout": 300
    })

# Add 环评公示 (list/4.html, id=4)
existing_hpgs = any(c["name"] == "工程建设验收公示网-环评公示" for c in cfg["crawlers"])
if not existing_hpgs:
    cfg["crawlers"].append({
        "name": "工程建设验收公示网-环评公示",
        "script": "crawl_yanshougs_sbys.py",
        "args": "--id 4 --name yanshougs_hpgs",
        "incremental": True,
        "sync_mode": "direct_server",
        "enabled": True,
        "group": "全国",
        "note": "yanshougs.com 环评公示(list/4.html, id=4), 日跑5页增量",
        "timeout": 300
    })

with open(CONFIG_PATH, "w") as f:
    json.dump(cfg, f, ensure_ascii=False, indent=2)

print("=== Updated yanshougs config ===")
for c in cfg["crawlers"]:
    if "yanshou" in c.get("script", "") or "yanshou" in c.get("name", "").lower():
        print(f"  Name: {c['name']}")
        print(f"  Script: {c['script']}")
        print(f"  Args: {c.get('args', '')}")
        print(f"  Note: {c.get('note', '')}")
        print()
