2021-03-01 22:39:48 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2021-03-08 21:13:57 +00:00
|
|
|
from consoleoutput import oColors
|
|
|
|
from plugin_downloader import getSpecificPackage #handleInput
|
2021-03-04 23:09:03 +00:00
|
|
|
from web_request import doAPIRequest
|
2021-03-08 21:13:57 +00:00
|
|
|
from handle_config import checkConfig
|
2021-03-01 22:39:48 +00:00
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def createPluginList():
|
|
|
|
global INSTALLEDPLUGINLIST
|
|
|
|
INSTALLEDPLUGINLIST = []
|
|
|
|
return INSTALLEDPLUGINLIST
|
2021-03-03 16:13:43 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def addToPluginList(pluginId, versionId, plugin_is_outdated):
|
|
|
|
INSTALLEDPLUGINLIST.append([pluginId, versionId, plugin_is_outdated])
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-03 16:13:43 +00:00
|
|
|
def getFileName(pluginName):
|
2021-03-01 22:39:48 +00:00
|
|
|
pluginNameFull = pluginName
|
|
|
|
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
|
|
|
|
pluginVersionFull = pluginVersion.group()
|
|
|
|
pluginNameOnly = pluginNameFull.replace(pluginVersionFull, '')
|
2021-03-03 22:25:44 +00:00
|
|
|
pluginNameOnly = re.sub(r'(\-$)', '', pluginNameOnly)
|
2021-03-03 16:13:43 +00:00
|
|
|
return pluginNameOnly
|
|
|
|
|
|
|
|
|
|
|
|
def getFileVersion(pluginName):
|
2021-03-03 22:25:44 +00:00
|
|
|
#pluginVersionString = None
|
2021-03-03 16:13:43 +00:00
|
|
|
pluginNameFull = pluginName
|
|
|
|
pluginVersion = re.search(r'([\d.]+[.jar]+)', pluginNameFull)
|
|
|
|
pluginVersionFull = pluginVersion.group()
|
|
|
|
pluginVersionString = pluginVersionFull.replace('.jar', '')
|
|
|
|
return pluginVersionString
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
|
|
|
|
def compareVersions(pluginId, pluginVersion):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest"
|
|
|
|
latestUpdateSearch = doAPIRequest(url)
|
2021-03-03 16:13:43 +00:00
|
|
|
versionLatestUpdate = latestUpdateSearch["name"]
|
|
|
|
if pluginVersion != versionLatestUpdate:
|
|
|
|
plugin_is_outdated = True
|
|
|
|
else:
|
|
|
|
plugin_is_outdated = False
|
|
|
|
return plugin_is_outdated
|
|
|
|
|
|
|
|
|
2021-03-08 01:56:00 +00:00
|
|
|
def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"):
|
|
|
|
createPluginList()
|
|
|
|
pluginList = os.listdir(pluginFolderPath)
|
|
|
|
i = 0
|
|
|
|
oldPackages = 0
|
|
|
|
print("Index / Name / Installed Version / Update available")
|
|
|
|
|
|
|
|
for plugin in pluginList:
|
|
|
|
fileName = getFileName(plugin)
|
|
|
|
fileVersion = getFileVersion(plugin)
|
|
|
|
pluginId = getInstalledPlugin(fileName, fileVersion)
|
|
|
|
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:
|
2021-03-08 21:13:57 +00:00
|
|
|
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way for the 2 else
|
2021-03-08 01:56:00 +00:00
|
|
|
else:
|
|
|
|
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}")
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
print(f"Old packages: [{oldPackages}/{i}]")
|
|
|
|
|
|
|
|
|
|
|
|
def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'):
|
2021-03-03 22:25:44 +00:00
|
|
|
createPluginList()
|
2021-03-03 16:13:43 +00:00
|
|
|
pluginList = os.listdir(pluginFolderPath)
|
|
|
|
print(pluginList)
|
2021-03-03 22:25:44 +00:00
|
|
|
i = 0
|
2021-03-03 16:13:43 +00:00
|
|
|
for plugin in pluginList:
|
|
|
|
print(plugin)
|
|
|
|
fileName = getFileName(plugin)
|
|
|
|
fileVersion = getFileVersion(plugin)
|
2021-03-03 22:25:44 +00:00
|
|
|
pluginId = getInstalledPlugin(fileName, fileVersion)
|
2021-03-08 01:56:00 +00:00
|
|
|
pluginIdStr = str(pluginId)
|
|
|
|
|
2021-03-03 16:13:43 +00:00
|
|
|
if pluginId == None:
|
2021-03-03 22:25:44 +00:00
|
|
|
print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite)
|
2021-03-03 16:13:43 +00:00
|
|
|
continue
|
2021-03-08 01:56:00 +00:00
|
|
|
|
|
|
|
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
|
|
|
|
print(f"Updating: {fileName}")
|
2021-03-08 21:13:57 +00:00
|
|
|
pluginPath = checkConfig().pathToPluginFolder
|
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
|
|
|
os.remove(pluginPath)
|
|
|
|
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
2021-03-08 01:56:00 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if inputSelectedObject == 'all':
|
|
|
|
if INSTALLEDPLUGINLIST[i][2] == True:
|
|
|
|
print("Deleting old plugin...")
|
2021-03-08 21:13:57 +00:00
|
|
|
pluginPath = checkConfig().pathToPluginFolder
|
|
|
|
pluginPath = f"{pluginPath}\\{plugin}"
|
|
|
|
os.remove(pluginPath)
|
2021-03-08 01:56:00 +00:00
|
|
|
print("Downloading new plugin...")
|
2021-03-08 21:13:57 +00:00
|
|
|
getSpecificPackage(pluginId, checkConfig().pathToPluginFolder)
|
2021-03-04 23:09:03 +00:00
|
|
|
i = i + 1
|
2021-03-08 01:56:00 +00:00
|
|
|
#print(INSTALLEDPLUGINLIST[1][0])
|
2021-03-03 16:13:43 +00:00
|
|
|
#getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\")
|
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def getInstalledPlugin(localFileName, localFileVersion):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
|
|
|
|
packageName = doAPIRequest(url)
|
|
|
|
#packageName = response.json()
|
2021-03-02 23:17:15 +00:00
|
|
|
i = 1
|
2021-03-03 16:13:43 +00:00
|
|
|
plugin_match_found = False
|
|
|
|
pluginID = None
|
2021-03-02 23:17:15 +00:00
|
|
|
for ressource in packageName:
|
2021-03-03 16:13:43 +00:00
|
|
|
if plugin_match_found == True:
|
|
|
|
break
|
2021-03-08 21:13:57 +00:00
|
|
|
#pName = ressource["name"]
|
2021-03-02 23:17:15 +00:00
|
|
|
pID = ressource["id"]
|
2021-03-04 23:09:03 +00:00
|
|
|
url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name"
|
|
|
|
packageVersions = doAPIRequest(url2)
|
2021-03-03 16:13:43 +00:00
|
|
|
for updates in packageVersions:
|
|
|
|
updateVersion = updates["name"]
|
|
|
|
if localFileVersion == updateVersion:
|
|
|
|
plugin_match_found = True
|
|
|
|
pluginID = pID
|
2021-03-03 22:25:44 +00:00
|
|
|
updateId = updates["id"]
|
|
|
|
plugin_is_outdated = compareVersions(pID, updateVersion)
|
|
|
|
addToPluginList(pID, updateId, plugin_is_outdated)
|
2021-03-03 16:13:43 +00:00
|
|
|
break
|
2021-03-02 23:17:15 +00:00
|
|
|
i = i + 1
|
2021-03-03 16:13:43 +00:00
|
|
|
return pluginID
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-04 23:09:03 +00:00
|
|
|
# start query
|
2021-03-01 22:39:48 +00:00
|
|
|
# get id
|
2021-03-03 16:13:43 +00:00
|
|
|
# search with id for all version upates
|
2021-03-01 22:39:48 +00:00
|
|
|
# get version that matches installed version
|
|
|
|
# if match then download latest update
|
2021-03-08 01:56:00 +00:00
|
|
|
# else get second query
|