Fixed input handling and added web_request file to simplify the code

This commit is contained in:
Neocky 2021-03-05 00:09:03 +01:00
parent 91e66792a3
commit f44f06a803
4 changed files with 58 additions and 45 deletions

View File

@ -5,16 +5,16 @@ from plugin_updatechecker import getInstalledPackages
def inputOption(inputOptionString): def inputOption(inputOptionString):
if inputOptionString == "1": inputString = None
ressourceID = input("SpigotMC Ressource ID: ") print(inputOptionString)
return ressourceID if inputOptionString == 1:
if inputOptionString == "2": inputString = input("SpigotMC Ressource ID: ")
ressourceId = input("SpigotMC Ressource ID: ") if inputOptionString == 2:
return ressourceId inputString = input("SpigotMC Ressource ID: ")
if inputOptionString == "3": if inputOptionString == 3:
ressourceName = input(" SpigotMC Ressource Name: ") inputString = input(" SpigotMC Ressource Name: ")
print("ich bin ein test") print("ich bin ein test")
return ressourceName return inputString
def handleInput(inputString): def handleInput(inputString):
@ -39,7 +39,6 @@ def inputMainMenu():
handleInput(inputSt) handleInput(inputSt)
def outputTest(): def outputTest():
print("Hello world") print("Hello world")
print("Waiting still seconds: 5", end='\r') print("Waiting still seconds: 5", end='\r')

View File

@ -1,13 +1,13 @@
import urllib.request import urllib.request
import cgi import cgi
import re import re
import requests from web_request import doAPIRequest
def calculateFileSize(downloadFileSize): def calculateFileSize(downloadFileSize):
fileSizeDownload = int(downloadFileSize) fileSizeDownload = int(downloadFileSize)
fileSizeMb = fileSizeDownload / 1024 / 1024 fileSizeKb = fileSizeDownload / 1024
roundedFileSize = round(fileSizeMb, 2) roundedFileSize = round(fileSizeKb, 2)
return roundedFileSize return roundedFileSize
@ -30,7 +30,7 @@ def downloadPackageManual():
filename = params["filename"] filename = params["filename"]
# creating file path # creating file path
path = r"C:\\Users\USER\Desktop\\" path = r"C:\\Users\Jan-Luca\Desktop\\"
ppath = path + filename ppath = path + filename
# download file # download file
@ -61,18 +61,18 @@ def handleRegexPackageName(packageNameFull):
return packageNameOnly return packageNameOnly
def getlatestVersion(packageId): def getlatestVersion(packageId):
response = requests.get(f"https://api.spiget.org/v2/resources/{packageId}/versions/latest") url = f"https://api.spiget.org/v2/resources/{packageId}/versions/latest"
#packageDetails = response.json() response = doAPIRequest(url)
packageVersion = response.json()["name"] packageVersion = response["name"]
return packageVersion return packageVersion
def apiCallTest(ressourceId): def apiCallTest(ressourceId):
response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}") url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = response.json() response = doAPIRequest(url)
print(packageDetails) print(response)
packageName = response.json()["name"] packageName = response["name"]
packageTag = response.json()["tag"] packageTag = response["tag"]
print(packageName) print(packageName)
print(packageTag) print(packageTag)
packageNameNew = handleRegexPackageName(packageName) packageNameNew = handleRegexPackageName(packageName)
@ -87,21 +87,23 @@ def compareVersions():
def searchPackage(ressourceName): def searchPackage(ressourceName):
response = requests.get(f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name") url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name"
#https://api.spiget.org/v2/search/resources/luckperms?field=name packageName = doAPIRequest(url)
print(f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name") print(url)
packageName = response.json()
i = 1 i = 1
print("Index / Name / Description / Downloads")
for ressource in packageName: for ressource in packageName:
pName = ressource["name"] pName = ressource["name"]
print(f" [{i}] {pName}") pTag = ressource["tag"]
pDownloads = ressource["downloads"]
print(f" [{i}] {pName} / {pTag}/ {pDownloads}")
i = i + 1 i = i + 1
ressourceSelected = int(input(" Select your wanted Ressource: ")) ressourceSelected = int(input(" Select your wanted Ressource: "))
ressourceSelected = ressourceSelected - 1 ressourceSelected = ressourceSelected - 1
fileInfo = response.json()[ressourceSelected]["file"] fileInfo = packageName[ressourceSelected]["file"]
packageUrl = fileInfo["url"] packageUrl = fileInfo["url"]
ressourceId = response.json()[ressourceSelected]["id"] ressourceId = packageName[ressourceSelected]["id"]
print(packageUrl) print(packageUrl)
print(ressourceId) print(ressourceId)
@ -115,15 +117,15 @@ def downloadLatestVersion(ressourceId, packageDownloadName, sourcePath):
filesizeData = calculateFileSize(filesize) filesizeData = calculateFileSize(filesize)
print(filesizeData) print(filesizeData)
print(filesize) print(filesize)
print(f" Downloadsize: {filesizeData} MB") print(f" Downloadsize: {filesizeData} KB")
def getLatestPackageVersion(ressourceId, downloadPath): 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}") url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = response.json() packageDetails = doAPIRequest(url)
packageName = packageDetails["name"] packageName = packageDetails["name"]
packageTag = packageDetails["tag"] #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"
@ -132,10 +134,10 @@ def getLatestPackageVersion(ressourceId, downloadPath):
def getLatestPackageVersionInteractive(downloadPath): def getLatestPackageVersionInteractive(downloadPath):
ressourceId = input(" SpigotMC Ressource ID: ") ressourceId = input(" SpigotMC Ressource ID: ")
response = requests.get(f"https://api.spiget.org/v2/resources/{ressourceId}") url = f"https://api.spiget.org/v2/resources/{ressourceId}"
packageDetails = response.json() packageDetails = doAPIRequest(url)
packageName = packageDetails["name"] packageName = packageDetails["name"]
packageTag = packageDetails["tag"] #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

@ -1,9 +1,9 @@
import os import os
import re import re
import requests
from consoleoutput import oColors #consoleTitle, clearConsole from consoleoutput import oColors #consoleTitle, clearConsole
from plugin_downloader import getLatestPackageVersion #handleInput from plugin_downloader import getLatestPackageVersion #handleInput
from web_request import doAPIRequest
def createPluginList(): def createPluginList():
@ -35,8 +35,8 @@ def getFileVersion(pluginName):
def compareVersions(pluginId, pluginVersion): def compareVersions(pluginId, pluginVersion):
responseUpdateSearch = requests.get(f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest") url = f"https://api.spiget.org/v2/resources/{pluginId}/versions/latest"
latestUpdateSearch = responseUpdateSearch.json() latestUpdateSearch = doAPIRequest(url)
versionLatestUpdate = latestUpdateSearch["name"] versionLatestUpdate = latestUpdateSearch["name"]
print(pluginVersion) print(pluginVersion)
print(versionLatestUpdate) print(versionLatestUpdate)
@ -67,17 +67,20 @@ def getInstalledPackages(pluginFolderPath):
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 INSTALLEDPLUGINLIST[i][2] == True:
os.remove(f"C:\\Users\\USER\\Desktop\\plugins\\{plugin}")
getLatestPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\") getLatestPackageVersion(pluginId, r"C:\\Users\\USER\\Desktop\\plugins\\")
os.remove(f"C:\\Users\USER\\Desktop\\plugins\\{plugin}") 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):
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")
url = "https://api.spiget.org/v2/search/resources/" + localFileName + "?field=name"
packageName = doAPIRequest(url)
#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")
packageName = response.json() #packageName = response.json()
i = 1 i = 1
plugin_match_found = False plugin_match_found = False
pluginID = None pluginID = None
@ -87,8 +90,8 @@ def getInstalledPlugin(localFileName, localFileVersion):
pName = ressource["name"] pName = ressource["name"]
pID = ressource["id"] pID = ressource["id"]
print(f" [{i}] {pName} - {pID}") print(f" [{i}] {pName} - {pID}")
response2 = requests.get(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 = response2.json() packageVersions = doAPIRequest(url2)
for updates in packageVersions: for updates in packageVersions:
updateVersion = updates["name"] updateVersion = updates["name"]
if localFileVersion == updateVersion: if localFileVersion == updateVersion:
@ -105,7 +108,7 @@ def getInstalledPlugin(localFileName, localFileVersion):
return pluginID return pluginID
# start query # start query
# get id # get id
# search with id for all version upates # search with id for all version upates
# get version that matches installed version # get version that matches installed version

9
pluGET/web_request.py Normal file
View File

@ -0,0 +1,9 @@
import requests
#import urllib.request
def doAPIRequest(url):
headers = {'user-agent': 'pluGET'}
response = requests.get(url, headers=headers)
packageDetails = response.json()
return packageDetails