Minecraft-Updater/parsers/modrinth.py

130 lines
4.8 KiB
Python
Raw Normal View History

2021-12-19 18:00:57 -06:00
from os import error
2021-12-20 09:38:59 -06:00
from types import WrapperDescriptorType
2021-12-19 23:42:31 -06:00
from pytz.tzinfo import memorized_timedelta
2021-12-20 09:38:59 -06:00
#import requests
import socket
2022-02-24 13:29:47 -06:00
import time
2021-12-20 09:38:59 -06:00
from request_wrapper import requests_wrapper as requests
2021-12-19 23:42:31 -06:00
import datetime
import iso8601
import pytz
2021-12-20 09:38:59 -06:00
debug = True
2021-12-19 18:00:57 -06:00
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
2021-12-20 09:38:59 -06:00
base_api_url = "https://api.modrinth.com:443/api/v1"
2022-02-24 13:29:47 -06:00
def failCheck(response, functOrigin):
2021-12-19 18:00:57 -06:00
print("Status Code is: "+str(response.status_code))
2022-02-24 13:29:47 -06:00
if response.status_code == 429:
print("Too many requests!")
time.sleep(int(response.headers["Retry-After"]))
print("Retrying "+functOrigin"...")
return True
elif response.status_code != 200:
2021-12-19 18:00:57 -06:00
raise error
# Data Caching
dataCache = {}
def cacheData(function_name, cached_data):
print("Caching data!")
dataCache[function_name] = cached_data
2021-12-20 09:38:59 -06:00
print("Stored "+function_name+"'s data to cache")
2021-12-19 18:00:57 -06:00
#
def modInfo(project):
print("Calling modInfo()...")
if "modInfo" in dataCache:
2021-12-20 09:38:59 -06:00
print("Returning cached data!")
2021-12-19 18:00:57 -06:00
return dataCache["modInfo"]
2021-12-20 09:38:59 -06:00
response = requests.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
2022-02-24 13:29:47 -06:00
if failCheck(response, "modInfo") == True: #Attempt to requery API
response = requests.get(base_api_url+"/mod/"+project, family=socket.AF_INET)
2021-12-19 18:00:57 -06:00
api_response = response.json()
cacheData("modInfo", api_response)
return api_response
def getVersions(project):
print("Calling getVersions()...")
if "getVersions" in dataCache:
2021-12-20 09:38:59 -06:00
print("Returning cached data!")
2021-12-19 18:00:57 -06:00
return dataCache["getVersions"]
workingDict = modInfo(project)
versions = workingDict["versions"]
2021-12-19 23:42:31 -06:00
cacheData("getVersions", versions)
2021-12-19 18:00:57 -06:00
return versions
def getAllModVersionInfo(project):
print("Calling getAllModVersionInfo()...")
if "getAllModVersionInfo" in dataCache:
2021-12-20 09:38:59 -06:00
print("Returning cached data!")
2021-12-19 18:00:57 -06:00
return dataCache["getAllMinecraftVersionInfo"]
versions = getVersions(project)
responseList = []
numberOfVersions = len(versions)
for item in range(numberOfVersions):
2021-12-20 09:38:59 -06:00
response = requests.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
2022-02-24 13:29:47 -06:00
if failCheck(response, "getAllModVersionInfo") == True: #Attempt to requery API
response = requests.get(base_api_url+"/version/"+versions[item], family=socket.AF_INET)
2021-12-19 18:00:57 -06:00
api_response = response.json()
responseList.append(api_response)
cacheData("getAllMinecraftVersionInfo", responseList)
return responseList
2021-12-19 23:42:31 -06:00
def determine(project, whatToDetermine):
2021-12-20 09:38:59 -06:00
print("Calling determine()...")
2021-12-19 18:00:57 -06:00
modInfo = getAllModVersionInfo(project)
numberOfVersions = len(modInfo)
2021-12-19 23:42:31 -06:00
determine = []
for item in range(numberOfVersions):
workingDict = modInfo[item]
determine.append(workingDict[whatToDetermine])
#print(str(item)+" "+str(determine[item]))
return determine
def getLatestVersion(project):
2021-12-20 09:38:59 -06:00
print("Calling getLatestVersion()...")
2021-12-19 23:42:31 -06:00
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]
2021-12-19 18:00:57 -06:00
2021-12-19 23:42:31 -06:00
def getDownloadURL(project, versionID):
2021-12-20 09:38:59 -06:00
print("Calling getDownloadURL()...")
2021-12-19 23:42:31 -06:00
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]]