File size: 1,200 Bytes
6bc6eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()