from os import error #import requests import socket from request_wrapper import requests_wrapper as requests debug = True if debug == True: import logging from requests import api logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True timeoutTime = 1 base_api_url = "https://papermc.io:443/api/v2" def failCheck(response): print("Status Code is: "+str(response.status_code)) if response.status_code != 200: raise error # Data Caching dataCache = {} def cacheData(function_name, cached_data): print("Caching data!") dataCache[function_name] = cached_data print("Stored "+function_name+"'s data to cache") # This function returns the versions from a project from the Paper API. def getVersions(project): print("Calling getVersions()...") if "getVersions" in dataCache: return dataCache["getVersions"] response = requests.get(base_api_url+"/projects/"+project, family=socket.AF_INET, timeout=timeoutTime) failCheck(response) api_response = response.json() cacheData("getVersions", api_response['versions']) return api_response['versions'] def getLatest(project): print("Calling getLatest()...") versions = getVersions(project) return versions[-1] def getBuildNumber(project, version): print("Calling getBuildNumber()...") if "getBuildNumber" in dataCache: return dataCache["getBuildNumber"] response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version, family=socket.AF_INET, timeout=timeoutTime) failCheck(response) api_response = response.json() cacheData("getBuildNumber", api_response['builds']) return api_response['builds'] def getBuildInfo(project, version): print("Calling getBuildInfo()...") if "getBuildInfo" in dataCache: return dataCache["getBuildInfo"] buildID1 = getBuildNumber(project, version) buildID = str(buildID1[-1]) response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID, family=socket.AF_INET, timeout=timeoutTime) failCheck(response) api_response = response.json() cacheData("getBuildInfo", api_response) return api_response def getJarInfo(project, version): print("Calling getJarInfo()...") buildInfo = getBuildInfo(project, version) workingDict = buildInfo["downloads"] workingDict2 = workingDict["application"] return workingDict2 def downloadVersion(project, version): print("Calling downloadVersion()...") jarName = getJarInfo(project, version) buildID1 = getBuildNumber(project, version) buildID = str(buildID1[-1]) response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID+"/downloads/"+jarName["name"], family=socket.AF_INET, stream=True, timeout=timeoutTime) failCheck(response) return response