Finished the local plugin auto downloader function.

Added:
- console color red
- two dimensional array for plugins
- finished it with a deletion of old files
This commit is contained in:
Neocky 2021-03-03 23:25:44 +01:00
parent a713a63264
commit fd90b86e87
4 changed files with 79 additions and 42 deletions

View File

@ -10,10 +10,12 @@ def clearConsole():
system('cls' if name=='nt' else 'clear')
# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
class oColors:
standardWhite = "\033[0m"
brightYellow = "\033[93m"
brightMagenta = "\033[95m"
brightRed = "\033[91m"
darkMagenta = "\033[35m"

View File

@ -1,19 +1,33 @@
import time
from consoleoutput import consoleTitle, clearConsole, printMainMenu
from plugin_downloader import downloadPackageManual, apiCallTest, searchPackage, getLatestPackageVersion
from plugin_downloader import downloadPackageManual, apiCallTest, searchPackage, getLatestPackageVersionInteractive
from plugin_updatechecker import getInstalledPackages
def inputOption(inputOptionString):
if inputOptionString == "1":
ressourceID = input("SpigotMC Ressource ID: ")
return ressourceID
if inputOptionString == "2":
ressourceId = input("SpigotMC Ressource ID: ")
return ressourceId
if inputOptionString == "3":
ressourceName = input(" SpigotMC Ressource Name: ")
print("ich bin ein test")
return ressourceName
def handleInput(inputString):
if inputString == "1":
downloadPackageManual()
if inputString == "2":
apiCallTest()
ressourceId = inputOption(2)
apiCallTest(ressourceId)
if inputString == "3":
searchPackage()
ressourceName = inputOption(3)
searchPackage(ressourceName)
if inputString == "4":
ressourceID = inputOption(inputString)
getLatestPackageVersion(ressourceID, r"C:\\Users\USER\Desktop\\")
getLatestPackageVersionInteractive(r"C:\\Users\USER\Desktop\\")
if inputString == "5":
getInstalledPackages('C:\\Users\\USER\\Desktop\\plugins')
@ -25,11 +39,6 @@ def inputMainMenu():
handleInput(inputSt)
def inputOption(inputOption):
if inputOption == "1":
ressourceID = input("SpigotMC Ressource ID: ")
return ressourceID
def outputTest():
print("Hello world")

View File

@ -4,7 +4,6 @@ import re
import requests
def calculateFileSize(downloadFileSize):
fileSizeDownload = int(downloadFileSize)
fileSizeMb = fileSizeDownload / 1024 / 1024
@ -68,9 +67,8 @@ def getlatestVersion(packageId):
return packageVersion
def apiCallTest():
ressourceId = input("SpigotMC Ressource ID: ")
response = requests.get("https://api.spiget.org/v2/resources/" + ressourceId)
def apiCallTest(ressourceId):
response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}")
packageDetails = response.json()
print(packageDetails)
packageName = response.json()["name"]
@ -88,11 +86,10 @@ def compareVersions():
print("compareVersions")
def searchPackage():
ressourceName = input(" SpigotMC Ressource Name: ")
response = requests.get("https://api.spiget.org/v2/search/resources/" + ressourceName + "?field=name")
def searchPackage(ressourceName):
response = requests.get(f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name")
#https://api.spiget.org/v2/search/resources/luckperms?field=name
print("https://api.spiget.org/v2/search/resources/" + ressourceName + "?field=name")
print(f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name")
packageName = response.json()
i = 1
for ressource in packageName:
@ -125,8 +122,20 @@ def getLatestPackageVersion(ressourceId, downloadPath):
#ressourceId = input(" SpigotMC Ressource ID: ")
response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}")
packageDetails = response.json()
packageName = response.json()["name"]
packageTag = response.json()["tag"]
packageName = packageDetails["name"]
packageTag = packageDetails["tag"]
packageNameNew = handleRegexPackageName(packageName)
packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadLatestVersion(ressourceId, packageDownloadName, downloadPath)
def getLatestPackageVersionInteractive(downloadPath):
ressourceId = input(" SpigotMC Ressource ID: ")
response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}")
packageDetails = response.json()
packageName = packageDetails["name"]
packageTag = packageDetails["tag"]
packageNameNew = handleRegexPackageName(packageName)
packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"

View File

@ -2,38 +2,44 @@ import os
import re
import requests
#from consoleoutput import consoleTitle, clearConsole
from consoleoutput import oColors #consoleTitle, clearConsole
from plugin_downloader import getLatestPackageVersion #handleInput
# as seen on https://pythonguides.com/create-list-in-python/
class installedPlugin:
def __init__(self, pluginId, plugin_is_outdated):
self.pluginId = pluginId
self.plugin_is_outdated = plugin_is_outdated
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)
pluginVersionFull = pluginVersion.group()
pluginNameOnly = pluginNameFull.replace(pluginVersionFull, '')
pluginNameOnly = pluginNameOnly[:-1]
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnly)
return pluginNameOnly
def getFileVersion(pluginName):
#pluginVersionString = None
pluginNameFull = pluginName
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
pluginVersionFull = pluginVersion.group()
pluginVersionString = pluginVersionFull.replace('.jar', '')
return pluginVersionString
# not yet implemented
def compareVersions(pluginVersion, pluginId, updateId):
latestUpdateSearch = requests.get(f"https://api.spiget.org/v2/resources/{pluginId}/versions/{updateId}")
def compareVersions(pluginId, pluginVersion):
responseUpdateSearch = requests.get(f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest")
latestUpdateSearch = responseUpdateSearch.json()
versionLatestUpdate = latestUpdateSearch["name"]
print(pluginVersion)
print(versionLatestUpdate)
if pluginVersion != versionLatestUpdate:
plugin_is_outdated = True
else:
@ -42,24 +48,32 @@ def compareVersions(pluginVersion, pluginId, updateId):
def getInstalledPackages(pluginFolderPath):
list = []
createPluginList()
pluginList = os.listdir(pluginFolderPath)
print(pluginList)
i = 0
for plugin in pluginList:
print(plugin)
fileName = getFileName(plugin)
fileVersion = getFileVersion(plugin)
pluginId = getInstalledPluginVersion(fileName, fileVersion)
list.append( installedPlugin(pluginId, plugin_is_outdated))
if pluginId == None:
print("Couldn't find plugin id. Sorry :(")
continue
pluginId = getInstalledPlugin(fileName, fileVersion)
print(f"name: {fileName}")
print(f"version: {fileVersion}")
print(INSTALLEDPLUGINLIST)
if pluginId == None:
print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite)
continue
if INSTALLEDPLUGINLIST[i][2] == True:
getLatestPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\")
os.remove(f"C:\\Users\USER\\Desktop\\plugins\\{plugin}")
print(INSTALLEDPLUGINLIST[1][0])
#getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\")
def getInstalledPluginVersion(localFileName, localFileVersion):
def getInstalledPlugin(localFileName, localFileVersion):
response = requests.get("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name")
#https://api.spiget.org/v2/search/resources/luckperms?field=name
print("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name")
@ -80,9 +94,12 @@ def getInstalledPluginVersion(localFileName, localFileVersion):
if localFileVersion == updateVersion:
plugin_match_found = True
pluginID = pID
print(updates["id"])
print("Found match")
updateId = updates["id"]
plugin_is_outdated = compareVersions(pID, updateVersion)
addToPluginList(pID, updateId, plugin_is_outdated)
print(updateId)
print(pID)
print("Found match")
break
i = i + 1
return pluginID