99 lines
4.4 KiB
Python
Executable File
99 lines
4.4 KiB
Python
Executable File
#!/bin/python
|
|
# Setup Parser
|
|
import requests
|
|
#import socket
|
|
#from request_wrapper import requests_wrapper as requests
|
|
from re import sub
|
|
import parsers.paperMC
|
|
import parsers.modrinth
|
|
import argparse
|
|
import shutil
|
|
import hashlib
|
|
from os import error
|
|
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('project', metavar='project', help='which project to query for')
|
|
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('-o', nargs="?", help='Optional: Specify output filepath')
|
|
args = parser.parse_args()
|
|
print("mcUp.py, written by Caleb Fontenot")
|
|
print(args.o)
|
|
# PaperMC command functions
|
|
def paperMC(project, action, subAction):
|
|
if action == "get":
|
|
if subAction == "versions":
|
|
print(parsers.paperMC.getVersions(project))
|
|
elif subAction == "latest":
|
|
latestVersion = parsers.paperMC.getLatest(project)
|
|
buildNumber = parsers.paperMC.getBuildNumber(project, latestVersion)
|
|
print("Latest version of "+project+" is "+latestVersion+" build #"+str(buildNumber[-1]))
|
|
if action == "download":
|
|
if subAction == "latest":
|
|
latestVersion = parsers.paperMC.getLatest(project)
|
|
output = parsers.paperMC.getJarInfo(project, latestVersion)
|
|
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 open(output_file, 'wb') as file_object:
|
|
shutil.copyfileobj(raw.raw, file_object)
|
|
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
|
|
# modrinth command functions
|
|
def modrinth(project, action, subAction):
|
|
if action == "get":
|
|
if subAction == "info":
|
|
print(parsers.modrinth.modInfo(project))
|
|
if subAction == "version_info":
|
|
print(parsers.modrinth.getAllModVersionInfo(project))
|
|
if subAction == "versions":
|
|
print(parsers.modrinth.determine(project, "version_number"))
|
|
if subAction == "latest":
|
|
print(parsers.modrinth.getLatestVersion(project))
|
|
if subAction == "get_URL":
|
|
print(parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project)))
|
|
|
|
if action == "download":
|
|
if subAction == "latest":
|
|
latestVersion = parsers.modrinth.getLatestVersion(project)
|
|
output = parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project))
|
|
response = requests.get(output[0], stream=True, timeout=1)
|
|
if args.o != None: # Check if user set an output filepath
|
|
output_file = args.o
|
|
else:
|
|
output_file = output[2]
|
|
with response as raw:
|
|
with open(output_file, 'wb') as file_object:
|
|
shutil.copyfileobj(raw.raw, file_object)
|
|
print("Downloaded "+latestVersion+" to "+output_file)
|
|
#Calculate hash, compare with API given hash
|
|
h_sha1 = hashlib.sha1()
|
|
with open(output_file, 'rb') as file_object:
|
|
chunk = 0
|
|
while chunk != b'':
|
|
chunk = file_object.read(1024)
|
|
h_sha1.update(chunk)
|
|
if h_sha1.hexdigest() == output[1]:
|
|
print("sha1sum of downloaded file matches the sum that the API gave, jar is safe to use")
|
|
else:
|
|
raise error
|
|
|
|
# Determine which API parser to use:
|
|
if args.api == "paperMC":
|
|
paperMC(args.project, args.action, args.subAction)
|
|
elif args.api == "modrinth":
|
|
modrinth(args.project, args.action, args.subAction)
|
|
else:
|
|
print("Error: Unknown API: "+args.api) |