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
This commit is contained in:
Neocky 2021-03-31 18:11:41 +02:00
parent 927767b0aa
commit 706b8049c3
6 changed files with 29 additions and 34 deletions

View File

@ -12,4 +12,5 @@ def mainFunction():
createInputLists() createInputLists()
printMainMenu() printMainMenu()
getInput() getInput()
mainFunction() mainFunction()

View File

@ -25,11 +25,6 @@ def ftp_showPlugins(ftp):
print(attr.filename, attr) print(attr.filename, attr)
def ftp_cdPluginDir(ftp):
configValues = configurationValues()
ftp.cwd(configValues.sftp_folderPath)
def ftp_upload_file(ftp, itemPath): def ftp_upload_file(ftp, itemPath):
configValues = configurationValues() configValues = configurationValues()
try: try:
@ -41,8 +36,8 @@ def ftp_upload_file(ftp, itemPath):
with open (itemPath, 'rb') as plugin_file: with open (itemPath, 'rb') as plugin_file:
ftp.storbinary('STOR '+ str(itemPath), plugin_file) ftp.storbinary('STOR '+ str(itemPath), plugin_file)
except FileNotFoundError: 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)
print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) print(oColors.brightRed + "[FTP]: Aborting uploading." + oColors.standardWhite)
os.chdir(currentDirectory) os.chdir(currentDirectory)
ftp.close() ftp.close()
@ -58,8 +53,8 @@ def ftp_upload_server_jar(ftp, itemPath):
with open (itemPath, 'rb') as server_jar: with open (itemPath, 'rb') as server_jar:
ftp.storbinary('STOR '+ str(itemPath), server_jar) ftp.storbinary('STOR '+ str(itemPath), server_jar)
except FileNotFoundError: 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)
print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) print(oColors.brightRed + "[FTP]: Aborting uploading." + oColors.standardWhite)
os.chdir(currentDirectory) os.chdir(currentDirectory)
ftp.close() ftp.close()
@ -70,12 +65,12 @@ def ftp_listAll(ftp):
ftp.cwd(configValues.sftp_folderPath) ftp.cwd(configValues.sftp_folderPath)
installedPlugins = ftp.nlst() installedPlugins = ftp.nlst()
except FileNotFoundError: 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: try:
return installedPlugins return installedPlugins
except UnboundLocalError: 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): def ftp_listFilesInServerRoot(ftp):
@ -83,12 +78,12 @@ def ftp_listFilesInServerRoot(ftp):
ftp.cwd('.') ftp.cwd('.')
filesInServerRoot = ftp.nlst() filesInServerRoot = ftp.nlst()
except FileNotFoundError: 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: try:
return filesInServerRoot return filesInServerRoot
except UnboundLocalError: 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): def ftp_downloadFile(ftp, downloadPath, fileToDownload):

View File

@ -2,7 +2,6 @@ import sys
import os import os
import pysftp import pysftp
import paramiko import paramiko
import ftplib
from utils.consoleoutput import oColors from utils.consoleoutput import oColors
from handlers.handle_config import configurationValues from handlers.handle_config import configurationValues
@ -33,18 +32,14 @@ def sftp_showPlugins(sftp):
print(attr.filename, attr) print(attr.filename, attr)
def sftp_cdPluginDir(sftp):
sftp.cd('plugins')
def sftp_upload_file(sftp, itemPath): def sftp_upload_file(sftp, itemPath):
try: try:
sftp.chdir('plugins') sftp.chdir('plugins')
sftp.put(itemPath) sftp.put(itemPath)
except FileNotFoundError: 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)
print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) print(oColors.brightRed + "[SFTP]: Aborting uploading." + oColors.standardWhite)
sftp.close() sftp.close()
@ -53,8 +48,8 @@ def sftp_upload_server_jar(sftp, itemPath):
sftp.chdir('.') sftp.chdir('.')
sftp.put(itemPath) sftp.put(itemPath)
except FileNotFoundError: 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)
print(oColors.brightRed + "Aborting uploading." + oColors.standardWhite) print(oColors.brightRed + "[SFTP]: Aborting uploading." + oColors.standardWhite)
sftp.close() sftp.close()
@ -63,24 +58,24 @@ def sftp_listAll(sftp):
sftp.chdir('plugins') sftp.chdir('plugins')
installedPlugins = sftp.listdir() installedPlugins = sftp.listdir()
except FileNotFoundError: 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: try:
return installedPlugins return installedPlugins
except UnboundLocalError: 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): def sftp_listFilesInServerRoot(sftp):
try: try:
filesInServerRoot = sftp.listdir() filesInServerRoot = sftp.listdir()
except FileNotFoundError: 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: try:
return filesInServerRoot return filesInServerRoot
except UnboundLocalError: 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): def sftp_downloadFile(sftp, downloadPath, fileToDownload):

View File

@ -8,7 +8,7 @@ from utils.web_request import doAPIRequest
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb
from handlers.handle_config import configurationValues from handlers.handle_config import configurationValues
from handlers.handle_sftp import sftp_upload_file, createSFTPConnection 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): 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}/download"
#url = f"https://api.spiget.org/v2/resources/{ressourceId}/versions/latest/download" #throws 403 forbidden error...cloudflare :( #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) remotefile = urllib.request.urlopen(url)
filesize = remotefile.info()['Content-Length'] filesize = remotefile.info()['Content-Length']
urllib.request.urlretrieve(url, downloadPath) urllib.request.urlretrieve(url, downloadPath)
@ -130,9 +134,9 @@ def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
versionId = getVersionID(ressourceId, inputPackageVersion) versionId = getVersionID(ressourceId, inputPackageVersion)
packageVersion = getVersionName(ressourceId, versionId) packageVersion = getVersionName(ressourceId, versionId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
if not configValues.localPluginFolder: #if not configValues.localPluginFolder:
downloadPackagePath = f"{downloadPath}/{packageDownloadName}" #downloadPackagePath = f"{downloadPath}/{packageDownloadName}"
else: #else:
downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}")
if inputPackageVersion is None or inputPackageVersion == 'latest': if inputPackageVersion is None or inputPackageVersion == 'latest':
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)

View File

@ -320,9 +320,9 @@ def updateInstalledPackage(inputSelectedObject='all'):
getSpecificPackage(pluginId, configValues.pathToPluginFolder) getSpecificPackage(pluginId, configValues.pathToPluginFolder)
if configValues.seperateDownloadPath is False: if configValues.seperateDownloadPath is False:
os.remove(pluginPath) os.remove(pluginPath)
#except HTTPError as err: except HTTPError as err:
# print(oColors.brightRed + f"HTTPError: {err.code} - {err.reason}" + oColors.standardWhite) print(oColors.brightRed + f"HTTPError: {err.code} - {err.reason}" + oColors.standardWhite)
# pluginsUpdated -= 1 pluginsUpdated -= 1
except FileNotFoundError: except FileNotFoundError:
print(oColors.brightRed + f"FileNotFoundError: Old plugin file coulnd't be deleted" + oColors.standardWhite) print(oColors.brightRed + f"FileNotFoundError: Old plugin file coulnd't be deleted" + oColors.standardWhite)

View File

@ -3,7 +3,7 @@ import requests
def doAPIRequest(url): def doAPIRequest(url):
headers = {'user-agent': 'pluGET'} headers = {'user-agent': 'pluGET/1.0'}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
packageDetails = response.json() packageDetails = response.json()
return packageDetails return packageDetails