mirror of
https://github.com/Neocky/pluGET.git
synced 2024-04-29 16:12:30 +00:00
Split everything in sub packages; Created main function; Added many exception handlers for SFTP
Added: - Split everything into sub packages - created a main file: __main__.py - added many exception handlings with SFTP Edited: - launcher.bat calls now the __main__.py file
This commit is contained in:
0
src/plugin/__init__.py
Normal file
0
src/plugin/__init__.py
Normal file
151
src/plugin/plugin_downloader.py
Normal file
151
src/plugin/plugin_downloader.py
Normal file
@@ -0,0 +1,151 @@
|
||||
import re
|
||||
import urllib.request
|
||||
from urllib.error import HTTPError
|
||||
|
||||
from utils.consoleoutput import oColors
|
||||
from utils.web_request import doAPIRequest
|
||||
from utils.utilities import createTempPluginFolder, deleteTempPluginFolder
|
||||
from handlers.handle_config import checkConfig
|
||||
from handlers.handle_sftp import sftp_upload_file, sftp_cdPluginDir, createSFTPConnection
|
||||
|
||||
|
||||
def calculateFileSize(downloadFileSize):
|
||||
fileSizeDownload = int(downloadFileSize)
|
||||
fileSizeKb = fileSizeDownload / 1024
|
||||
roundedFileSize = round(fileSizeKb, 2)
|
||||
return roundedFileSize
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
# TODO ununsed function
|
||||
def getlatestVersion(packageId):
|
||||
url = f"https://api.spiget.org/v2/resources/{packageId}/versions/latest"
|
||||
response = doAPIRequest(url)
|
||||
packageVersion = response["name"]
|
||||
return packageVersion
|
||||
|
||||
|
||||
def getVersionID(packageId, packageVersion):
|
||||
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
|
||||
|
||||
url = f"https://api.spiget.org/v2/resources/{packageId}/versions?size=100&sort=-name"
|
||||
versionList = doAPIRequest(url)
|
||||
|
||||
for packages in versionList:
|
||||
packageUpdate = packages["name"]
|
||||
versionId = packages["id"]
|
||||
if packageUpdate == packageVersion:
|
||||
return versionId
|
||||
return versionList[0]["id"]
|
||||
|
||||
|
||||
def getVersionName(packageId, versionId):
|
||||
url = f"https://api.spiget.org/v2/resources/{packageId}/versions/{versionId}"
|
||||
response = doAPIRequest(url)
|
||||
versionName = response["name"]
|
||||
return versionName
|
||||
|
||||
|
||||
def searchPackage(ressourceName):
|
||||
url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name"
|
||||
packageName = doAPIRequest(url)
|
||||
i = 1
|
||||
print("Index / Name / Description / Downloads")
|
||||
for ressource in packageName:
|
||||
pName = ressource["name"]
|
||||
pTag = ressource["tag"]
|
||||
pDownloads = ressource["downloads"]
|
||||
print(f" [{i}] {pName} / {pTag}/ {pDownloads}")
|
||||
i = i + 1
|
||||
|
||||
ressourceSelected = int(input("Select your wanted Ressource: "))
|
||||
ressourceSelected = ressourceSelected - 1
|
||||
#fileInfo = packageName[ressourceSelected]["file"]
|
||||
#packageUrl = fileInfo["url"]
|
||||
ressourceId = packageName[ressourceSelected]["id"]
|
||||
if not checkConfig().localPluginFolder:
|
||||
getSpecificPackage(ressourceId, checkConfig().sftp_folderPath)
|
||||
else:
|
||||
getSpecificPackage(ressourceId, checkConfig().pathToPluginFolder)
|
||||
|
||||
|
||||
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
|
||||
|
||||
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 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)
|
||||
#packageVersion = getlatestVersion(ressourceId)
|
||||
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
|
||||
downloadPackagePath = f"{downloadPath}\\{packageDownloadName}"
|
||||
if checkConfig().localPluginFolder:
|
||||
if inputPackageVersion is None or inputPackageVersion == 'latest':
|
||||
try:
|
||||
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
|
||||
except HTTPError as err:
|
||||
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
|
||||
else:
|
||||
try:
|
||||
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
|
||||
except HTTPError as err:
|
||||
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
|
||||
|
||||
if not checkConfig().localPluginFolder:
|
||||
if inputPackageVersion is None or inputPackageVersion == 'latest':
|
||||
try:
|
||||
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
|
||||
deleteTempPluginFolder(downloadPath)
|
||||
except HTTPError as err:
|
||||
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
|
||||
else:
|
||||
try:
|
||||
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
|
||||
deleteTempPluginFolder(downloadPath)
|
||||
except HTTPError as err:
|
||||
print(oColors.brightRed + f"Error: {err.code} - {err.reason}" + oColors.standardWhite)
|
||||
|
||||
# get latest update > https://api.spiget.org/v2/resources/28140/updates/latest
|
||||
# this also > https://api.spiget.org/v2/resources/28140/versions/latest
|
||||
# get latest download with correct name > https://api.spiget.org/v2/resources/28140/versions/latest/download cloudflare protected
|
||||
# query for a plugin https://api.spiget.org/v2/search/resources/luckperms?field=name
|
48
src/plugin/plugin_remover.py
Normal file
48
src/plugin/plugin_remover.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
from utils.consoleoutput import oColors
|
||||
from handlers.handle_config import checkConfig
|
||||
from handlers.handle_sftp import createSFTPConnection, sftp_listAll
|
||||
from plugin.plugin_updatechecker import getFileName, getFileVersion, getInstalledPlugin, createPluginList
|
||||
|
||||
|
||||
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 = 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)
|
197
src/plugin/plugin_updatechecker.py
Normal file
197
src/plugin/plugin_updatechecker.py
Normal file
@@ -0,0 +1,197 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
from utils.consoleoutput import oColors
|
||||
from utils.web_request import doAPIRequest
|
||||
from handlers.handle_config import checkConfig
|
||||
from handlers.handle_sftp import createSFTPConnection, sftp_listAll
|
||||
from plugin.plugin_downloader import getSpecificPackage
|
||||
|
||||
|
||||
def createPluginList():
|
||||
global INSTALLEDPLUGINLIST
|
||||
INSTALLEDPLUGINLIST = []
|
||||
return INSTALLEDPLUGINLIST
|
||||
|
||||
|
||||
def addToPluginList(pluginId, versionId, plugin_is_outdated):
|
||||
INSTALLEDPLUGINLIST.append([pluginId, versionId, plugin_is_outdated])
|
||||
|
||||
|
||||
def getFileName(pluginName):
|
||||
pluginNameFull = pluginName
|
||||
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
|
||||
try:
|
||||
pluginVersionFull = pluginVersion.group()
|
||||
except AttributeError:
|
||||
pluginVersionFull = pluginVersion
|
||||
pluginNameOnlyy = pluginNameFull.replace(pluginVersionFull, '')
|
||||
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnlyy)
|
||||
return pluginNameOnly
|
||||
|
||||
|
||||
def getFileVersion(pluginName):
|
||||
pluginNameFull = pluginName
|
||||
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
|
||||
pluginVersionFull = pluginVersion.group()
|
||||
pluginVersionString = pluginVersionFull.replace('.jar', '')
|
||||
return pluginVersionString
|
||||
|
||||
|
||||
def compareVersions(pluginId, pluginVersion):
|
||||
url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest"
|
||||
latestUpdateSearch = doAPIRequest(url)
|
||||
versionLatestUpdate = latestUpdateSearch["name"]
|
||||
if pluginVersion != versionLatestUpdate:
|
||||
plugin_is_outdated = True
|
||||
else:
|
||||
plugin_is_outdated = False
|
||||
return plugin_is_outdated
|
||||
|
||||
|
||||
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("Index / Name / Installed Version / Update available")
|
||||
try:
|
||||
for plugin in pluginList:
|
||||
try:
|
||||
fileName = getFileName(plugin)
|
||||
fileVersion = getFileVersion(plugin)
|
||||
pluginId = getInstalledPlugin(fileName, fileVersion)
|
||||
except TypeError:
|
||||
continue
|
||||
pluginIdStr = str(pluginId)
|
||||
|
||||
if fileVersion == '':
|
||||
fileVersion = 'N/A'
|
||||
|
||||
try:
|
||||
pluginIsOutdated = INSTALLEDPLUGINLIST[i][2]
|
||||
except IndexError:
|
||||
pluginIsOutdated = 'N/A'
|
||||
|
||||
if pluginIsOutdated == True:
|
||||
oldPackages = oldPackages + 1
|
||||
|
||||
if inputSelectedObject != "*":
|
||||
if inputSelectedObject != "all":
|
||||
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
|
||||
print(f"[{1}] {fileName} - {fileVersion} - {pluginIsOutdated}")
|
||||
break
|
||||
else:
|
||||
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way for the 2 else
|
||||
else:
|
||||
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}")
|
||||
|
||||
i = i + 1
|
||||
except TypeError:
|
||||
print(oColors.brightRed + "Aborted checking for plugins." + oColors.standardWhite)
|
||||
print(f"Old packages: [{oldPackages}/{i}]")
|
||||
|
||||
|
||||
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
|
||||
try:
|
||||
for plugin in pluginList:
|
||||
print(plugin)
|
||||
try:
|
||||
fileName = getFileName(plugin)
|
||||
fileVersion = getFileVersion(plugin)
|
||||
pluginId = getInstalledPlugin(fileName, fileVersion)
|
||||
except TypeError:
|
||||
continue
|
||||
pluginIdStr = str(pluginId)
|
||||
|
||||
if pluginId == None:
|
||||
print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite)
|
||||
continue
|
||||
|
||||
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
|
||||
print(f"Updating: {fileName}")
|
||||
if not checkConfig().localPluginFolder:
|
||||
pluginPath = checkConfig().sftp_folderPath
|
||||
pluginPath = f"{pluginPath}\\{plugin}"
|
||||
sftp = createSFTPConnection()
|
||||
sftp.remove(pluginPath)
|
||||
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
|
||||
pluginsUpdated += 1
|
||||
else:
|
||||
pluginPath = checkConfig().pathToPluginFolder
|
||||
pluginPath = f"{pluginPath}\\{plugin}"
|
||||
os.remove(pluginPath)
|
||||
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
||||
pluginsUpdated += 1
|
||||
break
|
||||
|
||||
if inputSelectedObject == 'all':
|
||||
if INSTALLEDPLUGINLIST[i][2] == True:
|
||||
if not checkConfig().localPluginFolder:
|
||||
pluginPath = checkConfig().sftp_folderPath
|
||||
pluginPath = f"{pluginPath}\\{plugin}"
|
||||
print("Deleting old plugin...")
|
||||
sftp = createSFTPConnection()
|
||||
sftp.remove(pluginPath)
|
||||
print("Downloading new plugin...")
|
||||
getSpecificPackage(pluginId, checkConfig().sftp_folderPath)
|
||||
pluginsUpdated += 1
|
||||
|
||||
else:
|
||||
pluginPath = checkConfig().pathToPluginFolder
|
||||
pluginPath = f"{pluginPath}\\{plugin}"
|
||||
print("Deleting old plugin...")
|
||||
os.remove(pluginPath)
|
||||
print("Downloading new plugin...")
|
||||
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
||||
pluginsUpdated += 1
|
||||
i = i + 1
|
||||
except TypeError:
|
||||
print(oColors.brightRed + "Aborted updating for plugins." + oColors.standardWhite)
|
||||
print(f"[{pluginsUpdated}/{i}] Plugins updated")
|
||||
|
||||
|
||||
def getInstalledPlugin(localFileName, localFileVersion):
|
||||
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
|
||||
packageName = doAPIRequest(url)
|
||||
i = 1
|
||||
plugin_match_found = False
|
||||
pluginID = None
|
||||
for ressource in packageName:
|
||||
if plugin_match_found == True:
|
||||
break
|
||||
#pName = ressource["name"]
|
||||
pID = ressource["id"]
|
||||
url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name"
|
||||
packageVersions = doAPIRequest(url2)
|
||||
for updates in packageVersions:
|
||||
updateVersion = updates["name"]
|
||||
if localFileVersion == updateVersion:
|
||||
plugin_match_found = True
|
||||
pluginID = pID
|
||||
updateId = updates["id"]
|
||||
plugin_is_outdated = compareVersions(pID, updateVersion)
|
||||
addToPluginList(pID, updateId, plugin_is_outdated)
|
||||
break
|
||||
i = i + 1
|
||||
return pluginID
|
||||
|
||||
|
||||
# start query
|
||||
# get id
|
||||
# search with id for all version upates
|
||||
# get version that matches installed version
|
||||
# if match then download latest update
|
||||
# else get second query
|
Reference in New Issue
Block a user