I broke some code here but it's fine, implemented getLatestVersion_by_number
This commit is contained in:
parent
702690aada
commit
0463ddc75f
2
mcUp.py
2
mcUp.py
@ -98,7 +98,7 @@ def modrinth(project, action, subAction):
|
||||
if subAction == "versions":
|
||||
print(parsers.modrinth.determine(project, "version_number"))
|
||||
if subAction == "latest":
|
||||
print(parsers.modrinth.getLatestVersion(project))
|
||||
print(parsers.modrinth.getLatestVersion_by_number(project, args.v))
|
||||
if subAction == "stable":
|
||||
print(parsers.modrinth.getLatestStable(project))
|
||||
if subAction == "get_URL":
|
||||
|
74
parsers/curseforge.py
Normal file
74
parsers/curseforge.py
Normal file
@ -0,0 +1,74 @@
|
||||
from request_wrapper import requests_wrapper as requests
|
||||
import re
|
||||
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
|
||||
# Setup session, this lets the parser re-use the connection instead of establishing a new connection for EACH request, not only does this cause a HUGE performance boost, it's also nicer to the API.
|
||||
session = requests.Session()
|
||||
base_api_url = "https://api.curseforge.com:443"
|
||||
minecraft_game_id='432'
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
'x-api-key': "$2a$10$Sh1qHpOdfdt/XKBbgTDnfeTfoAoNMskMf5smMQc3spQnK27Lmzvsq"
|
||||
}
|
||||
|
||||
def get_games():
|
||||
request = session.get(base_api_url+'/v1/games/'+minecraft_game_id, headers = headers)
|
||||
return request.json()
|
||||
|
||||
def search_mods(searchFilter):
|
||||
request = session.get(base_api_url+"/v1/mods/search", params={'gameId': minecraft_game_id, 'searchFilter': searchFilter}, headers = headers)
|
||||
return request.json()
|
||||
|
||||
def get_mod(mod_id):
|
||||
request = session.get(base_api_url+'/v1/mods/'+mod_id, headers = headers)
|
||||
return request.json()
|
||||
|
||||
# This function expects "modInfo" to be a list object
|
||||
def determine(modInfo, whatToDetermine):
|
||||
print("Calling determine()...")
|
||||
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 key_filter(project, dict_to_filter, key_to_grab, type_to_grab):
|
||||
print("Calling key_filter()...")
|
||||
versions = dict_to_filter
|
||||
build_type = determine(project, key_to_grab)
|
||||
# Build a dictionary that ties the versions to the build type
|
||||
build_type_dict = {}
|
||||
number_of_versions = len(versions)
|
||||
for item in range(number_of_versions):
|
||||
build_type_dict[versions[item]] = build_type[item]
|
||||
#print(build_type_dict)
|
||||
# Sort dictionary to filter out only the release builds
|
||||
stable = []
|
||||
print("Looking for "+str(type_to_grab))
|
||||
for key, value in build_type_dict.items():
|
||||
#print("looking at "+str(value))
|
||||
search = re.search(str(type_to_grab), str(value))
|
||||
if search != None:
|
||||
print("Match!")
|
||||
#print(key)
|
||||
stable.append(key)
|
||||
|
||||
# Call getLatestVersion, tell it to use our output
|
||||
return stable
|
||||
|
||||
def download(mod_id):
|
||||
modInfo = get_mod(mod_id)
|
||||
print(type(modInfo))
|
||||
modInfoList = []
|
||||
modInfoList.append(modInfo)
|
||||
data = determine(modInfoList, "data")
|
||||
print(key_filter(modInfo, data, "downloadUrl", ))
|
@ -123,52 +123,103 @@ def getLatestVersion(project, **kwargs):
|
||||
latest = {key: val for key, val in sorted(shortestDate.items(), key = lambda ele: ele[1])}
|
||||
return list(latest.keys())[0]
|
||||
|
||||
def key_filter(project, dict_to_filter, key_to_grab, type_to_grab):
|
||||
def key_filter(dict_to_filter, type_to_grab, **kwargs):
|
||||
print("Calling key_filter()...")
|
||||
versions = dict_to_filter
|
||||
build_type = determine(project, key_to_grab)
|
||||
return_both = kwargs.get('return_both', False)
|
||||
#versions = dict_to_filter
|
||||
#build_type = determine(project, key_to_grab)
|
||||
# Build a dictionary that ties the versions to the build type
|
||||
build_type_dict = {}
|
||||
number_of_versions = len(versions)
|
||||
for item in range(number_of_versions):
|
||||
build_type_dict[versions[item]] = build_type[item]
|
||||
#build_type_dict = {}
|
||||
#number_of_versions = len(versions)
|
||||
#for item in range(number_of_versions):
|
||||
# build_type_dict[versions[item]] = build_type[item]
|
||||
#print(build_type_dict)
|
||||
# Sort dictionary to filter out only the release builds
|
||||
stable = []
|
||||
#build_type_dict
|
||||
output_version = []
|
||||
output_query = []
|
||||
print("Looking for "+str(type_to_grab))
|
||||
for key, value in build_type_dict.items():
|
||||
for key, value in dict_to_filter.items():
|
||||
#print("looking at "+str(value))
|
||||
search = re.search(str(type_to_grab), str(value))
|
||||
if search != None:
|
||||
print("Match!")
|
||||
#print(key)
|
||||
stable.append(key)
|
||||
output_version.append(key)
|
||||
output_query.append(value)
|
||||
|
||||
# Call getLatestVersion, tell it to use our output
|
||||
return stable
|
||||
# Return output
|
||||
if return_both == False:
|
||||
return output_version
|
||||
elif return_both == True:
|
||||
#Tie the lists back together, and return it
|
||||
return_dict = {}
|
||||
return_list_count = len(key)
|
||||
for item in range(return_list_count):
|
||||
return_dict[output_query[item]] = output_version[item]
|
||||
return return_dict
|
||||
|
||||
def getLatestVersion_by_number(project, version, **kwargs):
|
||||
targetted_versions = kwargs.get('targetted_versions', None)
|
||||
if targetted_versions != None:
|
||||
versions = targetted_versions
|
||||
else:
|
||||
versions = getVersions(project)
|
||||
print("Calling getLatestVersion_by_number()...")
|
||||
version_number = determine(project, "version_number")
|
||||
print(version_number)
|
||||
print("Looking for latest build for Minecraft version: "+ str(version))
|
||||
version_numbers = {}
|
||||
number_of_versions = len(version_number)
|
||||
for item in range(number_of_versions):
|
||||
version_numbers[versions[item]] = version_number[item]
|
||||
print(version_numbers)
|
||||
# Ask the key_filter function to give us the builds that correspond to the requested version
|
||||
versions_to_target = key_filter(version_numbers, version, return_both=True)
|
||||
all_mc_mod_release = []
|
||||
for key, value in versions_to_target.items():
|
||||
mc_mod_release = re.split('-+', key)
|
||||
all_mc_mod_release.append(mc_mod_release)
|
||||
print(all_mc_mod_release)
|
||||
# Okay, take the last entry in the contents inside each mc_mod_release and output the higest value
|
||||
versions_to_parse_2 = []
|
||||
for item in range(len(all_mc_mod_release)):
|
||||
versions_to_parse = all_mc_mod_release[item]
|
||||
versions_to_parse_2.append(versions_to_parse[-1])
|
||||
latest_version = max(versions_to_parse_2)
|
||||
print(latest_version)
|
||||
#Finally, key_filter for the latest build version
|
||||
latest_version_id = key_filter(version_numbers, version+"-"+latest_version)
|
||||
return str(latest_version_id[0])
|
||||
|
||||
def getForMinecraftVersion(project, version, stability):
|
||||
print("Calling getForMinecraftVersion()...")
|
||||
print("Downloading",stability,"for Minecraft version", version)
|
||||
if stability == "stable":
|
||||
#Filter Game versions
|
||||
targetted_versions=key_filter(project, getVersions(project), "game_versions", version)
|
||||
targetted_versions=key_filter("game_versions", version)
|
||||
#Filter Stable versions
|
||||
stable_versions=key_filter(project, targetted_versions, "version_type", "release")
|
||||
result = getLatestVersion(project, targetted_versions=stable_versions)
|
||||
elif stability == "latest":
|
||||
result = getLatestVersion_by_number(project, version, targetted_versions=stable_versions)
|
||||
if stability == "latest":
|
||||
#Filter Game versions
|
||||
targetted_versions=key_filter(project, getVersions(project), "game_versions", version)
|
||||
#print(targetted_versions)
|
||||
#Filter Latest versions
|
||||
stable_versions=key_filter(project, targetted_versions, "version_type", "release")
|
||||
result = getLatestVersion(project, targetted_versions=stable_versions)
|
||||
targetted_versions=getVersions(project)
|
||||
stable_versions=(targetted_versions)
|
||||
result = getLatestVersion_by_number(project, version, targetted_versions=stable_versions)
|
||||
print("latest build for "+version+" is "+result)
|
||||
return result
|
||||
|
||||
def getLatestStable(project):
|
||||
print("Calling getLatestStable()...")
|
||||
return getLatestVersion(project, getVersions(project) ,targetted_versions=key_filter(project, "version_type", "release"))
|
||||
versions = getVersions(project)
|
||||
build_type = determine(project, "Release")
|
||||
# Build a dictionary that ties the versions to the build type
|
||||
build_type_dict = {}
|
||||
number_of_versions = len(versions)
|
||||
for item in range(number_of_versions):
|
||||
build_type_dict[versions[item]] = build_type[item]
|
||||
print(build_type_dict)
|
||||
return getLatestVersion_by_number(project, getVersions(project) ,targetted_versions=key_filter(project, getVersions(project), "version_type", "release"))
|
||||
|
||||
|
||||
def getDownloadURL(project, versionID):
|
||||
|
Loading…
Reference in New Issue
Block a user