From 8212f66e9068a5a59379e1333291124bc61d5995 Mon Sep 17 00:00:00 2001 From: Neocky Date: Mon, 8 Mar 2021 02:56:00 +0100 Subject: [PATCH] New input system; Only one download function now; Specific plugin updater added and check for all installed plugins; Removed: - cloudscraper - unnecessary functions Added: - new input system; -> Now it is like a console - general download function - specific version updater - check for updates on all installed version --- requirements.txt | 5 +- src/consoleoutput.py | 2 +- src/handle_input.py | 67 +++++++++++++++++++------- src/plugin_downloader.py | 94 ++++++++++++------------------------- src/plugin_updatechecker.py | 77 ++++++++++++++++++++++++------ src/web_request.py | 6 --- 6 files changed, 143 insertions(+), 108 deletions(-) diff --git a/requirements.txt b/requirements.txt index 3232d14..5a21f8b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,2 @@ urllib3 >= 1.21.1 -requests >=2.25.1 -requests_toolbelt >= 0.9.1 -pyparsing >= 2.4.7 -cloudscraper >= 1.2.56 \ No newline at end of file +requests >=2.25.1 \ No newline at end of file diff --git a/src/consoleoutput.py b/src/consoleoutput.py index b2261f7..fb7709d 100644 --- a/src/consoleoutput.py +++ b/src/consoleoutput.py @@ -88,4 +88,4 @@ def printMainMenu(): #print(" [3] Search for a plugin") #print(" [4] Download latest version of package") #print(" [5] Check update for installed plugins") - print() \ No newline at end of file + #print() diff --git a/src/handle_input.py b/src/handle_input.py index 3d766b9..a1cbf6a 100644 --- a/src/handle_input.py +++ b/src/handle_input.py @@ -1,28 +1,58 @@ import time -from consoleoutput import consoleTitle, clearConsole, printMainMenu -from plugin_downloader import downloadPackageManual, apiCallTest, searchPackage, getLatestPackageVersionInteractive, getPackageVersion -from plugin_updatechecker import getInstalledPackages -from web_request import createCloudScraperInstance#, CLOUDSCRAPER +import sys +from consoleoutput import consoleTitle, clearConsole, printMainMenu, oColors +from plugin_downloader import downloadPackageManual, apiCallTest, searchPackage, getPackageVersion +from plugin_updatechecker import updateInstalledPackage, checkInstalledPackage +def createInputLists(): + global COMMANDLIST + COMMANDLIST = [ + 'get', + 'update', + 'check', + 'exit' + ] + global INPUTSELECTEDOBJECT + INPUTSELECTEDOBJECT = [ + 'all', + '*' + ] -def handleInput(inputCommand, inputSelectedObject, inputParams="latest"): - if inputCommand == 'get': - getPackageVersion(inputSelectedObject, inputParams, r"C:\\Users\USER\Desktop\\") +def handleInput(inputCommand, inputSelectedObject, inputParams): + while True: + if inputCommand == 'get': + getPackageVersion(r"C:\\Users\USER\Desktop\\", inputSelectedObject, inputParams) + break + if inputCommand == 'update': + #if inputSelectedObject in INPUTSELECTEDOBJECT: + updateInstalledPackage(r'C:\\Users\\USER\\Desktop\\plugins', inputSelectedObject) + break + if inputCommand == 'check': + checkInstalledPackage(r'C:\\Users\\USER\\Desktop\\plugins', inputSelectedObject) + break + if inputCommand == 'exit': + sys.exit() + else: + print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite) + getInput() + getInput() def getInput(): - inputCommand, inputSelectedObject, *inputParams = input("pluGET >> ").split() - inputParams = inputParams[0] if inputParams else '' + while True: + try: + inputCommand, inputSelectedObject, *inputParams = input("pluGET >> ").split() + break + except ValueError: + print(oColors.brightRed + "Wrong input! Use: > *command* *selectedObject* *optionalParams*" + oColors.standardWhite) + + inputParams = inputParams[0] if inputParams else None print(inputCommand) print(inputSelectedObject) print(inputParams) - handleInput(inputCommand, inputSelectedObject,inputParams) - - - - + handleInput(inputCommand, inputSelectedObject, inputParams) def inputOption(inputOptionString): @@ -48,12 +78,15 @@ def handleInputOLD(inputString): ressourceName = inputOption(3) searchPackage(ressourceName) if inputString == "4": - getLatestPackageVersionInteractive(r"C:\\Users\USER\Desktop\\") + #getLatestPackageVersionInteractive(r"C:\\Users\USER\Desktop\\") + print("4") if inputString == "5": - getInstalledPackages('C:\\Users\\USER\\Desktop\\plugins') + #updateAllInstalledPackages(r'C:\\Users\\USER\\Desktop\\plugins') + print("5") def inputMainMenu(): + createInputLists() clearConsole() printMainMenu() getInput() @@ -77,7 +110,7 @@ def outputTest(): input("Press key to end program...") -createCloudScraperInstance() +#createCloudScraperInstance() consoleTitle() inputMainMenu() outputTest() diff --git a/src/plugin_downloader.py b/src/plugin_downloader.py index ad4ff2e..fdc0f65 100644 --- a/src/plugin_downloader.py +++ b/src/plugin_downloader.py @@ -1,7 +1,6 @@ import urllib.request import cgi import re -import cloudscraper from web_request import doAPIRequest @@ -52,13 +51,12 @@ def handleRegexPackageName(packageNameFull): packageNameFull2 = packageNameFull.replace(unwantedpackageNameString, '') print(packageNameFull2) print("packageNameFull2") - # gets the real packagename "word1 & word2" is not supported only gets word 1 packageName = re.search(r'([a-zA-Z]\d*)+(\s?\-*\_*[a-zA-Z]\d*\+*\-*\'*)+', packageNameFull2) packageNameFullString = packageName.group() packageNameOnly = packageNameFullString.replace(' ', '') - print(packageNameOnly) - print("packageNameOnly") + #print(packageNameOnly) + #print("packageNameOnly") return packageNameOnly @@ -81,24 +79,25 @@ def apiCallTest(ressourceId): print(packageNameNew) -# check version -def compareVersions(): - #https://api.spiget.org/v2/resources/28140/versions/latest - # compare latest package version with installed package version - print("compareVersions") - - def getVersionID(packageId, packageVersion): + print(packageVersion) + if packageVersion == None or packageVersion == 'latest': + url = f"https://api.spiget.org/v2/resources/{packageId}/versions/latest" + response = doAPIRequest(url) + versionId = response["id"] + return versionId + url = f"https://api.spiget.org/v2/resources/{packageId}/versions?size=100&sort=-name" versionList = doAPIRequest(url) - + for packages in versionList: packageUpdate = packages["name"] versionId = packages["id"] if packageUpdate == packageVersion: return versionId return versionList[0]["id"] - + + def getVersionName(packageId, versionId): url = f"https://api.spiget.org/v2/resources/{packageId}/versions/{versionId}" response = doAPIRequest(url) @@ -128,74 +127,39 @@ def searchPackage(ressourceName): print(ressourceId) -def downloadLatestVersion(ressourceId, packageDownloadName, sourcePath): +def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): + print(versionID) + if versionID != 'latest': + #url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionID}" + print("Sorry but specific version downloads aren't supported because of cloudflare protection. :(") + url = f"https://api.spiget.org/v2/resources/{ressourceId}/download" + #url = f"https://api.spiget.org/v2/resources/{ressourceId}/versions/latest/download" #throws 403 forbidden error + remotefile = urllib.request.urlopen(url) filesize = remotefile.info()['Content-Length'] - downloadPath = sourcePath + packageDownloadName urllib.request.urlretrieve(url, downloadPath) filesizeData = calculateFileSize(filesize) - print(filesizeData) - print(filesize) - print(f" Downloadsize: {filesizeData} KB") - -def downloadSpecificVersion(ressourceId, packageDownloadName, versionId, sourcePath): - url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionId}" - #url = f"https://api.spiget.org/v2/resources/{ressourceId}/versions/{versionId}/download" - downloadPath = sourcePath + packageDownloadName + print(f"File downloaded here: {downloadPath}") + print(f"Downloadsize: {filesizeData} KB") - #local_filename = url.split('/')[-1] - # NOTE the stream=True parameter below - with CLOUDSCRAPER.get(url, stream=True) as r: - #r.raise_for_status() - with open(downloadPath, 'wb') as fd: - for chunk in r.iter_content(chunk_size=128): - fd.write(chunk) - #return downloadPath - - - #remotefile = urllib.request.urlopen(url) - #cloudscraper.requests.get() - #filesize = remotefile.info()['Content-Length'] - - #urllib.request.urlretrieve(url, downloadPath) - #filesizeData = calculateFileSize(filesize) - #print(filesizeData) - #print(filesize) - #print(f" Downloadsize: {filesizeData} KB") - - -def getPackageVersion(ressourceId, packageVersion, downloadPath): - #ressourceId = input(" SpigotMC Ressource ID: ") - CLOUDSCRAPER = createCloudScraperInstance() +def getPackageVersion(ressourceId, downloadPath, inputPackageVersion='latest'): url = f"https://api.spiget.org/v2/resources/{ressourceId}" packageDetails = doAPIRequest(url) packageName = packageDetails["name"] #packageTag = packageDetails["tag"] packageNameNew = handleRegexPackageName(packageName) - versionId = getVersionID(ressourceId, packageVersion) + versionId = getVersionID(ressourceId, inputPackageVersion) packageVersion = getVersionName(ressourceId, versionId) #packageVersion = getlatestVersion(ressourceId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" - downloadSpecificVersion(ressourceId, packageDownloadName, versionId, downloadPath) + downloadPackagePath = downloadPath + packageDownloadName - -def getLatestPackageVersionInteractive(downloadPath): - ressourceId = input(" SpigotMC Ressource ID: ") - url = f"https://api.spiget.org/v2/resources/{ressourceId}" - packageDetails = doAPIRequest(url) - packageName = packageDetails["name"] - #packageTag = packageDetails["tag"] - packageNameNew = handleRegexPackageName(packageName) - packageVersion = getlatestVersion(ressourceId) - packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" - downloadLatestVersion(ressourceId, packageDownloadName, downloadPath) - -def createCloudScraperInstance(): - global CLOUDSCRAPER - CLOUDSCRAPER = cloudscraper.create_scraper(interpreter='nodejs',debug=True) # returns a CloudScraper instance - return CLOUDSCRAPER + if inputPackageVersion is None or inputPackageVersion == 'latest': + downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) + else: + downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) # get latest update > https://api.spiget.org/v2/resources/28140/updates/latest # this also > https://api.spiget.org/v2/resources/28140/versions/latest diff --git a/src/plugin_updatechecker.py b/src/plugin_updatechecker.py index 1178625..a8290be 100644 --- a/src/plugin_updatechecker.py +++ b/src/plugin_updatechecker.py @@ -38,8 +38,6 @@ def compareVersions(pluginId, pluginVersion): url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest" latestUpdateSearch = doAPIRequest(url) versionLatestUpdate = latestUpdateSearch["name"] - print(pluginVersion) - print(versionLatestUpdate) if pluginVersion != versionLatestUpdate: plugin_is_outdated = True else: @@ -47,7 +45,48 @@ def compareVersions(pluginId, pluginVersion): return plugin_is_outdated -def getInstalledPackages(pluginFolderPath): +def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"): + #if inputSelectedObject is not ('all', '*'): + # print(oColors.brightRed + "Only *all* as selected object is supported!" + oColors.standardWhite) + # inputSelectedObject = 'all' + createPluginList() + pluginList = os.listdir(pluginFolderPath) + i = 0 + oldPackages = 0 + print("Index / Name / Installed Version / Update available") + + for plugin in pluginList: + fileName = getFileName(plugin) + fileVersion = getFileVersion(plugin) + pluginId = getInstalledPlugin(fileName, fileVersion) + pluginIdStr = str(pluginId) + + if fileVersion == '': + fileVersion = 'N/A' + + try: + pluginIsOutdated = INSTALLEDPLUGINLIST[i][2] + except IndexError: + pluginIsOutdated = 'N/A' + + if pluginIsOutdated == True: + oldPackages = oldPackages + 1 + + if inputSelectedObject != "*": + if inputSelectedObject != "all": + if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): + print(f"[{1}] {fileName} - {fileVersion} - {pluginIsOutdated}") + break + else: + print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way + else: + print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") + + i = i + 1 + print(f"Old packages: [{oldPackages}/{i}]") + + +def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'): createPluginList() pluginList = os.listdir(pluginFolderPath) print(pluginList) @@ -57,27 +96,38 @@ def getInstalledPackages(pluginFolderPath): fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) - + pluginIdStr = str(pluginId) print(f"name: {fileName}") print(f"version: {fileVersion}") - + # debug purpose + print(inputSelectedObject) print(INSTALLEDPLUGINLIST) - + print(f"pluginId: {pluginId}") + if pluginId == None: print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) continue - if INSTALLEDPLUGINLIST[i][2] == True: + + if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE): + print(f"Updating: {fileName}") os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}") - getLatestPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\") + getPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\") + break + + if inputSelectedObject == 'all': + if INSTALLEDPLUGINLIST[i][2] == True: + print("Deleting old plugin...") + os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}") + print("Downloading new plugin...") + getPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\") i = i + 1 - print(INSTALLEDPLUGINLIST[1][0]) + #print(INSTALLEDPLUGINLIST[1][0]) #getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\") def getInstalledPlugin(localFileName, localFileVersion): url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name" packageName = doAPIRequest(url) - print("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name") #packageName = response.json() i = 1 plugin_match_found = False @@ -87,7 +137,7 @@ def getInstalledPlugin(localFileName, localFileVersion): break pName = ressource["name"] pID = ressource["id"] - print(f" [{i}] {pName} - {pID}") + #print(f" [{i}] {pName} - {pID}") url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name" packageVersions = doAPIRequest(url2) for updates in packageVersions: @@ -98,9 +148,6 @@ def getInstalledPlugin(localFileName, localFileVersion): updateId = updates["id"] plugin_is_outdated = compareVersions(pID, updateVersion) addToPluginList(pID, updateId, plugin_is_outdated) - print(updateId) - print(pID) - print("Found match") break i = i + 1 return pluginID @@ -111,4 +158,4 @@ def getInstalledPlugin(localFileName, localFileVersion): # search with id for all version upates # get version that matches installed version # if match then download latest update - # else get second query \ No newline at end of file + # else get second query diff --git a/src/web_request.py b/src/web_request.py index 41cc479..fffe8f5 100644 --- a/src/web_request.py +++ b/src/web_request.py @@ -1,6 +1,5 @@ # Handles the web requests import requests -import cloudscraper #import urllib.request @@ -9,8 +8,3 @@ def doAPIRequest(url): response = requests.get(url, headers=headers) packageDetails = response.json() return packageDetails - -def createCloudScraperInstance(): - global CLOUDSCRAPER - CLOUDSCRAPER = cloudscraper.create_scraper() # returns a CloudScraper instance - return CLOUDSCRAPER \ No newline at end of file