from os import error from types import WrapperDescriptorType from pytz.tzinfo import memorized_timedelta #import requests import socket from request_wrapper import requests_wrapper as requests import datetime import iso8601 import pytz 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://api.modrinth.com:443/api/v1" 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") # def modInfo(project): print("Calling modInfo()...") if "modInfo" in dataCache: print("Returning cached data!") return dataCache["modInfo"] response = requests.get(base_api_url+"/mod/"+project, family=socket.AF_INET) failCheck(response) 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 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 = requests.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET) failCheck(response) api_response = response.json() responseList.append(api_response) cacheData("getAllMinecraftVersionInfo", responseList) return responseList def determine(project, whatToDetermine): print("Calling determine()...") modInfo = getAllModVersionInfo(project) numberOfVersions = len(modInfo) determine = [] for item in range(numberOfVersions): workingDict = modInfo[item] determine.append(workingDict[whatToDetermine]) #print(str(item)+" "+str(determine[item])) return determine def getLatestVersion(project): print("Calling getLatestVersion()...") versions = getVersions(project) 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): convertTime = iso8601.parse_date(publishDates[item]) convertedDates[versions[item]] = convertTime shortestDate = {} for item in range(numberOfDates): 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 getDownloadURL(project, versionID): print("Calling getDownloadURL()...") versions = getVersions(project) versionInfo = getAllModVersionInfo(project) downloadURLs = {} downloadSHA1 = {} downloadFilenames = {} # Iterate through the nested lists/dicts for item in range(len(versions)): workingDict = versionInfo[item] #print("workingDict: "+str(workingDict)) workingList = workingDict["files"] #print("workingList: "+str(workingList)) workingDict2 = workingList[0] workingDict3 = workingDict2["hashes"] #print(workingDict3) downloadURLs[versions[item]] = workingDict2["url"] downloadSHA1[versions[item]] = workingDict3["sha1"] downloadFilenames[versions[item]] = workingDict2["filename"] #print(downloadURLs) return [downloadURLs[versionID], downloadSHA1[versionID], downloadFilenames[versionID]]