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
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(" [4] Download latest version of package")
#print(" [5] Check update for installed plugins")
print()
#print()

View File

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

View File

@ -1,7 +1,6 @@
import urllib.request
import cgi
import re
import cloudscraper
from web_request import doAPIRequest
@ -52,13 +51,12 @@ def handleRegexPackageName(packageNameFull):
packageNameFull2 = packageNameFull.replace(unwantedpackageNameString, '')
print(packageNameFull2)
print("packageNameFull2")
# 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(' ', '')
print(packageNameOnly)
print("packageNameOnly")
#print(packageNameOnly)
#print("packageNameOnly")
return packageNameOnly
@ -81,14 +79,14 @@ def apiCallTest(ressourceId):
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):
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"
versionList = doAPIRequest(url)
@ -99,6 +97,7 @@ def getVersionID(packageId, 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)
@ -128,74 +127,39 @@ def searchPackage(ressourceName):
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}/versions/latest/download" #throws 403 forbidden error
remotefile = urllib.request.urlopen(url)
filesize = remotefile.info()['Content-Length']
downloadPath = sourcePath + packageDownloadName
urllib.request.urlretrieve(url, downloadPath)
filesizeData = calculateFileSize(filesize)
print(filesizeData)
print(filesize)
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
print(f"File downloaded here: {downloadPath}")
print(f"Downloadsize: {filesizeData} KB")
#local_filename = url.split('/')[-1]
# 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()
def getPackageVersion(ressourceId, downloadPath, inputPackageVersion='latest'):
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = doAPIRequest(url)
packageName = packageDetails["name"]
#packageTag = packageDetails["tag"]
packageNameNew = handleRegexPackageName(packageName)
versionId = getVersionID(ressourceId, packageVersion)
versionId = getVersionID(ressourceId, inputPackageVersion)
packageVersion = getVersionName(ressourceId, versionId)
#packageVersion = getlatestVersion(ressourceId)
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
downloadSpecificVersion(ressourceId, packageDownloadName, versionId, downloadPath)
downloadPackagePath = downloadPath + packageDownloadName
def getLatestPackageVersionInteractive(downloadPath):
ressourceId = input(" SpigotMC Ressource ID: ")
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
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
if inputPackageVersion is None or inputPackageVersion == 'latest':
downloadSpecificVersion(ressourceId=ressourceId, downloadPath=downloadPackagePath)
else:
downloadSpecificVersion(ressourceId, downloadPackagePath, versionId)
# get latest update > https://api.spiget.org/v2/resources/28140/updates/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"
latestUpdateSearch = doAPIRequest(url)
versionLatestUpdate = latestUpdateSearch["name"]
print(pluginVersion)
print(versionLatestUpdate)
if pluginVersion != versionLatestUpdate:
plugin_is_outdated = True
else:
@ -47,7 +45,48 @@ def compareVersions(pluginId, pluginVersion):
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()
pluginList = os.listdir(pluginFolderPath)
print(pluginList)
@ -57,27 +96,38 @@ def getInstalledPackages(pluginFolderPath):
fileName = getFileName(plugin)
fileVersion = getFileVersion(plugin)
pluginId = getInstalledPlugin(fileName, fileVersion)
pluginIdStr = str(pluginId)
print(f"name: {fileName}")
print(f"version: {fileVersion}")
# debug purpose
print(inputSelectedObject)
print(INSTALLEDPLUGINLIST)
print(f"pluginId: {pluginId}")
if pluginId == None:
print(oColors.brightRed + "Couldn't find plugin id. Sorry :(" + oColors.standardWhite)
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}")
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
print(INSTALLEDPLUGINLIST[1][0])
#print(INSTALLEDPLUGINLIST[1][0])
#getLatestPackageVersion(pluginID, r"C:\\Users\USER\Desktop\\plugins\\")
def getInstalledPlugin(localFileName, localFileVersion):
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
packageName = doAPIRequest(url)
print("https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name")
#packageName = response.json()
i = 1
plugin_match_found = False
@ -87,7 +137,7 @@ def getInstalledPlugin(localFileName, localFileVersion):
break
pName = ressource["name"]
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"
packageVersions = doAPIRequest(url2)
for updates in packageVersions:
@ -98,9 +148,6 @@ def getInstalledPlugin(localFileName, localFileVersion):
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

View File

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