summaryrefslogtreecommitdiff
path: root/scripts/setup_eve_data.py
blob: a7f7efd94ca3a251ea3f6729dfec4749ffded878 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import annotations

import bz2
import shutil
import sys
import urllib.request
from pathlib import Path


ROOT_DIR = Path(__file__).resolve().parent.parent
DATA_DIR = ROOT_DIR / "backend" / "data"
BASE_URL = "https://www.fuzzwork.co.uk/dump/latest"

REQUIRED_FILES = {
    "mapDenormalize.csv": f"{BASE_URL}/mapDenormalize.csv.bz2",
    "mapJumps.csv": f"{BASE_URL}/mapJumps.csv.bz2",
    "invTypes.csv": f"{BASE_URL}/invTypes.csv.bz2",
    "mapRegions.csv": f"{BASE_URL}/mapRegions.csv.bz2",
}


def download_and_extract(file_name: str, url: str) -> None:
    destination = DATA_DIR / file_name
    archive_path = destination.with_suffix(destination.suffix + ".bz2")

    if destination.exists():
        print(f"Skipping {file_name}; already exists.")
        return

    print(f"Downloading {file_name} from {url}")

    try:
        with urllib.request.urlopen(url, timeout=60) as response, archive_path.open("wb") as output_file:
            shutil.copyfileobj(response, output_file)

        with bz2.open(archive_path, "rb") as compressed_file, destination.open("wb") as output_file:
            shutil.copyfileobj(compressed_file, output_file)
    finally:
        archive_path.unlink(missing_ok=True)

    print(f"Wrote {destination}")


def main() -> int:
    DATA_DIR.mkdir(parents=True, exist_ok=True)

    for file_name, url in REQUIRED_FILES.items():
        download_and_extract(file_name, url)

    print("EVE static data is ready.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())