"""Per-campaign scrape checkpoint stored locally in data/checkpoints/.""" from __future__ import annotations import json from pathlib import Path from typing import Any, cast _LOCAL_DIR = Path("data/checkpoints") def _local_path(campaign_id: str) -> Path: return _LOCAL_DIR / f"{campaign_id}.json" def load_checkpoint(campaign_id: str) -> dict[str, Any]: p = _local_path(campaign_id) if p.exists(): return cast(dict[str, Any], json.loads(p.read_text(encoding="utf-8"))) return {"completed": [], "failed": []} def save_checkpoint(campaign_id: str, data: dict[str, Any]) -> None: _LOCAL_DIR.mkdir(parents=True, exist_ok=True) _local_path(campaign_id).write_text( json.dumps(data, indent=2, default=str), encoding="utf-8" ) def clear_checkpoint(campaign_id: str) -> None: p = _local_path(campaign_id) if p.exists(): p.unlink()