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
|
||||
# Setup Parser
|
||||
from ssl import HAS_ECDH
|
||||
import requests
|
||||
#import socket
|
||||
#from request_wrapper import requests_wrapper as requests
|
||||
@ -10,7 +11,9 @@ import argparse
|
||||
import shutil
|
||||
import hashlib
|
||||
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.add_argument('api', metavar='api', help='which API to use')
|
||||
parser.add_argument('project', metavar='project', help='which project to query for')
|
||||
@ -86,10 +89,12 @@ def modrinth(project, action, subAction):
|
||||
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 error
|
||||
raise MismatchSHA1Error
|
||||
|
||||
# Determine which API parser to use:
|
||||
if args.api == "paperMC":
|
||||
|
@ -25,8 +25,10 @@ base_api_url = "https://api.modrinth.com:443/api/v1"
|
||||
def failCheck(response, functOrigin):
|
||||
print("Status Code is: "+str(response.status_code))
|
||||
if response.status_code == 429:
|
||||
print("Too many requests!"+'\n'+"Waiting for 30 seconds...")
|
||||
time.sleep(30) # Wait 30 seconds
|
||||
sleep_time = int(response.headers["X-Ratelimit-Reset"])+1
|
||||
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+"...")
|
||||
return True
|
||||
elif response.status_code != 200:
|
||||
@ -36,46 +38,50 @@ dataCache = {}
|
||||
def cacheData(function_name, cached_data):
|
||||
print("Caching data!")
|
||||
dataCache[function_name] = cached_data
|
||||
#print(dataCache)
|
||||
print("Stored "+function_name+"'s data to cache")
|
||||
#
|
||||
|
||||
def modInfo(project):
|
||||
print("Calling modInfo()...")
|
||||
if "modInfo" in dataCache:
|
||||
print("Returning cached data!")
|
||||
return dataCache["modInfo"]
|
||||
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||
if failCheck(response, "modInfo") == True: #Attempt to requery API
|
||||
else:
|
||||
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||
api_response = response.json()
|
||||
cacheData("modInfo", api_response)
|
||||
return api_response
|
||||
if failCheck(response, "modInfo") == True: #Attempt to requery API
|
||||
response = session.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
|
||||
api_response = response.json()
|
||||
cacheData("modInfo", api_response)
|
||||
return api_response
|
||||
|
||||
def getVersions(project):
|
||||
print("Calling getVersions()...")
|
||||
if "getVersions" in dataCache:
|
||||
print("Returning cached data!")
|
||||
return dataCache["getVersions"]
|
||||
workingDict = modInfo(project)
|
||||
versions = workingDict["versions"]
|
||||
cacheData("getVersions", versions)
|
||||
return versions
|
||||
else:
|
||||
workingDict = modInfo(project)
|
||||
versions = workingDict["versions"]
|
||||
cacheData("getVersions", versions)
|
||||
return versions
|
||||
|
||||
def getAllModVersionInfo(project):
|
||||
print("Calling getAllModVersionInfo()...")
|
||||
if "getAllModVersionInfo" in dataCache:
|
||||
print("Returning cached data!")
|
||||
return dataCache["getAllMinecraftVersionInfo"]
|
||||
versions = getVersions(project)
|
||||
responseList = []
|
||||
numberOfVersions = len(versions)
|
||||
for item in range(numberOfVersions):
|
||||
response = session.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
||||
if failCheck(response, "getAllModVersionInfo") == True: #Attempt to requery API
|
||||
return dataCache["getAllModVersionInfo"]
|
||||
else:
|
||||
versions = getVersions(project)
|
||||
responseList = []
|
||||
numberOfVersions = len(versions)
|
||||
for item in range(numberOfVersions):
|
||||
response = session.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
||||
api_response = response.json()
|
||||
responseList.append(api_response)
|
||||
cacheData("getAllMinecraftVersionInfo", responseList)
|
||||
return responseList
|
||||
if failCheck(response, "getAllModVersionInfo") == True: #Attempt to requery API
|
||||
response = session.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
|
||||
api_response = response.json()
|
||||
responseList.append(api_response)
|
||||
cacheData("getAllModVersionInfo", responseList)
|
||||
return responseList
|
||||
|
||||
def determine(project, whatToDetermine):
|
||||
print("Calling determine()...")
|
||||
|
Loading…
x
Reference in New Issue
Block a user