fork/nix-minecraft

git clone https://git.julia.blue/fork/nix-minecraft

fmbearmfpurpur-servers: init115703f

master
3.0 KiB109 linesraw
1#!/usr/bin/env nix-shell
2#!nix-shell -i python3 -p python3Packages.requests  python3Packages.progressbar
3
4import json
5import requests
6import hashlib
7from pathlib import Path
8from requests.adapters import HTTPAdapter, Retry
9import time
10import progressbar
11
12
13ENDPOINT = "https://api.purpurmc.org/v2/purpur"
14
15def load_lock(path):
16    print("Loading lock file")
17    if not path.exists():
18        print("└ Creating one from scratch")
19        return {}
20    with open(path, "r+") as f:
21        data = json.load(f)
22    return data
23
24def save_lock(path, data):
25    with open(path, "w") as f:
26        json.dump(data, f, indent=2)
27
28
29
30def get_versions():
31    print("Fetching Versions")
32    versions = requests.get(ENDPOINT).json()["versions"]
33    print(f"└ Total of {len(versions)} versions")
34    return versions
35
36def get_builds(version):
37    print(f"Fetching {version} version builds")
38    builds = requests.get(f"{ENDPOINT}/{version}").json()["builds"]["all"]
39    print(f"└ Total of {len(builds)} builds")
40    return builds
41
42def get_build_info(version, build):
43    print(f" └ Fetching {build} build info")
44    info = requests.get(f"{ENDPOINT}/{version}/{build}").json()
45    return info
46
47def get_build_sha256(build_url):
48    print(f"    └ Generating SHA256")
49    sha256 = hashlib.sha256()
50    with requests.get(build_url, stream=True) as response:
51        response.raise_for_status()
52        for chunk in response.iter_content(8192):
53            sha256.update(chunk)
54    return sha256.hexdigest()
55
56def main(lock_path, bad_path):
57    lock_data = load_lock(lock_path)
58    bad_data = load_lock(bad_path)
59    versions = get_versions();
60    for version in versions:
61
62        # When version doesn't exist in lock file
63        if version not in lock_data:
64            lock_data[version] = {}
65            bad_data[version] = []
66        builds = get_builds(version)
67        updated = 0
68        for build in builds:
69            if build in lock_data[version]:
70                continue
71            if build in bad_data[version]:
72                continue
73
74            build_info = get_build_info(version, build)
75
76
77            if build_info["result"] == "FAILURE":
78                print(f"   └ Failed to get build info of {build}")
79                bad_data[version].append(build)                    
80                continue
81
82            build_download = f"{ENDPOINT}/{version}/{build}/download"
83            build_sha256 = get_build_sha256(build_download)
84
85            updated += 1
86            lock_data[version][build] = {
87                "sha256": build_sha256
88            }
89
90
91    save_lock(bad_path, bad_data)
92    save_lock(lock_path, lock_data)
93    print(f"-> Updated {updated} builds of version {version}")
94
95
96            
97        
98
99if __name__ == "__main__":
100    start = time.process_time()
101    folder = Path(__file__).parent
102    lock_path = Path(folder / "lock.json")
103    # Saving builds not found
104    bad_path = Path(folder / "bad.json")
105    main(lock_path, bad_path)
106    end = time.process_time() - start
107    print(f"Finished {round(end, 2)}s")
108
109