2021-03-12 00:39:39 +00:00
|
|
|
import re
|
2021-02-26 00:22:16 +00:00
|
|
|
import urllib.request
|
2021-03-08 21:13:57 +00:00
|
|
|
from urllib.error import HTTPError
|
2021-03-16 00:04:13 +00:00
|
|
|
from pathlib import Path
|
2021-03-12 00:39:39 +00:00
|
|
|
|
|
|
|
from utils.consoleoutput import oColors
|
|
|
|
from utils.web_request import doAPIRequest
|
2021-03-26 19:24:15 +00:00
|
|
|
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder, calculateFileSizeKb, calculateFileSizeMb
|
2021-03-27 11:07:19 +00:00
|
|
|
from handlers.handle_config import configurationValues
|
2021-03-30 21:55:04 +00:00
|
|
|
from handlers.handle_sftp import sftp_upload_file, createSFTPConnection
|
2021-03-31 16:11:41 +00:00
|
|
|
from handlers.handle_ftp import ftp_upload_file, createFTPConnection
|
2021-03-03 16:13:43 +00:00
|
|
|
|
2021-02-26 00:22:16 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
def handleRegexPackageName(packageNameFull):
|
|
|
|
packageNameFull2 = packageNameFull
|
|
|
|
# trims the part of the package that has for example "[1.1 Off]" in it
|
|
|
|
unwantedpackageName = re.search(r'(^\[+[a-zA-Z0-9\s\W*\.*\-*\+*\%*\,]*\]+)', packageNameFull)
|
|
|
|
unwantedpackageNamematch = bool(unwantedpackageName)
|
|
|
|
if unwantedpackageNamematch:
|
|
|
|
unwantedpackageNameString = unwantedpackageName.group()
|
|
|
|
packageNameFull2 = packageNameFull.replace(unwantedpackageNameString, '')
|
|
|
|
# gets the real packagename "word1 & word2" is not supported only gets word 1
|
|
|
|
packageName = re.search(r'([a-zA-Z]\d*)+(\s?\-*\_*[a-zA-Z]\d*\+*\-*\'*)+', packageNameFull2)
|
|
|
|
packageNameFullString = packageName.group()
|
|
|
|
packageNameOnly = packageNameFullString.replace(' ', '')
|
|
|
|
return packageNameOnly
|
2021-02-26 00:22:16 +00:00
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
|
|
|
|
def getVersionID(packageId, packageVersion):
|
2021-03-08 01:56:00 +00:00
|
|
|
if packageVersion == None or packageVersion == 'latest':
|
|
|
|
url = f"https://api.spiget.org/v2/resources/{packageId}/versions/latest"
|
|
|
|
response = doAPIRequest(url)
|
|
|
|
versionId = response["id"]
|
|
|
|
return versionId
|
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{packageId}/versions?size=100&sort=-name"
|
|
|
|
versionList = doAPIRequest(url)
|
2021-03-08 01:56:00 +00:00
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
for packages in versionList:
|
|
|
|
packageUpdate = packages["name"]
|
|
|
|
versionId = packages["id"]
|
|
|
|
if packageUpdate == packageVersion:
|
|
|
|
return versionId
|
|
|
|
return versionList[0]["id"]
|
2021-03-08 01:56:00 +00:00
|
|
|
|
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
def getVersionName(packageId, versionId):
|
|
|
|
url = f"https://api.spiget.org/v2/resources/{packageId}/versions/{versionId}"
|
|
|
|
response = doAPIRequest(url)
|
|
|
|
versionName = response["name"]
|
|
|
|
return versionName
|
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def searchPackage(ressourceName):
|
2021-03-27 11:07:19 +00:00
|
|
|
configValues = configurationValues()
|
2021-03-13 22:44:01 +00:00
|
|
|
url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name&sort=-downloads"
|
2021-03-04 23:09:03 +00:00
|
|
|
packageName = doAPIRequest(url)
|
2021-03-01 22:39:48 +00:00
|
|
|
i = 1
|
2021-03-21 13:30:27 +00:00
|
|
|
print(oColors.brightBlack + f"Searching: {ressourceName}" + oColors.standardWhite)
|
2021-03-28 14:15:03 +00:00
|
|
|
print("┌─────┬─────────────────────────────┬───────────┬──────────────────────────────────────────────────────────────────────┐")
|
|
|
|
print("│ No. │ Name │ Downloads │ Description │")
|
|
|
|
print("└─────┴─────────────────────────────┴───────────┴──────────────────────────────────────────────────────────────────────┘")
|
2021-03-01 22:39:48 +00:00
|
|
|
for ressource in packageName:
|
|
|
|
pName = ressource["name"]
|
2021-03-13 21:09:32 +00:00
|
|
|
newName = handleRegexPackageName(pName)
|
2021-03-04 23:09:03 +00:00
|
|
|
pTag = ressource["tag"]
|
|
|
|
pDownloads = ressource["downloads"]
|
2021-03-28 14:15:03 +00:00
|
|
|
print(f" [{i}]".rjust(6), end='')
|
|
|
|
print(" ", end='')
|
2021-03-13 21:09:32 +00:00
|
|
|
print(f"{newName}".ljust(30), end='')
|
2021-03-28 14:15:03 +00:00
|
|
|
print(f"{pDownloads}".rjust(9), end='')
|
|
|
|
print(" ", end='')
|
|
|
|
print(f"{pTag}".ljust(120))
|
2021-03-01 22:39:48 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2021-03-28 14:15:03 +00:00
|
|
|
ressourceSelected = int(input("Select your wanted Ressource (No.)(0 to exit): "))
|
2021-03-13 21:09:32 +00:00
|
|
|
if ressourceSelected != 0:
|
|
|
|
ressourceSelected = ressourceSelected - 1
|
|
|
|
ressourceId = packageName[ressourceSelected]["id"]
|
2021-03-27 11:07:19 +00:00
|
|
|
if not configValues.localPluginFolder:
|
2021-03-15 00:20:18 +00:00
|
|
|
try:
|
2021-03-27 11:07:19 +00:00
|
|
|
getSpecificPackage(ressourceId, configValues.sftp_folderPath)
|
2021-03-15 00:20:18 +00:00
|
|
|
except HTTPError as err:
|
|
|
|
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
|
2021-03-13 21:09:32 +00:00
|
|
|
else:
|
2021-03-15 00:20:18 +00:00
|
|
|
try:
|
2021-03-27 11:07:19 +00:00
|
|
|
getSpecificPackage(ressourceId, configValues.pathToPluginFolder)
|
2021-03-15 00:20:18 +00:00
|
|
|
except HTTPError as err:
|
|
|
|
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-02 23:17:15 +00:00
|
|
|
|
2021-03-08 01:56:00 +00:00
|
|
|
def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
|
2021-03-27 17:58:16 +00:00
|
|
|
configValues = configurationValues()
|
2021-03-08 01:56:00 +00:00
|
|
|
if versionID != 'latest':
|
|
|
|
#url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionID}"
|
2021-03-08 21:13:57 +00:00
|
|
|
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)
|
2021-03-08 01:56:00 +00:00
|
|
|
|
2021-03-03 16:13:43 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{ressourceId}/download"
|
2021-03-13 21:09:32 +00:00
|
|
|
#url = f"https://api.spiget.org/v2/resources/{ressourceId}/versions/latest/download" #throws 403 forbidden error...cloudflare :(
|
2021-03-08 01:56:00 +00:00
|
|
|
|
2021-03-31 16:11:41 +00:00
|
|
|
urrlib_opener = urllib.request.build_opener()
|
|
|
|
urrlib_opener.addheaders = [('User-agent', 'pluGET/1.0')]
|
|
|
|
urllib.request.install_opener(urrlib_opener)
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
remotefile = urllib.request.urlopen(url)
|
|
|
|
filesize = remotefile.info()['Content-Length']
|
|
|
|
urllib.request.urlretrieve(url, downloadPath)
|
2021-03-26 19:24:15 +00:00
|
|
|
filesize = int(filesize)
|
|
|
|
print(" ", end='')
|
|
|
|
if filesize >= 1000000:
|
|
|
|
filesizeData = calculateFileSizeMb(filesize)
|
|
|
|
print("Downloaded " + (str(filesizeData)).rjust(9) + f" MB here {downloadPath}")
|
|
|
|
else:
|
|
|
|
filesizeData = calculateFileSizeKb(filesize)
|
|
|
|
print("Downloaded " + (str(filesizeData)).rjust(9) + f" KB here {downloadPath}")
|
2021-03-27 17:58:16 +00:00
|
|
|
if not configValues.localPluginFolder:
|
2021-03-30 21:55:04 +00:00
|
|
|
if configValues.sftp_useSftp:
|
|
|
|
sftpSession = createSFTPConnection()
|
|
|
|
sftp_upload_file(sftpSession, downloadPath)
|
|
|
|
else:
|
|
|
|
ftpSession = createFTPConnection()
|
|
|
|
ftp_upload_file(ftpSession, downloadPath)
|
2021-03-03 22:25:44 +00:00
|
|
|
|
|
|
|
|
2021-03-08 21:13:57 +00:00
|
|
|
def getSpecificPackage(ressourceId, downloadPath, inputPackageVersion='latest'):
|
2021-03-27 11:07:19 +00:00
|
|
|
configValues = configurationValues()
|
|
|
|
if configValues.localPluginFolder == False:
|
2021-03-10 01:05:56 +00:00
|
|
|
downloadPath = createTempPluginFolder()
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
|
|
|
|
packageDetails = doAPIRequest(url)
|
2021-03-03 22:25:44 +00:00
|
|
|
packageName = packageDetails["name"]
|
2021-03-01 22:39:48 +00:00
|
|
|
packageNameNew = handleRegexPackageName(packageName)
|
2021-03-08 01:56:00 +00:00
|
|
|
versionId = getVersionID(ressourceId, inputPackageVersion)
|
|
|
|
packageVersion = getVersionName(ressourceId, versionId)
|
2021-03-01 22:39:48 +00:00
|
|
|
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
|
2021-03-31 16:11:41 +00:00
|
|
|
#if not configValues.localPluginFolder:
|
|
|
|
#downloadPackagePath = f"{downloadPath}/{packageDownloadName}"
|
|
|
|
#else:
|
2021-03-16 00:04:13 +00:00
|
|
|
downloadPackagePath = Path(f"{downloadPath}/{packageDownloadName}")
|
2021-03-30 21:55:04 +00:00
|
|
|
if inputPackageVersion is None or inputPackageVersion == 'latest':
|
|
|
|
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
|
|
|
|
else:
|
|
|
|
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
|
2021-03-10 01:05:56 +00:00
|
|
|
|
2021-03-27 11:07:19 +00:00
|
|
|
if not configValues.localPluginFolder:
|
2021-03-30 21:55:04 +00:00
|
|
|
deleteTempPluginFolder(downloadPath)
|