#!/bin/bash
set -u
cd /root/gov_crawler
echo "=== 1) 补 import time ==="
python3 - <<'PYEOF'
import ast, re, shutil, os
p = "/root/gov_crawler/crawl_heshan_gkmlpt.py"
src = open(p, encoding="utf-8").read()
if re.search(r"(?m)^import time\b", src):
    print("  已有 import time")
else:
    m = re.search(r"(?m)^import [^\n]+\n", src)
    src2 = src[:m.end()] + "import time\n" + src[m.end():]
    ast.parse(src2)
    b = "/root/gov_crawler/Archive/crawl_heshan_gkmlpt.py.bak_20260926_timeimport"
    if not os.path.exists(b): shutil.copy2(p, b)
    open(p, "w", encoding="utf-8").write(src2)
    print("  ✅ 已插入 import time")
import py_compile; py_compile.compile(p, doraise=True); print("  py_compile ✅")
PYEOF
echo "=== 2) 核对 config ==="
python3 -c "import json;c=json.load(open('/root/gov_crawler/daily_crawl_config.json',encoding='utf-8'));lst=c if isinstance(c,list) else c.get('tasks',[]);e=[x for x in lst if x.get('script')=='crawl_heshan_gkmlpt.py'];print('  timeout =', e[0].get('timeout') if e else 'N/A')"
