From f44f06a803eee97d06af87abebb935d1556b90f9 Mon Sep 17 00:00:00 2001 From: Neocky Date: Fri, 5 Mar 2021 00:09:03 +0100 Subject: [PATCH] Fixed input handling and added web_request file to simplify the code --- pluGET/handle_input.py | 19 ++++++------ pluGET/plugin_downloader.py | 54 ++++++++++++++++++---------------- pluGET/plugin_updatechecker.py | 21 +++++++------ pluGET/web_request.py | 9 ++++++ 4 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 pluGET/web_request.py diff --git a/pluGET/handle_input.py b/pluGET/handle_input.py index 7e4dac7..f120d7b 100644 --- a/pluGET/handle_input.py +++ b/pluGET/handle_input.py @@ -5,16 +5,16 @@ from plugin_updatechecker import getInstalledPackages def inputOption(inputOptionString): - if inputOptionString == "1": - ressourceID = input("SpigotMC Ressource ID: ") - return ressourceID - if inputOptionString == "2": - ressourceId = input("SpigotMC Ressource ID: ") - return ressourceId - if inputOptionString == "3": - ressourceName = input(" SpigotMC Ressource Name: ") + inputString = None + print(inputOptionString) + if inputOptionString == 1: + inputString = input("SpigotMC Ressource ID: ") + if inputOptionString == 2: + inputString = input("SpigotMC Ressource ID: ") + if inputOptionString == 3: + inputString = input(" SpigotMC Ressource Name: ") print("ich bin ein test") - return ressourceName + return inputString def handleInput(inputString): @@ -39,7 +39,6 @@ def inputMainMenu(): handleInput(inputSt) - def outputTest(): print("Hello world") print("Waiting still seconds: 5", end='\r') diff --git a/pluGET/plugin_downloader.py b/pluGET/plugin_downloader.py index c4e330b..72d7b56 100644 --- a/pluGET/plugin_downloader.py +++ b/pluGET/plugin_downloader.py @@ -1,13 +1,13 @@ import urllib.request import cgi import re -import requests +from web_request import doAPIRequest def calculateFileSize(downloadFileSize): fileSizeDownload = int(downloadFileSize) - fileSizeMb = fileSizeDownload / 1024 / 1024 - roundedFileSize = round(fileSizeMb, 2) + fileSizeKb = fileSizeDownload / 1024 + roundedFileSize = round(fileSizeKb, 2) return roundedFileSize @@ -30,7 +30,7 @@ def downloadPackageManual(): filename = params["filename"] # creating file path - path = r"C:\\Users\USER\Desktop\\" + path = r"C:\\Users\Jan-Luca\Desktop\\" ppath = path + filename # download file @@ -61,18 +61,18 @@ def handleRegexPackageName(packageNameFull): return packageNameOnly def getlatestVersion(packageId): - response = requests.get(f"https://api.spiget.org/v2/resources/{packageId}/versions/latest") - #packageDetails = response.json() - packageVersion = response.json()["name"] + url = f"https://api.spiget.org/v2/resources/{packageId}/versions/latest" + response = doAPIRequest(url) + packageVersion = response["name"] return packageVersion def apiCallTest(ressourceId): - response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}") - packageDetails = response.json() - print(packageDetails) - packageName = response.json()["name"] - packageTag = response.json()["tag"] + url = f"https://api.spiget.org/v2/resources/{ressourceId}" + response = doAPIRequest(url) + print(response) + packageName = response["name"] + packageTag = response["tag"] print(packageName) print(packageTag) packageNameNew = handleRegexPackageName(packageName) @@ -87,21 +87,23 @@ def compareVersions(): def searchPackage(ressourceName): - response = requests.get(f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name") - #https://api.spiget.org/v2/search/resources/luckperms?field=name - print(f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name") - packageName = response.json() + url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name" + packageName = doAPIRequest(url) + print(url) i = 1 + print("Index / Name / Description / Downloads") for ressource in packageName: pName = ressource["name"] - print(f" [{i}] {pName}") + pTag = ressource["tag"] + pDownloads = ressource["downloads"] + print(f" [{i}] {pName} / {pTag}/ {pDownloads}") i = i + 1 ressourceSelected = int(input(" Select your wanted Ressource: ")) ressourceSelected = ressourceSelected - 1 - fileInfo = response.json()[ressourceSelected]["file"] + fileInfo = packageName[ressourceSelected]["file"] packageUrl = fileInfo["url"] - ressourceId = response.json()[ressourceSelected]["id"] + ressourceId = packageName[ressourceSelected]["id"] print(packageUrl) print(ressourceId) @@ -115,15 +117,15 @@ def downloadLatestVersion(ressourceId, packageDownloadName, sourcePath): filesizeData = calculateFileSize(filesize) print(filesizeData) print(filesize) - print(f" Downloadsize: {filesizeData} MB") + print(f" Downloadsize: {filesizeData} KB") def getLatestPackageVersion(ressourceId, downloadPath): #ressourceId = input(" SpigotMC Ressource ID: ") - response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}") - packageDetails = response.json() + url = f"https://api.spiget.org/v2/resources/{ressourceId}" + packageDetails = doAPIRequest(url) packageName = packageDetails["name"] - packageTag = packageDetails["tag"] + #packageTag = packageDetails["tag"] packageNameNew = handleRegexPackageName(packageName) packageVersion = getlatestVersion(ressourceId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" @@ -132,10 +134,10 @@ def getLatestPackageVersion(ressourceId, downloadPath): def getLatestPackageVersionInteractive(downloadPath): ressourceId = input(" SpigotMC Ressource ID: ") - response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}") - packageDetails = response.json() + url = f"https://api.spiget.org/v2/resources/{ressourceId}" + packageDetails = doAPIRequest(url) packageName = packageDetails["name"] - packageTag = packageDetails["tag"] + #packageTag = packageDetails["tag"] packageNameNew = handleRegexPackageName(packageName) packageVersion = getlatestVersion(ressourceId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" diff --git a/pluGET/plugin_updatechecker.py b/pluGET/plugin_updatechecker.py index 7ceca09..63d0bff 100644 --- a/pluGET/plugin_updatechecker.py +++ b/pluGET/plugin_updatechecker.py @@ -1,9 +1,9 @@ import os import re -import requests from consoleoutput import oColors #consoleTitle, clearConsole from plugin_downloader import getLatestPackageVersion #handleInput +from web_request import doAPIRequest def createPluginList(): @@ -35,8 +35,8 @@ def getFileVersion(pluginName): def compareVersions(pluginId, pluginVersion): - responseUpdateSearch = requests.get(f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest") - latestUpdateSearch = responseUpdateSearch.json() + url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest" + latestUpdateSearch = doAPIRequest(url) versionLatestUpdate = latestUpdateSearch["name"] print(pluginVersion) print(versionLatestUpdate) @@ -67,17 +67,20 @@ def getInstalledPackages(pluginFolderPath): print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) continue if INSTALLEDPLUGINLIST[i][2] == True: + os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}") getLatestPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\") - os.remove(f"C:\\Users\USER\\Desktop\\plugins\\{plugin}") + i = i + 1 print(INSTALLEDPLUGINLIST[1][0]) #getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\") def getInstalledPlugin(localFileName, localFileVersion): - response = requests.get("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name") + #response = requests.get("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name") + url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name" + packageName = doAPIRequest(url) #https://api.spiget.org/v2/search/resources/luckperms?field=name print("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name") - packageName = response.json() + #packageName = response.json() i = 1 plugin_match_found = False pluginID = None @@ -87,8 +90,8 @@ def getInstalledPlugin(localFileName, localFileVersion): pName = ressource["name"] pID = ressource["id"] print(f" [{i}] {pName} - {pID}") - response2 = requests.get(f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name") - packageVersions = response2.json() + url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name" + packageVersions = doAPIRequest(url2) for updates in packageVersions: updateVersion = updates["name"] if localFileVersion == updateVersion: @@ -105,7 +108,7 @@ def getInstalledPlugin(localFileName, localFileVersion): return pluginID - # start query + # start query # get id # search with id for all version upates # get version that matches installed version diff --git a/pluGET/web_request.py b/pluGET/web_request.py new file mode 100644 index 0000000..9690137 --- /dev/null +++ b/pluGET/web_request.py @@ -0,0 +1,9 @@ +import requests +#import urllib.request + + +def doAPIRequest(url): + headers = {'user-agent': 'pluGET'} + response = requests.get(url, headers=headers) + packageDetails = response.json() + return packageDetails