import os from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TARGET = ROOT / "src" / "sermon_chunks.pkl" def main() -> None: if TARGET.exists() and TARGET.stat().st_size > 0: print(f"Using existing chunk file: {TARGET}") return url = os.getenv("SERMON_CHUNKS_URL", "").strip() if not url: raise RuntimeError( "src/sermon_chunks.pkl is missing and SERMON_CHUNKS_URL is not set." ) TARGET.parent.mkdir(parents=True, exist_ok=True) tmp_path = TARGET.with_suffix(".pkl.download") try: import gdown output = gdown.download(url=url, output=str(tmp_path), quiet=False, fuzzy=True) if not output: raise RuntimeError("gdown did not return an output path.") except Exception as exc: if tmp_path.exists(): tmp_path.unlink() raise RuntimeError(f"Failed to download sermon chunks: {exc}") from exc if not tmp_path.exists() or tmp_path.stat().st_size == 0: raise RuntimeError("Downloaded sermon chunk file is empty.") tmp_path.replace(TARGET) print(f"Downloaded sermon chunks to: {TARGET}") if __name__ == "__main__": main()