From 7e7b46c36b86cc0a22451811030a06c1159d1047 Mon Sep 17 00:00:00 2001 From: Evgenij Titarenko Date: Sat, 1 Oct 2022 11:16:47 +0300 Subject: [PATCH] Initial api realization --- api.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ mc-get.py | 16 ++++++++++++---- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 api.py diff --git a/api.py b/api.py new file mode 100644 index 0000000..2b077aa --- /dev/null +++ b/api.py @@ -0,0 +1,51 @@ +#from objects.api-objects import * +import requests +from tqdm import tqdm + +API_VERSION = "v2" +HEADERS = { + 'User-Agent': 'mc-get-testing' +} + +def download(file_url:str, file_size:int): + resp = requests.get(file_url, stream=True, headers=HEADERS) + with open(file_url.split("/")[-1],'wb') as file: + for data in tqdm(resp.iter_content(), total=file_size,\ + unit_scale=True, unit="byte"): + file.write(data) + +def __method(method:str, api_version:str=API_VERSION): + api_url = "https://api.modrinth.com" + def request(**args): + sub_method = "" + if "project" in args: + sub_method = "/" + args.pop("project") + elif "version" in args: + sub_method = "/" + args.pop("version") + print(f"{api_url}/{api_version}{method}{sub_method}") + resp = requests.get(f"{api_url}/{api_version}{method}{sub_method}",\ + params=args, headers=HEADERS) + match resp.status_code: + case 200: + print("200: OK") + case 400: + #invalid request + print("400: ERROR") + case 401: + # No autorization + print("401: ERROR") + case 404: + ''' + The requested project was not found or + no authorization to see this project + ''' + print("404: ERROR") + return type(method, (object,), resp.json()) + return request + +test = __method("", "") +search = __method("/search") +project = __method("/project") +version = __method("/version") + + diff --git a/mc-get.py b/mc-get.py index e14ccf9..cd9dba8 100644 --- a/mc-get.py +++ b/mc-get.py @@ -1,10 +1,18 @@ import argparse +import api +#import urllib.request def validate(): pass -def install(): - pass +def install(projects:list): + for project in projects: + project_data = api.project(project=project) + version = api.version(version=project_data.versions[0]) + file = version.files[0].get("url", "NO URL") + print(file) + api.download(file, version.files[0].get("size", 0)) + #urllib.request.urlretrieve(file) def search(): pass @@ -17,11 +25,11 @@ validate - validate mods installation\n search - search mods''' formatter_class=argparse.RawTextHelpFormatter) parser.add_argument("method", choices=['install', 'search', 'validate'],\ metavar="method") - parser.add_argument("--mversion", help="Minecraft version") + parser.add_argument("method_args", nargs="*") args = parser.parse_args() match args.method: case "install": - install() + install(args.method_args) case "search": search() case "validate":