Added getLatestStable() to modrinth parser

This commit is contained in:
2022-02-26 19:40:45 -06:00
parent 3693e23a73
commit aaa72fd6e6
2 changed files with 59 additions and 29 deletions

54
mcUp.py
View File

@@ -66,35 +66,41 @@ def modrinth(project, action, subAction):
print(parsers.modrinth.determine(project, "version_number")) print(parsers.modrinth.determine(project, "version_number"))
if subAction == "latest": if subAction == "latest":
print(parsers.modrinth.getLatestVersion(project)) print(parsers.modrinth.getLatestVersion(project))
if subAction == "stable":
print(parsers.modrinth.getLatestStable(project))
if subAction == "get_URL": if subAction == "get_URL":
print(parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project))) print(parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project)))
if action == "download": if action == "download":
if subAction == "stable":
download(project, parsers.modrinth.getLatestStable(project))
if subAction == "latest": if subAction == "latest":
latestVersion = parsers.modrinth.getLatestVersion(project) download(project, parsers.modrinth.getLatestVersion(project))
output = parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project)) def download(project, what):
response = requests.get(output[0], stream=True, timeout=1) versionToDownload = what
if args.o != None: # Check if user set an output filepath output = parsers.modrinth.getDownloadURL(project, what)
output_file = args.o response = requests.get(output[0], stream=True, timeout=1)
else: if args.o != None: # Check if user set an output filepath
output_file = output[2] output_file = args.o
with response as raw: else:
with open(output_file, 'wb') as file_object: output_file = output[2]
shutil.copyfileobj(raw.raw, file_object) with response as raw:
print("Downloaded "+latestVersion+" to "+output_file) with open(output_file, 'wb') as file_object:
#Calculate hash, compare with API given hash shutil.copyfileobj(raw.raw, file_object)
h_sha1 = hashlib.sha1() print("Downloaded "+versionToDownload+" to "+output_file)
with open(output_file, 'rb') as file_object: #Calculate hash, compare with API given hash
chunk = 0 h_sha1 = hashlib.sha1()
while chunk != b'': with open(output_file, 'rb') as file_object:
chunk = file_object.read(1024) chunk = 0
h_sha1.update(chunk) while chunk != b'':
print("API SHA 1: "+str(output[1])) chunk = file_object.read(1024)
print("Our calculated SHA 1: "+str(h_sha1.hexdigest())) h_sha1.update(chunk)
if h_sha1.hexdigest() == output[1]: print("API SHA 1: "+str(output[1]))
print("sha1sum of downloaded file matches the sum that the API gave, jar is safe to use") print("Our calculated SHA 1: "+str(h_sha1.hexdigest()))
else: if h_sha1.hexdigest() == output[1]:
raise MismatchSHA1Error 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: # Determine which API parser to use:
if args.api == "paperMC": if args.api == "paperMC":

View File

@@ -94,27 +94,51 @@ def determine(project, whatToDetermine):
#print(str(item)+" "+str(determine[item])) #print(str(item)+" "+str(determine[item]))
return determine return determine
def getLatestVersion(project): def getLatestVersion(project, **kwargs):
print("Calling getLatestVersion()...") 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") publishDates = determine(project, "date_published")
#print(publishDates) #print(publishDates)
# Get current date # Get current date
currentDate = pytz.utc.localize(datetime.datetime.utcnow()) currentDate = pytz.utc.localize(datetime.datetime.utcnow())
#print(currentDate) #print(currentDate)
convertedDates = {} convertedDates = {}
numberOfDates = len(publishDates) numberOfVersions = len(versions)
for item in range(numberOfDates): for item in range(numberOfVersions):
convertTime = iso8601.parse_date(publishDates[item]) convertTime = iso8601.parse_date(publishDates[item])
convertedDates[versions[item]] = convertTime convertedDates[versions[item]] = convertTime
shortestDate = {} shortestDate = {}
for item in range(numberOfDates): for item in range(numberOfVersions):
shortestDate[versions[item]] = currentDate - convertedDates[versions[item]] shortestDate[versions[item]] = currentDate - convertedDates[versions[item]]
#print(shortestDate) #print(shortestDate)
# Sort the dictionary to find the most recent version # Sort the dictionary to find the most recent version
latest = {key: val for key, val in sorted(shortestDate.items(), key = lambda ele: ele[1])} latest = {key: val for key, val in sorted(shortestDate.items(), key = lambda ele: ele[1])}
return list(latest.keys())[0] 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): def getDownloadURL(project, versionID):
print("Calling getDownloadURL()...") print("Calling getDownloadURL()...")
versions = getVersions(project) versions = getVersions(project)