def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'): if checkConfig().localPluginFolder == False: downloadPath = createTempPluginFolder() url = f"https://api.spiget.org/v2/resources/{ressourceId}" packageDetails = doAPIRequest(url) packageName = packageDetails["name"] packageNameNew = handleRegexPackageName(packageName) versionId = getVersionID(ressourceId, inputPackageVersion) packageVersion = getVersionName(ressourceId, versionId) packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}") if checkConfig().localPluginFolder: if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) else: downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) if not checkConfig().localPluginFolder: if inputPackageVersion is None or inputPackageVersion == 'latest': downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath) deleteTempPluginFolder(downloadPath) else: downloadSpecificVersion(ressourceId, downloadPackagePath, versionId) deleteTempPluginFolder(downloadPath)
def mainFunction(): consoleTitle() clearConsole() checkConfig() check_requirements() createInputLists() printMainMenu() getInput()
def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersion=None): if checkConfig().localPluginFolder == False: downloadPath = createTempPluginFolder() else: downloadPath = checkConfig().pathToPluginFolder helpPath = Path('/plugins') helpPathstr = str(helpPath) downloadPath = Path(str(downloadPath).replace(helpPathstr, '')) if mcVersion == None: if paperBuild == 'latest': mcVersion = '1.16.5' else: mcVersion = findBuildVersion(paperBuild) if installedServerjarName != None: mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName) if paperBuild == 'latest': paperBuild = findLatestBuildForVersion(mcVersion) 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) paperBuild = findLatestBuildForVersion(mcVersion) downloadFileName = getDownloadFileName(mcVersion, paperBuild) downloadPackagePath = Path(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 = calculateFileSizeMb(filesize) print(f"Downloadsize: {filesizeData} MB") print(f"File downloaded here: {downloadPackagePath}") if not checkConfig().localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_server_jar(sftpSession, downloadPackagePath) deleteTempPluginFolder(downloadPath) print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper-{paperBuild}" + \ oColors.brightGreen + " for " + oColors.standardWhite + f"{mcVersion}" + oColors.standardWhite)
def handleInput(inputCommand, inputSelectedObject, inputParams): while True: if inputCommand == 'get': if inputSelectedObject.isdigit(): if not checkConfig().localPluginFolder: if checkConfig().sftp_seperateDownloadPath is True: pluginPath = checkConfig( ).sftp_pathToSeperateDownloadPath else: pluginPath = checkConfig().sftp_folderPath getSpecificPackage(inputSelectedObject, pluginPath, inputParams) break else: if checkConfig().seperateDownloadPath is True: pluginPath = checkConfig().pathToSeperateDownloadPath else: pluginPath = checkConfig().pathToPluginFolder getSpecificPackage(inputSelectedObject, pluginPath, inputParams) break else: searchPackage(inputSelectedObject) break if inputCommand == 'update': if inputSelectedObject == 'serverjar': updateServerjar(inputParams) else: updateInstalledPackage(inputSelectedObject) break if inputCommand == 'check': if inputSelectedObject == 'serverjar': checkInstalledServerjar() else: checkInstalledPackage(inputSelectedObject) break if inputCommand == 'search': searchPackage(inputSelectedObject) break if inputCommand == 'exit': sys.exit() if inputCommand == 'help': if inputSelectedObject == 'command' or inputSelectedObject == 'commands': getCommandHelp(inputParams) else: getHelp() break if inputCommand == 'remove': removePlugin(inputSelectedObject) break if inputCommand == 'get-paper': papermc_downloader(inputSelectedObject, inputParams) break else: print(oColors.brightRed + "Error: Command not found. Please try again. :(" + oColors.standardWhite) getInput() getInput()
def createSFTPConnection(): cnopts = pysftp.CnOpts() cnopts.hostkeys = None # TODO fix this try: sftp = pysftp.Connection(checkConfig().sftp_server, username=checkConfig().sftp_user, \ password=checkConfig().sftp_password, port=checkConfig().sftp_port, cnopts=cnopts) except paramiko.ssh_exception.AuthenticationException: print(oColors.brightRed + "[SFTP]: Wrong Username/Password" + oColors.standardWhite) except paramiko.ssh_exception.SSHException: print(oColors.brightRed + "[SFTP]: The SFTP server isn't available." + oColors.standardWhite) try: return sftp except UnboundLocalError: print(oColors.brightRed + "[SFTP]: Check your config.ini!" + oColors.standardWhite) print(oColors.brightRed + "Exiting program..." + oColors.standardWhite) sys.exit()
def removePlugin(pluginToRemove): createPluginList() if not checkConfig().localPluginFolder: sftp = createSFTPConnection() pluginList = sftp_listAll(sftp) else: pluginList = os.listdir(checkConfig().pathToPluginFolder) i = 0 try: for plugin in pluginList: try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) except TypeError: continue pluginIdStr = str(pluginId) if pluginToRemove == pluginIdStr or re.search( pluginToRemove, fileName, re.IGNORECASE): print(f"Removing: {fileName}") if not checkConfig().localPluginFolder: pluginPath = checkConfig().sftp_folderPath pluginPath = f"{pluginPath}/{plugin}" sftp = createSFTPConnection() sftp.remove(pluginPath) print(f"Removed: {fileName}") i += 1 break else: pluginPath = checkConfig().pathToPluginFolder pluginPath = Path(f"{pluginPath}/{plugin}") os.remove(pluginPath) print(f"Removed: {fileName}") i += 1 break except TypeError: print(oColors.brightRed + f"Aborted removing of: {pluginToRemove}." + oColors.standardWhite) if i == 0: print(oColors.brightRed + f"Couldn't remove plugin: {pluginToRemove}" + oColors.standardWhite)
def eggCrackingJar(localJarFileName): if not checkConfig().localPluginFolder: pluginPath = checkConfig().sftp_folderPath else: pluginPath = checkConfig().pathToPluginFolder pathToPluginJar = Path(f"{pluginPath}/{localJarFileName}") pluginVersion = '' with ZipFile(pathToPluginJar, 'r') as pluginJar: try: with io.TextIOWrapper(pluginJar.open('plugin.yml', 'r'), encoding="utf-8") as pluginYml: pluginYmlContentLine = pluginYml.readlines() for line in pluginYmlContentLine: if "version: " in line: pluginVersion = line.replace('version: ', '') pluginVersion = pluginVersion.replace('\n', '') break except FileNotFoundError: pluginVersion = '' return pluginVersion
def createTempPluginFolder(): tempPluginFolder = Path("./TempSFTPUploadFolder") if not os.path.isdir(tempPluginFolder): try: os.mkdir(tempPluginFolder) except OSError: print(oColors.brightRed + "Creation of directory %s failed" % checkConfig().pathToPluginFolder) print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() return tempPluginFolder
def check_local_plugin_folder(): if checkConfig().localPluginFolder: if checkConfig().seperateDownloadPath: pluginFolderPath = checkConfig().pathToSeperateDownloadPath else: pluginFolderPath = checkConfig().pathToPluginFolder if not os.path.isdir(pluginFolderPath): print(oColors.brightRed + "Plugin folder coulnd*t be found. Creating one..." + oColors.standardWhite) try: os.mkdir(pluginFolderPath) except OSError: print(oColors.brightRed + "Creation of directory %s failed" % pluginFolderPath) print(oColors.brightRed + "Please check the config file!" + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() else: print("Created directory %s" % pluginFolderPath)
def searchPackage(ressourceName): url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name&sort=-downloads" packageName = doAPIRequest(url) i = 1 print(oColors.brightBlack + f"Searching: {ressourceName}" + oColors.standardWhite) print( "Index | Name | Description | Downloads" ) for ressource in packageName: pName = ressource["name"] newName = handleRegexPackageName(pName) pTag = ressource["tag"] pDownloads = ressource["downloads"] print(f" [{i}]".ljust(8), end='') print(f"{newName}".ljust(30), end='') print(f"{pTag}".ljust(120), end='') print(f"{pDownloads}".ljust(7)) i = i + 1 ressourceSelected = int( input("Select your wanted Ressource (Index)(0 to exit): ")) if ressourceSelected != 0: ressourceSelected = ressourceSelected - 1 ressourceId = packageName[ressourceSelected]["id"] if not checkConfig().localPluginFolder: try: getSpecificPackage(ressourceId, checkConfig().sftp_folderPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) else: try: getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
def checkInstalledServerjar(): if not checkConfig().localPluginFolder: sftp = createSFTPConnection() serverRootList = sftp_listFilesInServerRoot(sftp) else: serverRootList = os.path.dirname(checkConfig().pathToPluginFolder) 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 downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'): if versionID != 'latest': #url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionID}" print( oColors.brightRed + "Sorry but specific version downloads aren't supported because of cloudflare protection. :(" + oColors.standardWhite) print(oColors.brightRed + "Reverting to latest version." + oColors.standardWhite) 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 :( remotefile = urllib.request.urlopen(url) filesize = remotefile.info()['Content-Length'] urllib.request.urlretrieve(url, downloadPath) filesizeData = calculateFileSize(filesize) print(f"Downloadsize: {filesizeData} KB") print(f"File downloaded here: {downloadPath}") if not checkConfig().localPluginFolder: sftpSession = createSFTPConnection() sftp_upload_file(sftpSession, downloadPath)
def updateServerjar(serverJarBuild='latest'): try: if serverJarBuild == None: serverJarBuild = 'latest' if not checkConfig().localPluginFolder: sftp = createSFTPConnection() serverRootPath = checkConfig().sftp_folderPath serverRootPath = Path(str(serverRootPath).replace(r'/plugins', '')) serverRootList = sftp_listFilesInServerRoot(sftp) else: serverRoot = os.path.dirname(checkConfig().pathToPluginFolder) serverRootList = os.listdir(serverRoot) serverRootPath = checkConfig().pathToPluginFolder helpPath = Path('/plugins') helpPathstr = str(helpPath) serverRootPath = Path(str(serverRootPath).replace(helpPathstr, '')) installedServerjarFullName = None except FileNotFoundError: print(oColors.brightRed + "Path couldn't be found!" + oColors.standardWhite) print(oColors.brightRed + "Check your config!" + oColors.standardWhite) print(oColors.brightRed + "Aborting the process." + oColors.standardWhite) input("Press any key + enter to exit...") sys.exit() 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() serverJarPath = Path(f"{serverRootPath}/{installedServerjarFullName}") if 'paper' in installedServerjarFullName: print(f"Updating Paper to build: {serverJarBuild}") if not checkConfig().localPluginFolder: try: papermc_downloader(serverJarBuild, installedServerjarFullName) sftp.remove(serverJarPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) except FileNotFoundError: print(oColors.brightRed + "Error: Old serverjar file coulnd't be deleted" + oColors.standardWhite) else: try: papermc_downloader(serverJarBuild, installedServerjarFullName) os.remove(serverJarPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) except FileNotFoundError: print(oColors.brightRed + "Error: Old serverjar file coulnd't be deleted" + oColors.standardWhite) else: print(oColors.brightRed + f"{installedServerjarFullName} isn't supported.") print(oColors.brightRed + "Aborting the process." + oColors.standardWhite)
def check_requirements(): apiTest() check_local_plugin_folder() if not checkConfig().localPluginFolder: createSFTPConnection()
def checkInstalledPackage(inputSelectedObject="all"): createPluginList() if not checkConfig().localPluginFolder: sftp = createSFTPConnection() pluginList = sftp_listAll(sftp) else: pluginList = os.listdir(checkConfig().pathToPluginFolder) i = 0 oldPackages = 0 print(oColors.brightBlack + f"Checking: {inputSelectedObject}" + oColors.standardWhite) print( "┌─────┬────────────────────────────────┬──────────────┬───────────┬───────────────────┐" ) print( "│ No. │ Name │ Installed V. │ Latest V. │ Update available │" ) print( "└─────┴────────────────────────────────┴──────────────┴───────────┴───────────────────┘" ) try: for plugin in track(pluginList, description="Checking for updates", transient=True, complete_style="cyan"): try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) except TypeError: i += 1 continue pluginIdStr = str(pluginId) if fileVersion == '': fileVersion = 'N/A' try: pluginLatestVersion = INSTALLEDPLUGINLIST[i][2] except IndexError: pluginLatestVersion = 'N/A' if pluginLatestVersion == None: pluginLatestVersion = 'N/A' try: pluginIsOutdated = INSTALLEDPLUGINLIST[i][3] except IndexError: pluginIsOutdated = 'N/A' if pluginIsOutdated == None: pluginIsOutdated = 'N/A' if pluginIsOutdated == True: oldPackages = oldPackages + 1 if inputSelectedObject != "*" and inputSelectedObject != "all": if inputSelectedObject == pluginIdStr or re.search( inputSelectedObject, fileName, re.IGNORECASE): if pluginLatestVersion == 'N/A': print(oColors.brightBlack + f" [{1}]".ljust(8), end='') else: print(f" [{1}]".ljust(8), end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(15), end='') print(f"{pluginLatestVersion}".ljust(12), end='') print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite) break else: if pluginLatestVersion == 'N/A': print(oColors.brightBlack + f" [{i+1}]".ljust(8), end='') else: print(f" [{i+1}]".ljust(8), end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(15), end='') print(f"{pluginLatestVersion}".ljust(12), end='') print(f" {pluginIsOutdated}".ljust(5) + oColors.standardWhite) i += 1 except TypeError: print(oColors.brightRed + "Error occured: Aborted checking for updates." + oColors.standardWhite) print(oColors.brightYellow + f"Old packages: [{oldPackages}/{i}]" + oColors.standardWhite)
def updateInstalledPackage(inputSelectedObject='all'): createPluginList() if not checkConfig().localPluginFolder: sftp = createSFTPConnection() pluginList = sftp_listAll(sftp) else: pluginList = os.listdir(checkConfig().pathToPluginFolder) i = 0 pluginsUpdated = 0 indexNumberUpdated = 0 print(oColors.brightBlack + f"Updating: {inputSelectedObject}" + oColors.standardWhite) print("┌─────┬────────────────────────────────┬────────────┬──────────┐") print("│ No. │ Name │ Old V. │ New V. │") print("└─────┴────────────────────────────────┴────────────┴──────────┘") try: for plugin in track(pluginList, description="Updating", transient=True, complete_style="red"): try: fileName = getFileName(plugin) fileVersion = getFileVersion(plugin) pluginId = getInstalledPlugin(fileName, fileVersion) latestVersion = getLatestPluginVersion(pluginId) except TypeError: i += 1 continue except ValueError: i += 1 continue pluginIdStr = str(pluginId) if pluginId == None or pluginId == '': print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) continue if inputSelectedObject == pluginIdStr or re.search( inputSelectedObject, fileName, re.IGNORECASE): if INSTALLEDPLUGINLIST[i][3] == True: print(f" [{indexNumberUpdated+1}]".ljust(8), end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(8), end='') print(" ", end='') print(f"{latestVersion}".ljust(8)) if not checkConfig().localPluginFolder: if checkConfig().sftp_seperateDownloadPath is True: pluginPath = checkConfig( ).sftp_pathToSeperateDownloadPath else: pluginPath = checkConfig().sftp_folderPath pluginPath = Path(f"{pluginPath}/{plugin}") sftp = createSFTPConnection() indexNumberUpdated += 1 pluginsUpdated += 1 try: getSpecificPackage(pluginId, checkConfig().sftp_folderPath) if checkConfig( ).sftp_seperateDownloadPath is False: sftp.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) pluginsUpdated -= 1 except FileNotFoundError: print( oColors.brightRed + "Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: if checkConfig().seperateDownloadPath is True: pluginPath = checkConfig( ).pathToSeperateDownloadPath else: pluginPath = checkConfig().pathToPluginFolder pluginPath = Path(f"{pluginPath}/{plugin}") indexNumberUpdated += 1 pluginsUpdated += 1 try: getSpecificPackage( pluginId, checkConfig().pathToPluginFolder) if checkConfig().seperateDownloadPath is False: os.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) pluginsUpdated -= 1 except FileNotFoundError: print( oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) break else: print(f"{fileName} is already on {latestVersion}") print(oColors.brightRed + "Aborting the update process." + oColors.standardWhite) break if inputSelectedObject == 'all': if INSTALLEDPLUGINLIST[i][3] == True: print(f" [{indexNumberUpdated+1}]".ljust(8), end='') print(f"{fileName}".ljust(33), end='') print(f"{fileVersion}".ljust(8), end='') print(" ", end='') print(f"{latestVersion}".ljust(8)) if not checkConfig().localPluginFolder: if checkConfig().sftp_seperateDownloadPath is True: pluginPath = checkConfig( ).sftp_pathToSeperateDownloadPath else: pluginPath = checkConfig().sftp_folderPath pluginPath = f"{pluginPath}/{plugin}" sftp = createSFTPConnection() indexNumberUpdated += 1 pluginsUpdated += 1 try: getSpecificPackage(pluginId, checkConfig().sftp_folderPath) if checkConfig( ).sftp_seperateDownloadPath is False: sftp.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) pluginsUpdated -= 1 except FileNotFoundError: print( oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) else: if checkConfig().seperateDownloadPath is True: pluginPath = checkConfig( ).pathToSeperateDownloadPath else: pluginPath = checkConfig().pathToPluginFolder pluginPath = Path(f"{pluginPath}/{plugin}") indexNumberUpdated += 1 pluginsUpdated += 1 try: getSpecificPackage( pluginId, checkConfig().pathToPluginFolder) if checkConfig().seperateDownloadPath is False: os.remove(pluginPath) except HTTPError as err: print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite) pluginsUpdated -= 1 except FileNotFoundError: print( oColors.brightRed + f"Error: Old plugin file coulnd't be deleted" + oColors.standardWhite) i = i + 1 except TypeError: print(oColors.brightRed + "Error occured: Aborted updating for plugins." + oColors.standardWhite) print(oColors.brightYellow + f"[{pluginsUpdated}/{i}] Plugins updated" + oColors.standardWhite) if inputSelectedObject == 'all' and pluginsUpdated == 0: print(oColors.brightGreen + "All found plugins are on the latest version!" + oColors.standardWhite)