Cleaned up code, added sha256sum checking, and added -o
This commit is contained in:
parent
f663618a9a
commit
dcbbdb63f2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
*.jar
|
*.jar
|
||||||
|
__pycache__
|
||||||
|
39
mcUp.py
39
mcUp.py
@ -3,14 +3,17 @@
|
|||||||
import parsers.paperMC
|
import parsers.paperMC
|
||||||
import argparse
|
import argparse
|
||||||
import shutil
|
import shutil
|
||||||
|
import hashlib
|
||||||
|
from os import error
|
||||||
parser = argparse.ArgumentParser(description='A command-line tool to update a Minecraft Server.')
|
parser = argparse.ArgumentParser(description='A command-line tool to update a Minecraft Server.')
|
||||||
parser.add_argument('api', metavar='api', help='which API to use')
|
parser.add_argument('api', metavar='api', help='which API to use')
|
||||||
parser.add_argument('project', metavar='project', help='which project to query for')
|
parser.add_argument('project', metavar='project', help='which project to query for')
|
||||||
parser.add_argument('action', metavar='action', help='what action to execute')
|
parser.add_argument('action', metavar='action', help='what action to execute')
|
||||||
parser.add_argument('subAction', metavar='subAction', help='what sub action to execute')
|
parser.add_argument('subAction', metavar='subAction', help='what sub action to execute')
|
||||||
|
parser.add_argument('-o', nargs="?", help='Optional: Specify output filepath')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
print("mcUp.py, written by Caleb Fontenot")
|
print("mcUp.py, written by Caleb Fontenot")
|
||||||
|
print(args.o)
|
||||||
# PaperMC command functions
|
# PaperMC command functions
|
||||||
def paperMC(project, action, subAction):
|
def paperMC(project, action, subAction):
|
||||||
if action == "get":
|
if action == "get":
|
||||||
@ -24,26 +27,26 @@ def paperMC(project, action, subAction):
|
|||||||
if subAction == "latest":
|
if subAction == "latest":
|
||||||
latestVersion = parsers.paperMC.getLatest(project)
|
latestVersion = parsers.paperMC.getLatest(project)
|
||||||
output = parsers.paperMC.getJarInfo(project, latestVersion)
|
output = parsers.paperMC.getJarInfo(project, latestVersion)
|
||||||
print(output["name"])
|
if args.o != None: # Check if user set an output filepath
|
||||||
|
output_file = args.o
|
||||||
|
else:
|
||||||
|
output_file = output["name"]
|
||||||
with parsers.paperMC.downloadVersion(project, latestVersion) as raw:
|
with parsers.paperMC.downloadVersion(project, latestVersion) as raw:
|
||||||
with open(output["name"], 'wb') as file_object:
|
with open(output_file, 'wb') as file_object:
|
||||||
shutil.copyfileobj(raw.raw, file_object)
|
shutil.copyfileobj(raw.raw, file_object)
|
||||||
print("Downloaded "+latestVersion+" to "+output["name"])
|
print("Downloaded "+latestVersion+" to "+output_file)
|
||||||
|
#Calculate hash, compare with API given hash
|
||||||
|
h_sha256 = hashlib.sha256()
|
||||||
|
with open(output_file, 'rb') as file_object:
|
||||||
|
chunk = 0
|
||||||
|
while chunk != b'':
|
||||||
|
chunk = file_object.read(1024)
|
||||||
|
h_sha256.update(chunk)
|
||||||
|
if h_sha256.hexdigest() == output["sha256"]:
|
||||||
|
print("sha256sum of downloaded file matches the sum that the API gave, jar is safe to use")
|
||||||
|
else:
|
||||||
|
raise error
|
||||||
|
|
||||||
|
|
||||||
#latestVersion = parsers.paperMC.getLatest(project)
|
|
||||||
#jarInfo = parsers.paperMC.getJarInfo(project, latestVersion)
|
|
||||||
#print(jarInfo)
|
|
||||||
#print(jarInfo[0])
|
|
||||||
#print(type(jarInfo))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#print(type(getVersionVelocity))
|
|
||||||
|
|
||||||
#print("Latest build of velocity is", parsers.paperMC.getLatest#("velocity"))
|
|
||||||
|
|
||||||
# Determine which API to use:
|
# Determine which API to use:
|
||||||
if args.api == "paperMC":
|
if args.api == "paperMC":
|
||||||
|
Binary file not shown.
@ -1,3 +1,4 @@
|
|||||||
|
from os import error
|
||||||
import requests
|
import requests
|
||||||
debug = False
|
debug = False
|
||||||
if debug == True:
|
if debug == True:
|
||||||
@ -10,12 +11,15 @@ if debug == True:
|
|||||||
requests_log.propagate = True
|
requests_log.propagate = True
|
||||||
timeoutTime = 1
|
timeoutTime = 1
|
||||||
base_api_url = "https://papermc.io/api/v2"
|
base_api_url = "https://papermc.io/api/v2"
|
||||||
|
def failCheck(response):
|
||||||
|
print("Status Code is: "+str(response.status_code))
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise error
|
||||||
# This function returns the versions from a project from the Paper API.
|
# This function returns the versions from a project from the Paper API.
|
||||||
def getVersions(project):
|
def getVersions(project):
|
||||||
print("Calling getVersions()...")
|
print("Calling getVersions()...")
|
||||||
print("Accessing "+base_api_url+"/projects/"+project+"...")
|
|
||||||
response = requests.get(base_api_url+"/projects/"+project, timeout=timeoutTime)
|
response = requests.get(base_api_url+"/projects/"+project, timeout=timeoutTime)
|
||||||
print("Status Code is: "+str(response.status_code))
|
failCheck(response)
|
||||||
api_response = response.json()
|
api_response = response.json()
|
||||||
return api_response['versions']
|
return api_response['versions']
|
||||||
|
|
||||||
@ -27,7 +31,7 @@ def getLatest(project):
|
|||||||
def getBuildNumber(project, version):
|
def getBuildNumber(project, version):
|
||||||
print("Calling getBuildNumber()...")
|
print("Calling getBuildNumber()...")
|
||||||
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version, timeout=timeoutTime)
|
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version, timeout=timeoutTime)
|
||||||
print("Status Code is: "+str(response.status_code))
|
failCheck(response)
|
||||||
api_response = response.json()
|
api_response = response.json()
|
||||||
return api_response['builds']
|
return api_response['builds']
|
||||||
|
|
||||||
@ -35,8 +39,8 @@ def getBuildInfo(project, version):
|
|||||||
print("Calling getBuildInfo()...")
|
print("Calling getBuildInfo()...")
|
||||||
buildID1 = getBuildNumber(project, version)
|
buildID1 = getBuildNumber(project, version)
|
||||||
buildID = str(buildID1[-1])
|
buildID = str(buildID1[-1])
|
||||||
print(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID)
|
|
||||||
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID, timeout=timeoutTime)
|
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID, timeout=timeoutTime)
|
||||||
|
failCheck(response)
|
||||||
api_response = response.json()
|
api_response = response.json()
|
||||||
return api_response
|
return api_response
|
||||||
|
|
||||||
@ -45,7 +49,6 @@ def getJarInfo(project, version):
|
|||||||
buildInfo = getBuildInfo(project, version)
|
buildInfo = getBuildInfo(project, version)
|
||||||
workingDict = buildInfo["downloads"]
|
workingDict = buildInfo["downloads"]
|
||||||
workingDict2 = workingDict["application"]
|
workingDict2 = workingDict["application"]
|
||||||
print(workingDict2)
|
|
||||||
return workingDict2
|
return workingDict2
|
||||||
|
|
||||||
def downloadVersion(project, version):
|
def downloadVersion(project, version):
|
||||||
@ -54,4 +57,6 @@ def downloadVersion(project, version):
|
|||||||
buildID1 = getBuildNumber(project, version)
|
buildID1 = getBuildNumber(project, version)
|
||||||
buildID = str(buildID1[-1])
|
buildID = str(buildID1[-1])
|
||||||
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID+"/downloads/"+jarName["name"], stream=True, timeout=timeoutTime)
|
response = requests.get(base_api_url+"/projects/"+project+"/versions/"+version+"/builds/"+buildID+"/downloads/"+jarName["name"], stream=True, timeout=timeoutTime)
|
||||||
|
failCheck(response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user