New input system; Only one download function now; Specific plugin updater added and check for all installed plugins;

Removed:
- cloudscraper
- unnecessary functions

Added:
- new input system;
-> Now it is like a console
- general download function
- specific version updater
- check for updates on all installed version
This commit is contained in:
Neocky 2021-03-08 02:56:00 +01:00
parent ed15790d97
commit 8212f66e90
6 changed files with 143 additions and 108 deletions

View File

@ -1,5 +1,2 @@
urllib3 >= 1.21.1 urllib3 >= 1.21.1
requests >=2.25.1 requests >=2.25.1
requests_toolbelt >= 0.9.1
pyparsing >= 2.4.7
cloudscraper >= 1.2.56

View File

@ -88,4 +88,4 @@ def printMainMenu():
#print(" [3] Search for a plugin") #print(" [3] Search for a plugin")
#print(" [4] Download latest version of package") #print(" [4] Download latest version of package")
#print(" [5] Check update for installed plugins") #print(" [5] Check update for installed plugins")
print() #print()

View File

@ -1,28 +1,58 @@
import time import time
from consoleoutput import consoleTitle, clearConsole, printMainMenu import sys
from plugin_downloader import downloadPackageManual, apiCallTest, searchPackage, getLatestPackageVersionInteractive, getPackageVersion from consoleoutput import consoleTitle, clearConsole, printMainMenu, oColors
from plugin_updatechecker import getInstalledPackages from plugin_downloader import downloadPackageManual, apiCallTest, searchPackage, getPackageVersion
from web_request import createCloudScraperInstance#, CLOUDSCRAPER from plugin_updatechecker import updateInstalledPackage, checkInstalledPackage
def createInputLists():
global COMMANDLIST
COMMANDLIST = [
'get',
'update',
'check',
'exit'
]
global INPUTSELECTEDOBJECT
INPUTSELECTEDOBJECT = [
'all',
'*'
]
def handleInput(inputCommand, inputSelectedObject, inputParams="latest"):
if inputCommand == 'get':
getPackageVersion(inputSelectedObject, inputParams, r"C:\\Users\USER\Desktop\\")
def handleInput(inputCommand, inputSelectedObject, inputParams):
while True:
if inputCommand == 'get':
getPackageVersion(r"C:\\Users\USER\Desktop\\", inputSelectedObject, inputParams)
break
if inputCommand == 'update':
#if inputSelectedObject in INPUTSELECTEDOBJECT:
updateInstalledPackage(r'C:\\Users\\USER\\Desktop\\plugins', inputSelectedObject)
break
if inputCommand == 'check':
checkInstalledPackage(r'C:\\Users\\USER\\Desktop\\plugins', inputSelectedObject)
break
if inputCommand == 'exit':
sys.exit()
else:
print(oColors.brightRed + "Command not found. Please try again." + oColors.standardWhite)
getInput()
getInput()
def getInput(): def getInput():
inputCommand, inputSelectedObject, *inputParams = input("pluGET >> ").split() while True:
inputParams = inputParams[0] if inputParams else '' try:
inputCommand, inputSelectedObject, *inputParams = input("pluGET >> ").split()
break
except ValueError:
print(oColors.brightRed + "Wrong input! Use: > *command* *selectedObject* *optionalParams*" + oColors.standardWhite)
inputParams = inputParams[0] if inputParams else None
print(inputCommand) print(inputCommand)
print(inputSelectedObject) print(inputSelectedObject)
print(inputParams) print(inputParams)
handleInput(inputCommand, inputSelectedObject,inputParams) handleInput(inputCommand, inputSelectedObject, inputParams)
def inputOption(inputOptionString): def inputOption(inputOptionString):
@ -48,12 +78,15 @@ def handleInputOLD(inputString):
ressourceName = inputOption(3) ressourceName = inputOption(3)
searchPackage(ressourceName) searchPackage(ressourceName)
if inputString == "4": if inputString == "4":
getLatestPackageVersionInteractive(r"C:\\Users\USER\Desktop\\") #getLatestPackageVersionInteractive(r"C:\\Users\USER\Desktop\\")
print("4")
if inputString == "5": if inputString == "5":
getInstalledPackages('C:\\Users\\USER\\Desktop\\plugins') #updateAllInstalledPackages(r'C:\\Users\\USER\\Desktop\\plugins')
print("5")
def inputMainMenu(): def inputMainMenu():
createInputLists()
clearConsole() clearConsole()
printMainMenu() printMainMenu()
getInput() getInput()
@ -77,7 +110,7 @@ def outputTest():
input("Press key to end program...") input("Press key to end program...")
createCloudScraperInstance() #createCloudScraperInstance()
consoleTitle() consoleTitle()
inputMainMenu() inputMainMenu()
outputTest() outputTest()

View File

@ -1,7 +1,6 @@
import urllib.request import urllib.request
import cgi import cgi
import re import re
import cloudscraper
from web_request import doAPIRequest from web_request import doAPIRequest
@ -52,13 +51,12 @@ def handleRegexPackageName(packageNameFull):
packageNameFull2 = packageNameFull.replace(unwantedpackageNameString, '') packageNameFull2 = packageNameFull.replace(unwantedpackageNameString, '')
print(packageNameFull2) print(packageNameFull2)
print("packageNameFull2") print("packageNameFull2")
# gets the real packagename "word1 & word2" is not supported only gets word 1 # 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) packageName = re.search(r'([a-zA-Z]\d*)+(\s?\-*\_*[a-zA-Z]\d*\+*\-*\'*)+', packageNameFull2)
packageNameFullString = packageName.group() packageNameFullString = packageName.group()
packageNameOnly = packageNameFullString.replace(' ', '') packageNameOnly = packageNameFullString.replace(' ', '')
print(packageNameOnly) #print(packageNameOnly)
print("packageNameOnly") #print("packageNameOnly")
return packageNameOnly return packageNameOnly
@ -81,14 +79,14 @@ def apiCallTest(ressourceId):
print(packageNameNew) print(packageNameNew)
# check version
def compareVersions():
#https://api.spiget.org/v2/resources/28140/versions/latest
# compare latest package version with installed package version
print("compareVersions")
def getVersionID(packageId, packageVersion): def getVersionID(packageId, packageVersion):
print(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" url = f"https://api.spiget.org/v2/resources/{packageId}/versions?size=100&sort=-name"
versionList = doAPIRequest(url) versionList = doAPIRequest(url)
@ -99,6 +97,7 @@ def getVersionID(packageId, packageVersion):
return versionId return versionId
return versionList[0]["id"] return versionList[0]["id"]
def getVersionName(packageId, versionId): def getVersionName(packageId, versionId):
url = f"https://api.spiget.org/v2/resources/{packageId}/versions/{versionId}" url = f"https://api.spiget.org/v2/resources/{packageId}/versions/{versionId}"
response = doAPIRequest(url) response = doAPIRequest(url)
@ -128,74 +127,39 @@ def searchPackage(ressourceName):
print(ressourceId) print(ressourceId)
def downloadLatestVersion(ressourceId, packageDownloadName, sourcePath): def downloadSpecificVersion(ressourceId, downloadPath, versionID='latest'):
print(versionID)
if versionID != 'latest':
#url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionID}"
print("Sorry but specific version downloads aren't supported because of cloudflare protection. :(")
url = f"https://api.spiget.org/v2/resources/{ressourceId}/download" 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) remotefile = urllib.request.urlopen(url)
filesize = remotefile.info()['Content-Length'] filesize = remotefile.info()['Content-Length']
downloadPath = sourcePath + packageDownloadName
urllib.request.urlretrieve(url, downloadPath) urllib.request.urlretrieve(url, downloadPath)
filesizeData = calculateFileSize(filesize) filesizeData = calculateFileSize(filesize)
print(filesizeData) print(f"File downloaded here: {downloadPath}")
print(filesize) print(f"Downloadsize: {filesizeData} KB")
print(f" Downloadsize: {filesizeData} KB")
def downloadSpecificVersion(ressourceId, packageDownloadName, versionId, sourcePath):
url = f"https://spigotmc.org/resources/{ressourceId}/download?version={versionId}"
#url = f"https://api.spiget.org/v2/resources/{ressourceId}/versions/{versionId}/download"
downloadPath = sourcePath + packageDownloadName
#local_filename = url.split('/')[-1] def getPackageVersion(ressourceId, downloadPath, inputPackageVersion='latest'):
# NOTE the stream=True parameter below
with CLOUDSCRAPER.get(url, stream=True) as r:
#r.raise_for_status()
with open(downloadPath, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)
#return downloadPath
#remotefile = urllib.request.urlopen(url)
#cloudscraper.requests.get()
#filesize = remotefile.info()['Content-Length']
#urllib.request.urlretrieve(url, downloadPath)
#filesizeData = calculateFileSize(filesize)
#print(filesizeData)
#print(filesize)
#print(f" Downloadsize: {filesizeData} KB")
def getPackageVersion(ressourceId, packageVersion, downloadPath):
#ressourceId = input(" SpigotMC Ressource ID: ")
CLOUDSCRAPER = createCloudScraperInstance()
url = f"https://api.spiget.org/v2/resources/{ressourceId}" url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = doAPIRequest(url) packageDetails = doAPIRequest(url)
packageName = packageDetails["name"] packageName = packageDetails["name"]
#packageTag = packageDetails["tag"] #packageTag = packageDetails["tag"]
packageNameNew = handleRegexPackageName(packageName) packageNameNew = handleRegexPackageName(packageName)
versionId = getVersionID(ressourceId, packageVersion) versionId = getVersionID(ressourceId, inputPackageVersion)
packageVersion = getVersionName(ressourceId, versionId) packageVersion = getVersionName(ressourceId, versionId)
#packageVersion = getlatestVersion(ressourceId) #packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar" packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadSpecificVersion(ressourceId, packageDownloadName, versionId, downloadPath) downloadPackagePath = downloadPath + packageDownloadName
if inputPackageVersion is None or inputPackageVersion == 'latest':
def getLatestPackageVersionInteractive(downloadPath): downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
ressourceId = input(" SpigotMC Ressource ID: ") else:
url = f"https://api.spiget.org/v2/resources/{ressourceId}" downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
packageDetails = doAPIRequest(url)
packageName = packageDetails["name"]
#packageTag = packageDetails["tag"]
packageNameNew = handleRegexPackageName(packageName)
packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadLatestVersion(ressourceId, packageDownloadName, downloadPath)
def createCloudScraperInstance():
global CLOUDSCRAPER
CLOUDSCRAPER = cloudscraper.create_scraper(interpreter='nodejs',debug=True) # returns a CloudScraper instance
return CLOUDSCRAPER
# get latest update > https://api.spiget.org/v2/resources/28140/updates/latest # get latest update > https://api.spiget.org/v2/resources/28140/updates/latest
# this also > https://api.spiget.org/v2/resources/28140/versions/latest # this also > https://api.spiget.org/v2/resources/28140/versions/latest

View File

@ -38,8 +38,6 @@ def compareVersions(pluginId, pluginVersion):
url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest" url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest"
latestUpdateSearch = doAPIRequest(url) latestUpdateSearch = doAPIRequest(url)
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:
@ -47,7 +45,48 @@ def compareVersions(pluginId, pluginVersion):
return plugin_is_outdated return plugin_is_outdated
def getInstalledPackages(pluginFolderPath): def checkInstalledPackage(pluginFolderPath, inputSelectedObject="all"):
#if inputSelectedObject is not ('all', '*'):
# print(oColors.brightRed + "Only *all* as selected object is supported!" + oColors.standardWhite)
# 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:
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}") # TODO find better way
else:
print(f"[{i+1}] {fileName} - {fileVersion} - {pluginIsOutdated}")
i = i + 1
print(f"Old packages: [{oldPackages}/{i}]")
def updateInstalledPackage(pluginFolderPath, inputSelectedObject='all'):
createPluginList() createPluginList()
pluginList = os.listdir(pluginFolderPath) pluginList = os.listdir(pluginFolderPath)
print(pluginList) print(pluginList)
@ -57,27 +96,38 @@ def getInstalledPackages(pluginFolderPath):
fileName = getFileName(plugin) fileName = getFileName(plugin)
fileVersion = getFileVersion(plugin) fileVersion = getFileVersion(plugin)
pluginId = getInstalledPlugin(fileName, fileVersion) pluginId = getInstalledPlugin(fileName, fileVersion)
pluginIdStr = str(pluginId)
print(f"name: {fileName}") print(f"name: {fileName}")
print(f"version: {fileVersion}") print(f"version: {fileVersion}")
# debug purpose
print(inputSelectedObject)
print(INSTALLEDPLUGINLIST) print(INSTALLEDPLUGINLIST)
print(f"pluginId: {pluginId}")
if pluginId == None: if pluginId == None:
print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite) print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite)
continue continue
if INSTALLEDPLUGINLIST[i][2] == True:
if inputSelectedObject == pluginIdStr or re.search(inputSelectedObject, fileName, re.IGNORECASE):
print(f"Updating: {fileName}")
os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}") os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}")
getLatestPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\") getPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\")
break
if inputSelectedObject == 'all':
if INSTALLEDPLUGINLIST[i][2] == True:
print("Deleting old plugin...")
os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}")
print("Downloading new plugin...")
getPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\")
i = i + 1 i = i + 1
print(INSTALLEDPLUGINLIST[1][0]) #print(INSTALLEDPLUGINLIST[1][0])
#getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\") #getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\")
def getInstalledPlugin(localFileName, localFileVersion): def getInstalledPlugin(localFileName, localFileVersion):
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name" url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
packageName = doAPIRequest(url) packageName = doAPIRequest(url)
print("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name")
#packageName = response.json() #packageName = response.json()
i = 1 i = 1
plugin_match_found = False plugin_match_found = False
@ -87,7 +137,7 @@ def getInstalledPlugin(localFileName, localFileVersion):
break break
pName = ressource["name"] pName = ressource["name"]
pID = ressource["id"] pID = ressource["id"]
print(f" [{i}] {pName} - {pID}") #print(f" [{i}] {pName} - {pID}")
url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name" url2 = f"https://api.spiget.org/v2/resources/{pID}/versions?size=100&sort=-name"
packageVersions = doAPIRequest(url2) packageVersions = doAPIRequest(url2)
for updates in packageVersions: for updates in packageVersions:
@ -98,9 +148,6 @@ def getInstalledPlugin(localFileName, localFileVersion):
updateId = updates["id"] updateId = updates["id"]
plugin_is_outdated = compareVersions(pID, updateVersion) plugin_is_outdated = compareVersions(pID, updateVersion)
addToPluginList(pID, updateId, plugin_is_outdated) addToPluginList(pID, updateId, plugin_is_outdated)
print(updateId)
print(pID)
print("Found match")
break break
i = i + 1 i = i + 1
return pluginID return pluginID

View File

@ -1,6 +1,5 @@
# Handles the web requests # Handles the web requests
import requests import requests
import cloudscraper
#import urllib.request #import urllib.request
@ -9,8 +8,3 @@ def doAPIRequest(url):
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
packageDetails = response.json() packageDetails = response.json()
return packageDetails return packageDetails
def createCloudScraperInstance():
global CLOUDSCRAPER
CLOUDSCRAPER = cloudscraper.create_scraper() # returns a CloudScraper instance
return CLOUDSCRAPER