diff --git a/mcUp.py b/mcUp.py index 8b78663..f75682c 100755 --- a/mcUp.py +++ b/mcUp.py @@ -1,6 +1,7 @@ #!/bin/python # Setup Parser import parsers.paperMC +import parsers.modrinth import argparse import shutil import hashlib @@ -46,10 +47,20 @@ def paperMC(project, action, subAction): print("sha256sum of downloaded file matches the sum that the API gave, jar is safe to use") else: raise error +# modrinth command functions +def modrinth(project, action, subAction): + if action == "list": + if subAction == "info": + print(parsers.modrinth.modInfo(project)) + if subAction == "versions": + print(parsers.modrinth.getAllModVersionInfo(project)) + -# Determine which API to use: +# Determine which API parser to use: if args.api == "paperMC": paperMC(args.project, args.action, args.subAction) +elif args.api == "modrinth": + modrinth(args.project, args.action, args.subAction) else: print("Error: Unknown API: "+args.api) \ No newline at end of file diff --git a/parsers/modrinth.py b/parsers/modrinth.py new file mode 100644 index 0000000..b7b6cf7 --- /dev/null +++ b/parsers/modrinth.py @@ -0,0 +1,63 @@ +from os import error +import 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://api.modrinth.com/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 +# +def modInfo(project): + print("Calling modInfo()...") + if "modInfo" in dataCache: + return dataCache["modInfo"] + response = requests.get(base_api_url+"/mod/"+project) + failCheck(response) + api_response = response.json() + cacheData("modInfo", api_response) + return api_response + +def getVersions(project): + print("Calling getVersions()...") + if "getVersions" in dataCache: + return dataCache["getVersions"] + workingDict = modInfo(project) + versions = workingDict["versions"] + print(versions) + return versions + +def getAllModVersionInfo(project): + print("Calling getAllModVersionInfo()...") + if "getAllModVersionInfo" in dataCache: + return dataCache["getAllMinecraftVersionInfo"] + versions = getVersions(project) + responseList = [] + numberOfVersions = len(versions) + for item in range(numberOfVersions): + response = requests.get(base_api_url+"/version/"+versions[item]) + api_response = response.json() + responseList.append(api_response) + cacheData("getAllMinecraftVersionInfo", responseList) + return responseList + +def determineLatestMinecraftVersion(project): + modInfo = getAllModVersionInfo(project) + numberOfVersions = len(modInfo) + gameVersions = {} + #for item in range(numberOfVersions): + #gameVersions = +