From 706b8049c35084f0f820a1b92e33f913642ab936 Mon Sep 17 00:00:00 2001 From: Neocky Date: Wed, 31 Mar 2021 18:11:41 +0200 Subject: [PATCH] Added custom header to download & better ftp error messages Changes: - added custom header to plugin download - added better sftp/ftp error messages - removed unnecessary sftp/ftp functions --- src/__main__.py | 1 + src/handlers/handle_ftp.py | 21 ++++++++------------- src/handlers/handle_sftp.py | 21 ++++++++------------- src/plugin/plugin_downloader.py | 12 ++++++++---- src/plugin/plugin_updatechecker.py | 6 +++--- src/utils/web_request.py | 2 +- 6 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/__main__.py b/src/__main__.py index e6b9f08..77a4580 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -12,4 +12,5 @@ def mainFunction(): createInputLists() printMainMenu() getInput() + mainFunction() diff --git a/src/handlers/handle_ftp.py b/src/handlers/handle_ftp.py index 4d56f69..176c754 100644 --- a/src/handlers/handle_ftp.py +++ b/src/handlers/handle_ftp.py @@ -25,11 +25,6 @@ def ftp_showPlugins(ftp): print(attr.filename, attr) -def ftp_cdPluginDir(ftp): - configValues = configurationValues() - ftp.cwd(configValues.sftp_folderPath) - - def ftp_upload_file(ftp, itemPath): configValues = configurationValues() try: @@ -41,8 +36,8 @@ def ftp_upload_file(ftp, itemPath): with open (itemPath, 'rb') as plugin_file: ftp.storbinary('STOR '+ str(itemPath), plugin_file) except FileNotFoundError: - print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) - print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: Aborting uploading." + oColors.standardWhite) os.chdir(currentDirectory) ftp.close() @@ -58,8 +53,8 @@ def ftp_upload_server_jar(ftp, itemPath): with open (itemPath, 'rb') as server_jar: ftp.storbinary('STOR '+ str(itemPath), server_jar) except FileNotFoundError: - print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) - print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: Aborting uploading." + oColors.standardWhite) os.chdir(currentDirectory) ftp.close() @@ -70,12 +65,12 @@ def ftp_listAll(ftp): ftp.cwd(configValues.sftp_folderPath) installedPlugins = ftp.nlst() except FileNotFoundError: - print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) try: return installedPlugins except UnboundLocalError: - print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: No plugins were found." + oColors.standardWhite) def ftp_listFilesInServerRoot(ftp): @@ -83,12 +78,12 @@ def ftp_listFilesInServerRoot(ftp): ftp.cwd('.') filesInServerRoot = ftp.nlst() except FileNotFoundError: - print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) try: return filesInServerRoot except UnboundLocalError: - print(oColors.brightRed + "No Serverjar was found." + oColors.standardWhite) + print(oColors.brightRed + "[FTP]: No Serverjar was found." + oColors.standardWhite) def ftp_downloadFile(ftp, downloadPath, fileToDownload): diff --git a/src/handlers/handle_sftp.py b/src/handlers/handle_sftp.py index 3be570b..b2174aa 100644 --- a/src/handlers/handle_sftp.py +++ b/src/handlers/handle_sftp.py @@ -2,7 +2,6 @@ import sys import os import pysftp import paramiko -import ftplib from utils.consoleoutput import oColors from handlers.handle_config import configurationValues @@ -33,18 +32,14 @@ def sftp_showPlugins(sftp): print(attr.filename, attr) -def sftp_cdPluginDir(sftp): - sftp.cd('plugins') - - def sftp_upload_file(sftp, itemPath): try: sftp.chdir('plugins') sftp.put(itemPath) except FileNotFoundError: - print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) - print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: Aborting uploading." + oColors.standardWhite) sftp.close() @@ -53,8 +48,8 @@ def sftp_upload_server_jar(sftp, itemPath): sftp.chdir('.') sftp.put(itemPath) except FileNotFoundError: - print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) - print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: Aborting uploading." + oColors.standardWhite) sftp.close() @@ -63,24 +58,24 @@ def sftp_listAll(sftp): sftp.chdir('plugins') installedPlugins = sftp.listdir() except FileNotFoundError: - print(oColors.brightRed + "The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite) try: return installedPlugins except UnboundLocalError: - print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: No plugins were found." + oColors.standardWhite) def sftp_listFilesInServerRoot(sftp): try: filesInServerRoot = sftp.listdir() except FileNotFoundError: - print(oColors.brightRed + "The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite) try: return filesInServerRoot except UnboundLocalError: - print(oColors.brightRed + "No Serverjar was found." + oColors.standardWhite) + print(oColors.brightRed + "[SFTP]: No Serverjar was found." + oColors.standardWhite) def sftp_downloadFile(sftp, downloadPath, fileToDownload): diff --git a/src/plugin/plugin_downloader.py b/src/plugin/plugin_downloader.py index 1131ed8..0ae4bd9 100644 --- a/src/plugin/plugin_downloader.py +++ b/src/plugin/plugin_downloader.py @@ -8,7 +8,7 @@ from utils.web_request import doAPIRequest from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb from handlers.handle_config import configurationValues from handlers.handle_sftp import sftp_upload_file, createSFTPConnection -from handlers.handle_ftp import ftp_upload_file, ftp_cdPluginDir, createFTPConnection +from handlers.handle_ftp import ftp_upload_file, createFTPConnection def handleRegexPackageName(packageNameFull): @@ -99,6 +99,10 @@ def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): 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...cloudflare :( + urrlib_opener = urllib.request.build_opener() + urrlib_opener.addheaders = [('User-agent', 'pluGET/1.0')] + urllib.request.install_opener(urrlib_opener) + remotefile = urllib.request.urlopen(url) filesize = remotefile.info()['Content-Length'] urllib.request.urlretrieve(url, downloadPath) @@ -130,9 +134,9 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): versionId = getVersionID(ressourceId, inputPackageVersion) packageVersion = getVersionName(ressourceId, versionId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" - if not configValues.localPluginFolder: - downloadPackagePath = f"{downloadPath}/{packageDownloadName}" - else: + #if not configValues.localPluginFolder: + #downloadPackagePath = f"{downloadPath}/{packageDownloadName}" + #else: downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) diff --git a/src/plugin/plugin_updatechecker.py b/src/plugin/plugin_updatechecker.py index db2a945..f7a230c 100644 --- a/src/plugin/plugin_updatechecker.py +++ b/src/plugin/plugin_updatechecker.py @@ -320,9 +320,9 @@ def updateInstalledPackage(inputSelectedObject='all'): getSpecificPackage(pluginId, configValues.pathToPluginFolder) if configValues.seperateDownloadPath is False: os.remove(pluginPath) - #except HTTPError as err: - # print(oColors.brightRed + f"HTTPError: {err.code} - {err.reason}" + oColors.standardWhite) - # pluginsUpdated -= 1 + except HTTPError as err: + print(oColors.brightRed + f"HTTPError: {err.code} - {err.reason}" + oColors.standardWhite) + pluginsUpdated -= 1 except FileNotFoundError: print(oColors.brightRed + f"FileNotFoundError: Old plugin file coulnd't be deleted" + oColors.standardWhite) diff --git a/src/utils/web_request.py b/src/utils/web_request.py index b947ab6..e4e6652 100644 --- a/src/utils/web_request.py +++ b/src/utils/web_request.py @@ -3,7 +3,7 @@ import requests def doAPIRequest(url): - headers = {'user-agent': 'pluGET'} + headers = {'user-agent': 'pluGET/1.0'} response = requests.get(url, headers=headers) packageDetails = response.json() return packageDetails