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