Fixed bug that prevented getallmodVersionInfo() from caching data, also made HTTP 429 handling more intelligent
This commit is contained in:
parent
3d61098f9e
commit
3693e23a73
9
mcUp.py
9
mcUp.py
@ -1,5 +1,6 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
# Setup Parser
|
# Setup Parser
|
||||||
|
from ssl import HAS_ECDH
|
||||||
import requests
|
import requests
|
||||||
#import socket
|
#import socket
|
||||||
#from request_wrapper import requests_wrapper as requests
|
#from request_wrapper import requests_wrapper as requests
|
||||||
@ -10,7 +11,9 @@ import argparse
|
|||||||
import shutil
|
import shutil
|
||||||
import hashlib
|
import hashlib
|
||||||
from os import error
|
from os import error
|
||||||
from urllib.parse import unquote
|
# Define Errors:
|
||||||
|
class MismatchSHA1Error(Exception):
|
||||||
|
pass
|
||||||
parser = argparse.ArgumentParser(description='A command-line tool to update a Minecraft Server.')
|
parser = argparse.ArgumentParser(description='A command-line tool to update a Minecraft Server.')
|
||||||
parser.add_argument('api', metavar='api', help='which API to use')
|
parser.add_argument('api', metavar='api', help='which API to use')
|
||||||
parser.add_argument('project', metavar='project', help='which project to query for')
|
parser.add_argument('project', metavar='project', help='which project to query for')
|
||||||
@ -86,10 +89,12 @@ def modrinth(project, action, subAction):
|
|||||||
while chunk != b'':
|
while chunk != b'':
|
||||||
chunk = file_object.read(1024)
|
chunk = file_object.read(1024)
|
||||||
h_sha1.update(chunk)
|
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]:
|
if h_sha1.hexdigest() == output[1]:
|
||||||
print("sha1sum of downloaded file matches the sum that the API gave, jar is safe to use")
|
print("sha1sum of downloaded file matches the sum that the API gave, jar is safe to use")
|
||||||
else:
|
else:
|
||||||
raise error
|
raise MismatchSHA1Error
|
||||||
|
|
||||||
# Determine which API parser to use:
|
# Determine which API parser to use:
|
||||||
if args.api == "paperMC":
|
if args.api == "paperMC":
|
||||||
|
@ -25,8 +25,10 @@ base_api_url = "https://api.modrinth.com:443/api/v1"
|
|||||||
def failCheck(response, functOrigin):
|
def failCheck(response, functOrigin):
|
||||||
print("Status Code is: "+str(response.status_code))
|
print("Status Code is: "+str(response.status_code))
|
||||||
if response.status_code == 429:
|
if response.status_code == 429:
|
||||||
print("Too many requests!"+'\n'+"Waiting for 30 seconds...")
|
sleep_time = int(response.headers["X-Ratelimit-Reset"])+1
|
||||||
time.sleep(30) # Wait 30 seconds
|
print("Too many requests!"+'\n'+"Waiting for "+str(sleep_time)+" seconds...")
|
||||||
|
print(response.headers)
|
||||||
|
time.sleep(sleep_time) # Wait until API ratelimit is over
|
||||||
print("Retrying "+functOrigin+"...")
|
print("Retrying "+functOrigin+"...")
|
||||||
return True
|
return True
|
||||||
elif response.status_code != 200:
|
elif response.status_code != 200:
|
||||||
@ -36,13 +38,15 @@ dataCache = {}
|
|||||||
def cacheData(function_name, cached_data):
|
def cacheData(function_name, cached_data):
|
||||||
print("Caching data!")
|
print("Caching data!")
|
||||||
dataCache[function_name] = cached_data
|
dataCache[function_name] = cached_data
|
||||||
|
#print(dataCache)
|
||||||
print("Stored "+function_name+"'s data to cache")
|
print("Stored "+function_name+"'s data to cache")
|
||||||
#
|
|
||||||
def modInfo(project):
|
def modInfo(project):
|
||||||
print("Calling modInfo()...")
|
print("Calling modInfo()...")
|
||||||
if "modInfo" in dataCache:
|
if "modInfo" in dataCache:
|
||||||
print("Returning cached data!")
|
print("Returning cached data!")
|
||||||
return dataCache["modInfo"]
|
return dataCache["modInfo"]
|
||||||
|
else:
|
||||||
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||||
if failCheck(response, "modInfo") == True: #Attempt to requery API
|
if failCheck(response, "modInfo") == True: #Attempt to requery API
|
||||||
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||||
@ -55,6 +59,7 @@ def getVersions(project):
|
|||||||
if "getVersions" in dataCache:
|
if "getVersions" in dataCache:
|
||||||
print("Returning cached data!")
|
print("Returning cached data!")
|
||||||
return dataCache["getVersions"]
|
return dataCache["getVersions"]
|
||||||
|
else:
|
||||||
workingDict = modInfo(project)
|
workingDict = modInfo(project)
|
||||||
versions = workingDict["versions"]
|
versions = workingDict["versions"]
|
||||||
cacheData("getVersions", versions)
|
cacheData("getVersions", versions)
|
||||||
@ -64,7 +69,8 @@ def getAllModVersionInfo(project):
|
|||||||
print("Calling getAllModVersionInfo()...")
|
print("Calling getAllModVersionInfo()...")
|
||||||
if "getAllModVersionInfo" in dataCache:
|
if "getAllModVersionInfo" in dataCache:
|
||||||
print("Returning cached data!")
|
print("Returning cached data!")
|
||||||
return dataCache["getAllMinecraftVersionInfo"]
|
return dataCache["getAllModVersionInfo"]
|
||||||
|
else:
|
||||||
versions = getVersions(project)
|
versions = getVersions(project)
|
||||||
responseList = []
|
responseList = []
|
||||||
numberOfVersions = len(versions)
|
numberOfVersions = len(versions)
|
||||||
@ -74,7 +80,7 @@ def getAllModVersionInfo(project):
|
|||||||
response = session.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
response = session.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
||||||
api_response = response.json()
|
api_response = response.json()
|
||||||
responseList.append(api_response)
|
responseList.append(api_response)
|
||||||
cacheData("getAllMinecraftVersionInfo", responseList)
|
cacheData("getAllModVersionInfo", responseList)
|
||||||
return responseList
|
return responseList
|
||||||
|
|
||||||
def determine(project, whatToDetermine):
|
def determine(project, whatToDetermine):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user