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') system('cls' if name=='nt' else 'clear')
# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
class oColors: class oColors:
standardWhite = "\033[0m" standardWhite = "\033[0m"
brightYellow = "\033[93m" brightYellow = "\033[93m"
brightMagenta = "\033[95m" brightMagenta = "\033[95m"
brightRed = "\033[91m"
darkMagenta = "\033[35m" darkMagenta = "\033[35m"

View File

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

View File

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

View File

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