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-15 00:20:18 +00:00
|
|
|
|
|
|
|
from utils.consoleoutput import oColors
|
|
|
|
from utils.web_request import doAPIRequest
|
2021-03-15 15:26:00 +00:00
|
|
|
from handlers.handle_sftp import sftp_upload_server_jar, sftp_cdPluginDir, createSFTPConnection
|
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-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-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']
|
|
|
|
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
|
|
|
|
break
|
|
|
|
|
|
|
|
return paperVersionGroup
|
|
|
|
|
|
|
|
|
2021-03-16 00:04:13 +00:00
|
|
|
def findBuildVersion(wantedPaperBuild):
|
|
|
|
versionGroups = ['1.16', '1.15']
|
|
|
|
paperBuildFound = False
|
|
|
|
for versionGroup in versionGroups:
|
|
|
|
if paperBuildFound is True:
|
|
|
|
break
|
|
|
|
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:
|
|
|
|
paperBuildFound = True
|
|
|
|
paperVersionGroup = build["version"]
|
|
|
|
break
|
|
|
|
return paperVersionGroup
|
|
|
|
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-03-16 00:04:13 +00:00
|
|
|
def findLatestBuildForVersion(mcVersion):
|
|
|
|
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):
|
|
|
|
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)
|
|
|
|
|
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-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-16 00:04:13 +00:00
|
|
|
print(f"Starting Paper-{paperBuild} download for {mcVersion}...")
|
2021-03-15 15:26:00 +00:00
|
|
|
urllib.request.urlretrieve(url, downloadPackagePath)
|
2021-03-16 00:04:13 +00:00
|
|
|
filesizeData = calculateFileSizeMb(filesize)
|
2021-03-15 15:26:00 +00:00
|
|
|
|
2021-03-16 00:04:13 +00:00
|
|
|
print(f"Downloadsize: {filesizeData} MB")
|
2021-03-15 15:26:00 +00:00
|
|
|
print(f"File downloaded here: {downloadPackagePath}")
|
2021-03-27 11:07:19 +00:00
|
|
|
if not configValues.localPluginFolder:
|
2021-03-15 15:26:00 +00:00
|
|
|
sftpSession = createSFTPConnection()
|
|
|
|
sftp_upload_server_jar(sftpSession, downloadPackagePath)
|
|
|
|
deleteTempPluginFolder(downloadPath)
|
2021-03-16 00:04:13 +00:00
|
|
|
|
|
|
|
print(oColors.brightGreen + "Downloaded successfully " + oColors.standardWhite + f"Paper-{paperBuild}" + \
|
|
|
|
oColors.brightGreen + " for " + oColors.standardWhite + f"{mcVersion}" + oColors.standardWhite)
|