2021-12-14 18:09:16 -06:00
|
|
|
#!/bin/python
|
|
|
|
# Setup Parser
|
2022-02-26 18:07:53 -06:00
|
|
|
from ssl import HAS_ECDH
|
2021-12-19 23:42:31 -06:00
|
|
|
import requests
|
2021-12-20 09:38:59 -06:00
|
|
|
#import socket
|
|
|
|
#from request_wrapper import requests_wrapper as requests
|
2021-12-19 23:42:31 -06:00
|
|
|
from re import sub
|
2021-12-14 18:09:16 -06:00
|
|
|
import parsers.paperMC
|
2021-12-19 18:00:57 -06:00
|
|
|
import parsers.modrinth
|
2022-03-16 19:22:47 -05:00
|
|
|
import parsers.curseforge
|
2021-12-14 18:09:16 -06:00
|
|
|
import argparse
|
2021-12-14 20:02:16 -06:00
|
|
|
import shutil
|
2021-12-14 22:03:03 -06:00
|
|
|
import hashlib
|
|
|
|
from os import error
|
2022-02-26 18:07:53 -06:00
|
|
|
# Define Errors:
|
|
|
|
class MismatchSHA1Error(Exception):
|
|
|
|
pass
|
2021-12-14 18:09:16 -06:00
|
|
|
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')
|
2021-12-14 22:03:03 -06:00
|
|
|
parser.add_argument('-o', nargs="?", help='Optional: Specify output filepath')
|
2022-02-27 20:20:41 -06:00
|
|
|
parser.add_argument('-v', nargs="?", help='Specify Minecraft Version to target')
|
2021-12-14 18:09:16 -06:00
|
|
|
args = parser.parse_args()
|
|
|
|
print("mcUp.py, written by Caleb Fontenot")
|
2021-12-14 22:03:03 -06:00
|
|
|
print(args.o)
|
2022-02-27 20:20:41 -06:00
|
|
|
#Downloader function
|
|
|
|
def download(project, what):
|
|
|
|
versionToDownload = what
|
|
|
|
output = parsers.modrinth.getDownloadURL(project, what)
|
2022-03-16 21:04:49 -05:00
|
|
|
response = requests.get(output[0], stream=True, timeout=10)
|
2022-02-27 20:20:41 -06:00
|
|
|
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 "+versionToDownload+" 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)
|
|
|
|
print("API SHA 1: "+str(output[1]))
|
|
|
|
print("Our calculated SHA 1: "+str(h_sha1.hexdigest()))
|
|
|
|
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 MismatchSHA1Error
|
|
|
|
# determine if args.v has a value
|
|
|
|
def version_for_minecraft():
|
|
|
|
if args.v != None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2021-12-14 18:09:16 -06:00
|
|
|
# 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]))
|
2021-12-14 20:02:16 -06:00
|
|
|
if action == "download":
|
|
|
|
if subAction == "latest":
|
|
|
|
latestVersion = parsers.paperMC.getLatest(project)
|
|
|
|
output = parsers.paperMC.getJarInfo(project, latestVersion)
|
2021-12-14 22:03:03 -06:00
|
|
|
if args.o != None: # Check if user set an output filepath
|
|
|
|
output_file = args.o
|
|
|
|
else:
|
|
|
|
output_file = output["name"]
|
2021-12-14 20:02:16 -06:00
|
|
|
with parsers.paperMC.downloadVersion(project, latestVersion) as raw:
|
2021-12-14 22:03:03 -06:00
|
|
|
with open(output_file, 'wb') as file_object:
|
2021-12-14 20:02:16 -06:00
|
|
|
shutil.copyfileobj(raw.raw, file_object)
|
2021-12-14 22:03:03 -06:00
|
|
|
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
|
2021-12-19 18:00:57 -06:00
|
|
|
# modrinth command functions
|
|
|
|
def modrinth(project, action, subAction):
|
2021-12-19 23:42:31 -06:00
|
|
|
if action == "get":
|
2021-12-19 18:00:57 -06:00
|
|
|
if subAction == "info":
|
|
|
|
print(parsers.modrinth.modInfo(project))
|
2021-12-19 23:42:31 -06:00
|
|
|
if subAction == "version_info":
|
2021-12-19 18:00:57 -06:00
|
|
|
print(parsers.modrinth.getAllModVersionInfo(project))
|
2021-12-19 23:42:31 -06:00
|
|
|
if subAction == "versions":
|
|
|
|
print(parsers.modrinth.determine(project, "version_number"))
|
|
|
|
if subAction == "latest":
|
2022-03-16 19:13:00 -05:00
|
|
|
print(parsers.modrinth.getLatestVersion_by_number(project, args.v))
|
2022-02-26 19:40:45 -06:00
|
|
|
if subAction == "stable":
|
|
|
|
print(parsers.modrinth.getLatestStable(project))
|
2021-12-19 23:42:31 -06:00
|
|
|
if subAction == "get_URL":
|
|
|
|
print(parsers.modrinth.getDownloadURL(project, parsers.modrinth.getLatestVersion(project)))
|
|
|
|
if action == "download":
|
2022-02-26 19:40:45 -06:00
|
|
|
if subAction == "stable":
|
2022-02-27 20:20:41 -06:00
|
|
|
if version_for_minecraft() == True:
|
|
|
|
download(project, parsers.modrinth.getForMinecraftVersion(project, args.v, "stable"))
|
|
|
|
else:
|
|
|
|
download(project, parsers.modrinth.getLatestStable(project))
|
2021-12-19 23:42:31 -06:00
|
|
|
if subAction == "latest":
|
2022-02-27 20:20:41 -06:00
|
|
|
if version_for_minecraft() == True:
|
|
|
|
download(project, parsers.modrinth.getForMinecraftVersion(project, args.v, "latest"))
|
|
|
|
else:
|
|
|
|
download(project, parsers.modrinth.getLatestVersion(project))
|
|
|
|
|
|
|
|
#print(parsers.modrinth.getForMinecraftVersion(project, args.v))
|
2022-03-16 19:22:47 -05:00
|
|
|
# CurseForge command functions
|
|
|
|
def curseforge(project, action, subAction):
|
|
|
|
import json
|
|
|
|
if action == "get":
|
|
|
|
if subAction == "games":
|
|
|
|
print(parsers.curseforge.get_games())
|
|
|
|
if subAction == "mod":
|
|
|
|
print(json.dumps(parsers.curseforge.get_mod(project), indent=4))
|
|
|
|
if action == "search":
|
|
|
|
if subAction == "mod":
|
|
|
|
print(json.dumps(parsers.curseforge.search_mods(project), indent=4))
|
|
|
|
if action == "download":
|
|
|
|
if subAction == "latest":
|
|
|
|
print(parsers.curseforge.download(project))
|
2021-12-19 18:00:57 -06:00
|
|
|
# Determine which API parser to use:
|
2021-12-14 18:09:16 -06:00
|
|
|
if args.api == "paperMC":
|
|
|
|
paperMC(args.project, args.action, args.subAction)
|
2021-12-19 18:00:57 -06:00
|
|
|
elif args.api == "modrinth":
|
|
|
|
modrinth(args.project, args.action, args.subAction)
|
2022-03-16 19:22:47 -05:00
|
|
|
elif args.api == "curseforge":
|
|
|
|
curseforge(args.project, args.action, args.subAction)
|
2021-12-14 18:09:16 -06:00
|
|
|
else:
|
|
|
|
print("Error: Unknown API: "+args.api)
|