2021-02-26 00:22:16 +00:00
|
|
|
import urllib.request
|
|
|
|
import cgi
|
2021-03-01 22:39:48 +00:00
|
|
|
import re
|
2021-03-07 00:49:57 +00:00
|
|
|
import cloudscraper
|
2021-03-04 23:09:03 +00:00
|
|
|
from web_request import doAPIRequest
|
2021-03-03 16:13:43 +00:00
|
|
|
|
2021-02-26 00:22:16 +00:00
|
|
|
|
|
|
|
def calculateFileSize(downloadFileSize):
|
|
|
|
fileSizeDownload = int(downloadFileSize)
|
2021-03-04 23:09:03 +00:00
|
|
|
fileSizeKb = fileSizeDownload / 1024
|
|
|
|
roundedFileSize = round(fileSizeKb, 2)
|
2021-02-26 00:22:16 +00:00
|
|
|
return roundedFileSize
|
|
|
|
|
|
|
|
|
|
|
|
# 28140 for Luckperms (Testing only)
|
2021-03-01 22:39:48 +00:00
|
|
|
def downloadPackageManual():
|
|
|
|
ressourceId = input("SpigotMC Ressource ID: ")
|
|
|
|
|
|
|
|
url = "https://api.spiget.org/v2/resources/" + ressourceId + "/download"
|
|
|
|
#url2 = "https://api.spiget.org/v2/resources/" + ressourceId + "/versions/latest/download"
|
|
|
|
#print(url2)
|
|
|
|
#user_agent = 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'
|
|
|
|
#header = { 'User-agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11', 'Accept-Encoding': 'gzip, deflate, br' }
|
|
|
|
#req = urllib.request.Request(url2, headers=header)
|
|
|
|
#remotefile = urlopen(req)
|
|
|
|
remotefile = urllib.request.urlopen(url)
|
|
|
|
filecontent = remotefile.info()['Content-Disposition']
|
|
|
|
filesize = remotefile.info()['Content-Length']
|
|
|
|
# getting original filename
|
|
|
|
value, params = cgi.parse_header(filecontent)
|
|
|
|
filename = params["filename"]
|
2021-02-26 00:22:16 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
# creating file path
|
2021-03-04 23:09:03 +00:00
|
|
|
path = r"C:\\Users\Jan-Luca\Desktop\\"
|
2021-03-01 22:39:48 +00:00
|
|
|
ppath = path + filename
|
2021-02-26 00:22:16 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
# download file
|
|
|
|
urllib.request.urlretrieve(url, ppath)
|
2021-02-26 00:22:16 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
filesizeData = calculateFileSize(filesize)
|
|
|
|
print(f"Downloadsize: {filesizeData} MB")
|
2021-02-26 00:22:16 +00:00
|
|
|
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
# 89273
|
|
|
|
def handleRegexPackageName(packageNameFull):
|
|
|
|
packageNameFull2 = packageNameFull
|
|
|
|
# trims the part of the package that has for example "[1.1 Off]" in it
|
|
|
|
unwantedpackageName = re.search(r'(^\[+[a-zA-Z0-9\s\W*\.*\-*\+*\%*\,]*\]+)', packageNameFull)
|
|
|
|
unwantedpackageNamematch = bool(unwantedpackageName)
|
|
|
|
if unwantedpackageNamematch:
|
|
|
|
unwantedpackageNameString = unwantedpackageName.group()
|
|
|
|
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")
|
|
|
|
return packageNameOnly
|
2021-02-26 00:22:16 +00:00
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
def getlatestVersion(packageId):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{packageId}/versions/latest"
|
|
|
|
response = doAPIRequest(url)
|
|
|
|
packageVersion = response["name"]
|
2021-03-01 22:39:48 +00:00
|
|
|
return packageVersion
|
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def apiCallTest(ressourceId):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
|
|
|
|
response = doAPIRequest(url)
|
|
|
|
print(response)
|
|
|
|
packageName = response["name"]
|
|
|
|
packageTag = response["tag"]
|
2021-03-01 22:39:48 +00:00
|
|
|
print(packageName)
|
|
|
|
print(packageTag)
|
|
|
|
packageNameNew = handleRegexPackageName(packageName)
|
|
|
|
print(packageNameNew)
|
2021-02-27 00:36:16 +00:00
|
|
|
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
# check version
|
|
|
|
def compareVersions():
|
|
|
|
#https://api.spiget.org/v2/resources/28140/versions/latest
|
|
|
|
# compare latest package version with installed package version
|
|
|
|
print("compareVersions")
|
|
|
|
|
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
def getVersionID(packageId, packageVersion):
|
|
|
|
url = f"https://api.spiget.org/v2/resources/{packageId}/versions?size=100&sort=-name"
|
|
|
|
versionList = doAPIRequest(url)
|
|
|
|
|
|
|
|
for packages in versionList:
|
|
|
|
packageUpdate = packages["name"]
|
|
|
|
versionId = packages["id"]
|
|
|
|
if packageUpdate == 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)
|
|
|
|
versionName = response["name"]
|
|
|
|
return versionName
|
|
|
|
|
|
|
|
|
2021-03-03 22:25:44 +00:00
|
|
|
def searchPackage(ressourceName):
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/search/resources/{ressourceName}?field=name"
|
|
|
|
packageName = doAPIRequest(url)
|
|
|
|
print(url)
|
2021-03-01 22:39:48 +00:00
|
|
|
i = 1
|
2021-03-04 23:09:03 +00:00
|
|
|
print("Index / Name / Description / Downloads")
|
2021-03-01 22:39:48 +00:00
|
|
|
for ressource in packageName:
|
|
|
|
pName = ressource["name"]
|
2021-03-04 23:09:03 +00:00
|
|
|
pTag = ressource["tag"]
|
|
|
|
pDownloads = ressource["downloads"]
|
|
|
|
print(f" [{i}] {pName} / {pTag}/ {pDownloads}")
|
2021-03-01 22:39:48 +00:00
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
ressourceSelected = int(input(" Select your wanted Ressource: "))
|
|
|
|
ressourceSelected = ressourceSelected - 1
|
2021-03-04 23:09:03 +00:00
|
|
|
fileInfo = packageName[ressourceSelected]["file"]
|
2021-03-01 22:39:48 +00:00
|
|
|
packageUrl = fileInfo["url"]
|
2021-03-04 23:09:03 +00:00
|
|
|
ressourceId = packageName[ressourceSelected]["id"]
|
2021-03-01 22:39:48 +00:00
|
|
|
print(packageUrl)
|
|
|
|
print(ressourceId)
|
|
|
|
|
2021-03-02 23:17:15 +00:00
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
def downloadLatestVersion(ressourceId, packageDownloadName, sourcePath):
|
2021-03-03 16:13:43 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{ressourceId}/download"
|
2021-03-01 22:39:48 +00:00
|
|
|
remotefile = urllib.request.urlopen(url)
|
|
|
|
filesize = remotefile.info()['Content-Length']
|
|
|
|
downloadPath = sourcePath + packageDownloadName
|
|
|
|
urllib.request.urlretrieve(url, downloadPath)
|
|
|
|
filesizeData = calculateFileSize(filesize)
|
2021-03-02 23:17:15 +00:00
|
|
|
print(filesizeData)
|
|
|
|
print(filesize)
|
2021-03-04 23:09:03 +00:00
|
|
|
print(f" Downloadsize: {filesizeData} KB")
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
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
|
|
|
|
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
#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):
|
2021-03-03 16:13:43 +00:00
|
|
|
#ressourceId = input(" SpigotMC Ressource ID: ")
|
2021-03-07 00:49:57 +00:00
|
|
|
CLOUDSCRAPER = createCloudScraperInstance()
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
|
|
|
|
packageDetails = doAPIRequest(url)
|
2021-03-03 22:25:44 +00:00
|
|
|
packageName = packageDetails["name"]
|
2021-03-04 23:09:03 +00:00
|
|
|
#packageTag = packageDetails["tag"]
|
2021-03-03 22:25:44 +00:00
|
|
|
packageNameNew = handleRegexPackageName(packageName)
|
2021-03-07 00:49:57 +00:00
|
|
|
versionId = getVersionID(ressourceId, packageVersion)
|
|
|
|
packageVersion = getVersionName(ressourceId, versionId)
|
|
|
|
#packageVersion = getlatestVersion(ressourceId)
|
2021-03-03 22:25:44 +00:00
|
|
|
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
|
2021-03-07 00:49:57 +00:00
|
|
|
downloadSpecificVersion(ressourceId, packageDownloadName, versionId, downloadPath)
|
2021-03-03 22:25:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getLatestPackageVersionInteractive(downloadPath):
|
|
|
|
ressourceId = input(" SpigotMC Ressource ID: ")
|
2021-03-04 23:09:03 +00:00
|
|
|
url = f"https://api.spiget.org/v2/resources/{ressourceId}"
|
|
|
|
packageDetails = doAPIRequest(url)
|
2021-03-03 22:25:44 +00:00
|
|
|
packageName = packageDetails["name"]
|
2021-03-04 23:09:03 +00:00
|
|
|
#packageTag = packageDetails["tag"]
|
2021-03-01 22:39:48 +00:00
|
|
|
packageNameNew = handleRegexPackageName(packageName)
|
|
|
|
packageVersion = getlatestVersion(ressourceId)
|
|
|
|
packageDownloadName = f"{packageNameNew}-{packageVersion}.jar"
|
|
|
|
downloadLatestVersion(ressourceId, packageDownloadName, downloadPath)
|
|
|
|
|
2021-03-07 00:49:57 +00:00
|
|
|
def createCloudScraperInstance():
|
|
|
|
global CLOUDSCRAPER
|
|
|
|
CLOUDSCRAPER = cloudscraper.create_scraper(interpreter='nodejs',debug=True) # returns a CloudScraper instance
|
|
|
|
return CLOUDSCRAPER
|
2021-03-01 22:39:48 +00:00
|
|
|
|
2021-02-27 00:36:16 +00:00
|
|
|
# get latest update > https://api.spiget.org/v2/resources/28140/updates/latest
|
|
|
|
# this also > https://api.spiget.org/v2/resources/28140/versions/latest
|
2021-03-01 22:39:48 +00:00
|
|
|
# get latest download with correct name > https://api.spiget.org/v2/resources/28140/versions/latest/download cloudflare protected
|
2021-02-27 00:36:16 +00:00
|
|
|
# query for a plugin https://api.spiget.org/v2/search/resources/luckperms?field=name
|