2021-03-15 15:26:00 +00:00
|
|
|
import os
|
2021-03-15 00:20:18 +00:00
|
|
|
import sys
|
2021-03-15 15:26:00 +00:00
|
|
|
import re
|
|
|
|
import urllib.request
|
2021-03-16 00:04:13 +00:00
|
|
|
from pathlib import Path
|
2021-03-27 17:58:16 +00:00
|
|
|
from rich.console import Console
|
2021-03-15 00:20:18 +00:00
|
|
|
|
|
|
|
from utils.consoleoutput import oColors
|
|
|
|
from utils.web_request import doAPIRequest
|
2021-03-30 21:55:04 +00:00
|
|
|
from handlers.handle_sftp import createSFTPConnection, sftp_upload_server_jar
|
|
|
|
from handlers.handle_ftp import createFTPConnection, ftp_upload_server_jar
|
2021-03-27 11:07:19 +00:00
|
|
|
from handlers.handle_config import configurationValues
|
2021-03-16 00:04:13 +00:00
|
|
|
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeMb
|
2021-03-15 15:26:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
# = 1.16.5
|
2021-03-15 00:20:18 +00:00
|
|
|
def getInstalledPaperMinecraftVersion(localPaperName):
|
2021-05-09 05:33:05 +00:00
|
|
|
if localPaperName is None:
|
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
mcVersionFull = re.search(r'(\d*\.*\d)+', localPaperName)
|
|
|
|
try:
|
|
|
|
mcVersion = mcVersionFull.group()
|
|
|
|
except AttributeError:
|
|
|
|
mcVersion = mcVersionFull
|
|
|
|
return mcVersion
|
|
|
|
|
2021-03-15 00:20:18 +00:00
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
# = 550
|
2021-03-15 00:20:18 +00:00
|
|
|
def getInstalledPaperVersion(localPaperName):
|
2021-05-09 05:33:05 +00:00
|
|
|
if localPaperName is None:
|
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
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']
|
2021-05-09 05:33:05 +00:00
|
|
|
if mcVersion is None:
|
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
for versionGroup in versionGroups:
|
|
|
|
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:
|
|
|
|
paperVersionGroup = versionGroup
|
2021-05-09 05:33:05 +00:00
|
|
|
return paperVersionGroup
|
2021-03-15 15:26:00 +00:00
|
|
|
if versionGroup == mcVersion:
|
|
|
|
paperVersionGroup = versionGroup
|
2021-05-09 05:33:05 +00:00
|
|
|
return paperVersionGroup
|
|
|
|
return False # Not found
|
2021-03-15 15:26:00 +00:00
|
|
|
|
|
|
|
|
2021-03-16 00:04:13 +00:00
|
|
|
def findBuildVersion(wantedPaperBuild):
|
|
|
|
versionGroups = ['1.16', '1.15']
|
2021-05-09 05:33:05 +00:00
|
|
|
if wantedPaperBuild is None:
|
|
|
|
return False
|
2021-03-16 00:04:13 +00:00
|
|
|
for versionGroup in versionGroups:
|
|
|
|
url = f"https://papermc.io/api/v2/projects/paper/version_group/{versionGroup}/builds"
|
|
|
|
papermcdetails = doAPIRequest(url)
|
|
|
|
paperMcBuilds = papermcdetails["builds"]
|
|
|
|
for build in paperMcBuilds:
|
|
|
|
paperBuild = str(build["build"])
|
|
|
|
if paperBuild == wantedPaperBuild:
|
|
|
|
paperVersionGroup = build["version"]
|
2021-05-09 05:33:05 +00:00
|
|
|
return paperVersionGroup
|
|
|
|
return False # Not found
|
2021-03-16 00:04:13 +00:00
|
|
|
|
2021-05-15 23:05:29 +00:00
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
def findLatestBuild(paperVersionGroup):
|
2021-05-09 05:33:05 +00:00
|
|
|
if paperVersionGroup is None:
|
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
url = f"https://papermc.io/api/v2/projects/paper/version_group/{paperVersionGroup}/builds"
|
|
|
|
papermcbuilds = doAPIRequest(url)
|
2021-05-15 23:02:09 +00:00
|
|
|
if "status" in papermcbuilds: # Checks if the API returns a status. This means that there was an error.
|
2021-05-09 05:33:05 +00:00
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
latestPaperBuild = papermcbuilds["builds"][-1]["build"]
|
|
|
|
return latestPaperBuild
|
|
|
|
|
|
|
|
|
2021-03-16 00:04:13 +00:00
|
|
|
def findLatestBuildForVersion(mcVersion):
|
2021-05-09 05:33:05 +00:00
|
|
|
if mcVersion is None:
|
|
|
|
return False
|
2021-03-16 00:04:13 +00:00
|
|
|
url = f"https://papermc.io/api/v2/projects/paper/versions/{mcVersion}"
|
|
|
|
papermcbuilds = doAPIRequest(url)
|
|
|
|
latestPaperBuild = papermcbuilds["builds"][-1]
|
|
|
|
return latestPaperBuild
|
|
|
|
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
def versionBehind(installedPaperBuild, latestPaperBuild):
|
2021-05-09 05:33:05 +00:00
|
|
|
if installedPaperBuild or latestPaperBuild is None:
|
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
installedPaperBuildint = int(installedPaperBuild)
|
|
|
|
latestPaperBuildint = int(latestPaperBuild)
|
|
|
|
versionsBehind = latestPaperBuildint - installedPaperBuildint
|
|
|
|
return versionsBehind
|
|
|
|
|
|
|
|
|
|
|
|
def getDownloadFileName(paperMcVersion, paperBuild):
|
2021-05-09 05:33:05 +00:00
|
|
|
if paperMcVersion or paperBuild is None:
|
|
|
|
return False
|
2021-03-15 15:26:00 +00:00
|
|
|
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)
|
2021-05-09 05:33:05 +00:00
|
|
|
|
|
|
|
# Report an error if getInstalledPaperMinecraftVersion encountered an issue.
|
|
|
|
if not mcVersion:
|
|
|
|
print(oColors.brightRed + f"ERR: An error was encountered while detecting the server's Minecraft version." +
|
|
|
|
oColors.standardWhite)
|
|
|
|
return False
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
paperInstalledBuild = getInstalledPaperVersion(installedServerjarFullName)
|
2021-05-09 05:33:05 +00:00
|
|
|
|
|
|
|
# Report an error if getInstalledPaperVersion encountered an issue.
|
|
|
|
if not paperInstalledBuild:
|
|
|
|
print(oColors.brightRed + f"ERR: An error was encountered while detecting the server's Paper version." +
|
|
|
|
oColors.standardWhite)
|
|
|
|
return False
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
versionGroup = findVersionGroup(mcVersion)
|
2021-05-09 05:33:05 +00:00
|
|
|
|
|
|
|
# Report an error if findVersionGroup encountered an issue.
|
|
|
|
if not versionGroup:
|
|
|
|
print(oColors.brightRed + f"ERR: An error was encountered while fetching the server's version group." +
|
|
|
|
oColors.standardWhite)
|
|
|
|
return False
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
paperLatestBuild = findLatestBuild(versionGroup)
|
2021-05-09 05:33:05 +00:00
|
|
|
|
|
|
|
# Report an error if findLatestBuild encountered an issue.
|
|
|
|
if not paperLatestBuild:
|
|
|
|
print(oColors.brightRed + f"ERR: An error was encountered while fetching the latest version of PaperMC." +
|
|
|
|
oColors.standardWhite)
|
|
|
|
return False # Not currently handled, but can be at a later date. Currently just stops the following from
|
|
|
|
# being printed.
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
paperVersionBehind = versionBehind(paperInstalledBuild, paperLatestBuild)
|
|
|
|
|
2021-05-09 05:33:05 +00:00
|
|
|
# Report an error if getInstalledPaperVersion encountered an issue.
|
|
|
|
if not paperVersionBehind:
|
|
|
|
print(oColors.brightRed + f"ERR: An error was encountered while detecting how many versions behind you are. "
|
2021-05-15 23:05:29 +00:00
|
|
|
f"Will display as 'N/A'." + oColors.standardWhite)
|
|
|
|
paperVersionBehind = "N/A" # Sets paperVersionBehind to N/A while still letting the versionBehind check return
|
|
|
|
# # False for error-handing reasons.
|
2021-05-09 05:33:05 +00:00
|
|
|
|
2021-05-15 23:05:29 +00:00
|
|
|
# Does not return false as versions behind doesn't break things. It is just helpful information.
|
|
|
|
# paperVersionBehind will just display as "N/A"
|
2021-05-09 05:33:05 +00:00
|
|
|
|
2021-03-27 11:07:19 +00:00
|
|
|
print("┌─────┬────────────────────────────────┬──────────────┬──────────────┬───────────────────┐")
|
|
|
|
print("│ No. │ Name │ Installed V. │ Latest V. │ Versions behind │")
|
|
|
|
print("└─────┴────────────────────────────────┴──────────────┴──────────────┴───────────────────┘")
|
|
|
|
print(" [1]".rjust(6), end='')
|
|
|
|
print(" ", end='')
|
|
|
|
print("paper".ljust(33), end='')
|
|
|
|
print(f"{paperInstalledBuild}".ljust(15), end='')
|
|
|
|
print(f"{paperLatestBuild}".ljust(15), end='')
|
2021-03-15 15:26:00 +00:00
|
|
|
print(f"{paperVersionBehind}".ljust(8))
|
2021-03-27 17:58:16 +00:00
|
|
|
print(oColors.brightYellow + f"Versions behind: [{paperVersionBehind}]" + oColors.standardWhite)
|
2021-03-15 15:26:00 +00:00
|
|
|
|
2021-03-15 00:20:18 +00:00
|
|
|
|
|
|
|
# https://papermc.io/api/docs/swagger-ui/index.html?configUrl=/api/openapi/swagger-config#/
|
2021-03-15 15:26:00 +00:00
|
|
|
def papermc_downloader(paperBuild='latest', installedServerjarName=None, mcVersion=None):
|
2021-03-27 11:07:19 +00:00
|
|
|
configValues = configurationValues()
|
|
|
|
if configValues.localPluginFolder == False:
|
2021-03-15 15:26:00 +00:00
|
|
|
downloadPath = createTempPluginFolder()
|
|
|
|
else:
|
2021-03-27 11:07:19 +00:00
|
|
|
downloadPath = configValues.pathToPluginFolder
|
2021-03-16 00:04:13 +00:00
|
|
|
helpPath = Path('/plugins')
|
|
|
|
helpPathstr = str(helpPath)
|
|
|
|
downloadPath = Path(str(downloadPath).replace(helpPathstr, ''))
|
2021-03-15 15:26:00 +00:00
|
|
|
|
|
|
|
if mcVersion == None:
|
2021-03-16 00:04:13 +00:00
|
|
|
if paperBuild == 'latest':
|
|
|
|
mcVersion = '1.16.5'
|
|
|
|
else:
|
|
|
|
mcVersion = findBuildVersion(paperBuild)
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
if installedServerjarName != None:
|
|
|
|
mcVersion = getInstalledPaperMinecraftVersion(installedServerjarName)
|
|
|
|
|
|
|
|
if paperBuild == 'latest':
|
2021-03-16 00:04:13 +00:00
|
|
|
paperBuild = findLatestBuildForVersion(mcVersion)
|
2021-03-15 15:26:00 +00:00
|
|
|
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)
|
2021-03-16 00:04:13 +00:00
|
|
|
paperBuild = findLatestBuildForVersion(mcVersion)
|
2021-03-15 15:26:00 +00:00
|
|
|
downloadFileName = getDownloadFileName(mcVersion, paperBuild)
|
|
|
|
|
2021-03-16 00:04:13 +00:00
|
|
|
downloadPackagePath = Path(f"{downloadPath}/{downloadFileName}")
|
2021-03-15 15:26:00 +00:00
|
|
|
|
2021-03-27 11:07:19 +00:00
|
|
|
if configValues.localPluginFolder == False:
|
2021-03-15 15:26:00 +00:00
|
|
|
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']
|
2021-03-27 17:58:16 +00:00
|
|
|
print(f"Getting Paper {paperBuild} for {mcVersion}")
|
|
|
|
console = Console()
|
|
|
|
with console.status("Downloading...", spinner='line', spinner_style='bright_magenta') as status:
|
|
|
|
urllib.request.urlretrieve(url, downloadPackagePath)
|
2021-03-16 00:04:13 +00:00
|
|
|
filesizeData = calculateFileSizeMb(filesize)
|
2021-03-27 17:58:16 +00:00
|
|
|
print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPackagePath}")
|
2021-03-27 11:07:19 +00:00
|
|
|
if not configValues.localPluginFolder:
|
2021-03-30 21:55:04 +00:00
|
|
|
if not configValues.sftp_useSftp:
|
|
|
|
ftpSession = createFTPConnection()
|
|
|
|
ftp_upload_server_jar(ftpSession, downloadPackagePath)
|
|
|
|
else:
|
|
|
|
sftpSession = createSFTPConnection()
|
|
|
|
sftp_upload_server_jar(sftpSession, downloadPackagePath)
|
2021-05-09 05:33:05 +00:00
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
deleteTempPluginFolder(downloadPath)
|
2021-03-16 00:04:13 +00:00
|
|
|
|
2021-03-27 17:58:16 +00:00
|
|
|
print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper {paperBuild}" + \
|
2021-05-09 05:33:05 +00:00
|
|
|
oColors.brightGreen + " for " + oColors.standardWhite + f"{mcVersion}" + oColors.standardWhite)
|