diff --git a/src/handlers/handle_input.py b/src/handlers/handle_input.py index 1fb3cda..8cd6983 100644 --- a/src/handlers/handle_input.py +++ b/src/handlers/handle_input.py @@ -6,6 +6,9 @@ from handlers.handle_config import checkConfig from plugin.plugin_downloader import searchPackage, getSpecificPackage from plugin.plugin_updatechecker import updateInstalledPackage, checkInstalledPackage from plugin.plugin_remover import removePlugin +from serverjar.serverjar_checker import checkInstalledServerjar, updateServerjar +from serverjar.serverjar_paper import papermc_downloader + def createInputLists(): global COMMANDLIST @@ -16,7 +19,8 @@ def createInputLists(): 'search', 'exit', 'help', - 'remove' + 'remove', + 'get-paper' ] global INPUTSELECTEDOBJECT INPUTSELECTEDOBJECT = [ @@ -47,10 +51,16 @@ def handleInput(inputCommand, inputSelectedObject, inputParams): searchPackage(inputSelectedObject) break if inputCommand == 'update': - updateInstalledPackage(inputSelectedObject) + if inputSelectedObject == 'serverjar': + updateServerjar(inputParams) + else: + updateInstalledPackage(inputSelectedObject) break if inputCommand == 'check': - checkInstalledPackage(inputSelectedObject) + if inputSelectedObject == 'serverjar': + checkInstalledServerjar() + else: + checkInstalledPackage(inputSelectedObject) break if inputCommand == 'search': searchPackage(inputSelectedObject) @@ -63,6 +73,9 @@ def handleInput(inputCommand, inputSelectedObject, inputParams): if inputCommand == 'remove': removePlugin(inputSelectedObject) break + if inputCommand == 'get-paper': + papermc_downloader(inputSelectedObject, inputParams) + break else: print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite) getInput() diff --git a/src/handlers/handle_sftp.py b/src/handlers/handle_sftp.py index 8049959..aeebe07 100644 --- a/src/handlers/handle_sftp.py +++ b/src/handlers/handle_sftp.py @@ -45,6 +45,17 @@ def sftp_upload_file(sftp, itemPath): sftp.close() +def sftp_upload_server_jar(sftp, itemPath): + try: + sftp.chdir('.') + 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 installation." + oColors.standardWhite) + sftp.close() + + def sftp_listAll(sftp): try: sftp.chdir('plugins') @@ -56,4 +67,18 @@ def sftp_listAll(sftp): try: return installedPlugins except UnboundLocalError: - print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) \ No newline at end of file + print(oColors.brightRed + "No plugins were found." + oColors.standardWhite) + + +def sftp_listFilesInServerRoot(sftp): + try: + #sftp.chdir('plugins') + filesInServerRoot = sftp.listdir() + + except FileNotFoundError: + print(oColors.brightRed + "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) \ No newline at end of file diff --git a/src/serverjar/serverjar_checker.py b/src/serverjar/serverjar_checker.py new file mode 100644 index 0000000..cd153f1 --- /dev/null +++ b/src/serverjar/serverjar_checker.py @@ -0,0 +1,85 @@ +import os +import sys +from urllib.error import HTTPError + +from handlers.handle_sftp import createSFTPConnection, sftp_listFilesInServerRoot +from handlers.handle_config import checkConfig +from utils.consoleoutput import oColors +from serverjar.serverjar_paper import paperCheckForUpdate, papermc_downloader + + +def checkInstalledServerjar(): + if not checkConfig().localPluginFolder: + sftp = createSFTPConnection() + serverRootList = sftp_listFilesInServerRoot(sftp) + else: + serverRootList = os.path.dirname(checkConfig().pathToPluginFolder) + os.chdir('..') + serverRootList = os.listdir(serverRootList) + installedServerjarFullName = None + try: + for files in serverRootList: + try: + if '.jar' in files: + installedServerjarFullName = files + break + except TypeError: + continue + except TypeError: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + + if installedServerjarFullName == None: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + input("Press any key + enter to exit...") + sys.exit() + + if 'paper' in installedServerjarFullName: + paperCheckForUpdate(installedServerjarFullName) + + else: + print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.") + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + + +def updateServerjar(serverJarBuild='latest'): + if serverJarBuild == None: + serverJarBuild = 'latest' + if not checkConfig().localPluginFolder: + sftp = createSFTPConnection() + serverRootList = sftp_listFilesInServerRoot(sftp) + else: + serverRoot = os.path.dirname(checkConfig().pathToPluginFolder) + os.chdir('..') + serverRootList = os.listdir(serverRoot) + installedServerjarFullName = None + try: + for files in serverRootList: + try: + if '.jar' in files: + installedServerjarFullName = files + break + except TypeError: + continue + except TypeError: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + + if installedServerjarFullName == None: + print(oColors.brightRed + "Serverjar couldn't be found." + oColors.standardWhite) + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + input("Press any key + enter to exit...") + sys.exit() + + if 'paper' in installedServerjarFullName: + print(serverJarBuild) + try: + papermc_downloader(serverJarBuild, installedServerjarFullName) + except HTTPError as err: + print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) + + else: + print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.") + print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) + \ No newline at end of file diff --git a/src/serverjar/serverjar_paper.py b/src/serverjar/serverjar_paper.py index dc56407..d170958 100644 --- a/src/serverjar/serverjar_paper.py +++ b/src/serverjar/serverjar_paper.py @@ -1,32 +1,142 @@ +import os import sys +import re +import urllib.request from utils.consoleoutput import oColors from utils.web_request import doAPIRequest -from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection +from handlers.handle_sftp import sftp_upload_server_jar, sftp_cdPluginDir, createSFTPConnection +from handlers.handle_config import checkConfig +from utils.utilities import createTempPluginFolder, deleteTempPluginFolder +from plugin.plugin_downloader import calculateFileSize + + +# = 1.16.5 def getInstalledPaperMinecraftVersion(localPaperName): - print("test") + mcVersionFull = re.search(r'(\d*\.*\d)+', localPaperName) + try: + mcVersion = mcVersionFull.group() + except AttributeError: + mcVersion = mcVersionFull + return mcVersion + +# = 550 def getInstalledPaperVersion(localPaperName): - print("test") + paperBuildFull = re.search(r'([\d]*.jar)', localPaperName) + try: + paperBuild = paperBuildFull.group() + except AttributeError: + paperBuild = paperBuildFull + paperBuild = paperBuild.replace('.jar', '') + return paperBuild + + +def findVersionGroup(mcVersion): + versionGroups = ['1.16', '1.15'] + versionGroupFound = False + for versionGroup in versionGroups: + if versionGroupFound == True: + break + url = f"https://papermc.io/api/v2/projects/paper/version_group/{versionGroup}/builds" + papermcdetails = doAPIRequest(url) + papermcVersionForMc = papermcdetails["versions"] + for versions in papermcVersionForMc: + if versions == mcVersion: + versionGroupFound = True + paperVersionGroup = versionGroup + break + if versionGroup == mcVersion: + versionGroupFound = True + paperVersionGroup = versionGroup + print(versionGroup) + break + + return paperVersionGroup + + +def findLatestBuild(paperVersionGroup): + url = f"https://papermc.io/api/v2/projects/paper/version_group/{paperVersionGroup}/builds" + papermcbuilds = doAPIRequest(url) + latestPaperBuild = papermcbuilds["builds"][-1]["build"] + return latestPaperBuild + + +def versionBehind(installedPaperBuild, latestPaperBuild): + installedPaperBuildint = int(installedPaperBuild) + latestPaperBuildint = int(latestPaperBuild) + versionsBehind = latestPaperBuildint - installedPaperBuildint + return versionsBehind + + + +def getDownloadFileName(paperMcVersion, paperBuild): + url = f"https://papermc.io/api/v2/projects/paper/versions/{paperMcVersion}/builds/{paperBuild}" + buildDetails = doAPIRequest(url) + downloadName = buildDetails["downloads"]["application"]["name"] + return downloadName + + +def paperCheckForUpdate(installedServerjarFullName): + mcVersion = getInstalledPaperMinecraftVersion(installedServerjarFullName) + paperInstalledBuild = getInstalledPaperVersion(installedServerjarFullName) + versionGroup = findVersionGroup(mcVersion) + paperLatestBuild = findLatestBuild(versionGroup) + paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild) + + print(f"Paper for {mcVersion}") + print("Index | Name | Old V. | New V. | Versions behind ") + print(f" [1]".ljust(8), end='') + print(f"paper".ljust(24), end='') + print(f"{paperInstalledBuild}".ljust(8), end='') + print(" ", end='') + print(f"{paperLatestBuild}".ljust(8), end='') + print(" ", end='') + print(f"{paperVersionBehind}".ljust(8)) + # https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/ -def papermc_downloader(paperBuild, paperVersionGroup='1.16'): - url = f"https://papermc.io/api/v2/projects/paper/{serverVersion}/{inputPackageVersion}" # v1 is deprecated - #https://papermc.io/api/v2/projects/paper/version_group/1.16/builds get all builds for all 1.16 versions - #https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450 gets file name - # input serverversion = 1.16.5 inputpackageversion = 450 - # regex 1.16 % build url - # search for 450 in version group 1.16 - # if found get file name from builds/450 - # build url & download package - # https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/450/downloads/paper-1.16.5-450.jar +def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersion=None): + if checkConfig().localPluginFolder == False: + downloadPath = createTempPluginFolder() + else: + downloadPath = checkConfig().pathToPluginFolder + downloadPath = downloadPath.replace(r'\plugins', '') + if mcVersion == None: + mcVersion = '1.16.5' + + if installedServerjarName != None: + mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName) - papermcdetails = doAPIRequest(url) - errorExists = papermcdetails["error"] - if errorExists in papermcdetails: - print(f"PaperMc version: {inputPackageVersion} couldn't be found") - print("Aborting the download of PaperMc.") - input("Press any key + enter to exit...") - sys.exit() + if paperBuild == 'latest': + versionGroup = findVersionGroup(mcVersion) + paperBuild = findLatestBuild(versionGroup) + try: + downloadFileName = getDownloadFileName(mcVersion, paperBuild) + except KeyError: + print(oColors.brightRed + f"This version wasn't found for {mcVersion}" + oColors.standardWhite) + print(oColors.brightRed + f"Reverting to latest version for {mcVersion}" + oColors.standardWhite) + versionGroup = findVersionGroup(mcVersion) + paperBuild = findLatestBuild(versionGroup) + downloadFileName = getDownloadFileName(mcVersion, paperBuild) + + downloadPackagePath = f"{downloadPath}\\{downloadFileName}" + + if checkConfig().localPluginFolder == False: + downloadPath = createTempPluginFolder() + + url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}/builds/{paperBuild}/downloads/{downloadFileName}" + remotefile = urllib.request.urlopen(url) + filesize = remotefile.info()['Content-Length'] + print(f"Starting paper-{paperBuild} download for {mcVersion}...") + urllib.request.urlretrieve(url, downloadPackagePath) + filesizeData = calculateFileSize(filesize) + + print(f"Downloadsize: {filesizeData} KB") + print(f"File downloaded here: {downloadPackagePath}") + if not checkConfig().localPluginFolder: + sftpSession = createSFTPConnection() + sftp_upload_server_jar(sftpSession, downloadPackagePath) + deleteTempPluginFolder(downloadPath) diff --git a/src/utils/utilities.py b/src/utils/utilities.py index d6b503a..5190bb2 100644 --- a/src/utils/utilities.py +++ b/src/utils/utilities.py @@ -59,7 +59,7 @@ def check_requirements(): def createTempPluginFolder(): - tempPluginFolder = ".\\plugins" + tempPluginFolder = ".\\TempSFTPUploadFolder" if not os.path.isdir(tempPluginFolder): try: os.mkdir(tempPluginFolder)