From aaa72fd6e6e5c209c658406d41bb8dcf39f7de41 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Sat, 26 Feb 2022 19:40:45 -0600 Subject: [PATCH] Added getLatestStable() to modrinth parser --- mcUp.py | 54 +++++++++++++++++++++++++-------------------- parsers/modrinth.py | 34 +++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/mcUp.py b/mcUp.py index e4b3967..763b647 100755 --- a/mcUp.py +++ b/mcUp.py @@ -66,35 +66,41 @@ def modrinth(project, action, subAction): print(parsers.modrinth.determine(project, "version_number")) if subAction == "latest": print(parsers.modrinth.getLatestVersion(project)) + if subAction == "stable": + print(parsers.modrinth.getLatestStable(project)) if subAction == "get_URL": print(parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project))) if action == "download": + if subAction == "stable": + download(project, parsers.modrinth.getLatestStable(project)) if subAction == "latest": - latestVersion = parsers.modrinth.getLatestVersion(project) - output = parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project)) - response = requests.get(output[0], stream=True, timeout=1) - if args.o != None: # Check if user set an output filepath - output_file = args.o - else: - output_file = output[2] - with response as raw: - with open(output_file, 'wb') as file_object: - shutil.copyfileobj(raw.raw, file_object) - print("Downloaded "+latestVersion+" to "+output_file) - #Calculate hash, compare with API given hash - h_sha1 = hashlib.sha1() - with open(output_file, 'rb') as file_object: - chunk = 0 - while chunk != b'': - chunk = file_object.read(1024) - h_sha1.update(chunk) - print("API SHA 1: "+str(output[1])) - print("Our calculated SHA 1: "+str(h_sha1.hexdigest())) - if h_sha1.hexdigest() == output[1]: - print("sha1sum of downloaded file matches the sum that the API gave, jar is safe to use") - else: - raise MismatchSHA1Error + download(project, parsers.modrinth.getLatestVersion(project)) +def download(project, what): + versionToDownload = what + output = parsers.modrinth.getDownloadURL(project, what) + response = requests.get(output[0], stream=True, timeout=1) + if args.o != None: # Check if user set an output filepath + output_file = args.o + else: + output_file = output[2] + with response as raw: + with open(output_file, 'wb') as file_object: + shutil.copyfileobj(raw.raw, file_object) + print("Downloaded "+versionToDownload+" to "+output_file) + #Calculate hash, compare with API given hash + h_sha1 = hashlib.sha1() + with open(output_file, 'rb') as file_object: + chunk = 0 + while chunk != b'': + chunk = file_object.read(1024) + h_sha1.update(chunk) + print("API SHA 1: "+str(output[1])) + print("Our calculated SHA 1: "+str(h_sha1.hexdigest())) + if h_sha1.hexdigest() == output[1]: + print("sha1sum of downloaded file matches the sum that the API gave, jar is safe to use") + else: + raise MismatchSHA1Error # Determine which API parser to use: if args.api == "paperMC": diff --git a/parsers/modrinth.py b/parsers/modrinth.py index dbf407c..f27a060 100644 --- a/parsers/modrinth.py +++ b/parsers/modrinth.py @@ -94,27 +94,51 @@ def determine(project, whatToDetermine): #print(str(item)+" "+str(determine[item])) return determine -def getLatestVersion(project): +def getLatestVersion(project, **kwargs): print("Calling getLatestVersion()...") - versions = getVersions(project) + targetted_versions = kwargs.get('targetted_versions', None) + if targetted_versions != None: + versions = targetted_versions + else: + versions = getVersions(project) + print(versions) publishDates = determine(project, "date_published") #print(publishDates) # Get current date currentDate = pytz.utc.localize(datetime.datetime.utcnow()) #print(currentDate) convertedDates = {} - numberOfDates = len(publishDates) - for item in range(numberOfDates): + numberOfVersions = len(versions) + for item in range(numberOfVersions): convertTime = iso8601.parse_date(publishDates[item]) convertedDates[versions[item]] = convertTime shortestDate = {} - for item in range(numberOfDates): + for item in range(numberOfVersions): shortestDate[versions[item]] = currentDate - convertedDates[versions[item]] #print(shortestDate) # Sort the dictionary to find the most recent version latest = {key: val for key, val in sorted(shortestDate.items(), key = lambda ele: ele[1])} return list(latest.keys())[0] +def getLatestStable(project): + print("Calling getLatestStable()...") + versions = getVersions(project) + build_type = determine(project, "version_type") + # Build a dictionary that ties the versions to the build type + build_type_dict = {} + number_of_versions = len(versions) + for item in range(number_of_versions): + build_type_dict[versions[item]] = build_type[item] + print(build_type_dict) + # Sort dictionary to filter out only the release builds + stable = [] + for key, value in build_type_dict.items(): + if value == 'release': + print(key) + stable.append(key) + # Call getLatestVersion, tell it to use our output + return getLatestVersion(project, targetted_versions=stable) + def getDownloadURL(project, versionID): print("Calling getDownloadURL()...") versions = getVersions(project)