From 2ebc84ca2ce0c54f7ac6a89a4e9217e1cf8b87b9 Mon Sep 17 00:00:00 2001 From: Evgenij Titarenko Date: Sat, 1 Oct 2022 18:49:58 +0300 Subject: [PATCH] Initial install method implementation --- api.py | 14 ++++++++++---- mc-get.py | 37 ++++++++++++++++++++++++++++++++----- mcfs.py | 5 +++++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/api.py b/api.py index 2b077aa..2f5ea11 100644 --- a/api.py +++ b/api.py @@ -1,15 +1,16 @@ #from objects.api-objects import * import requests from tqdm import tqdm +import json API_VERSION = "v2" HEADERS = { 'User-Agent': 'mc-get-testing' } -def download(file_url:str, file_size:int): +def download(file_url:str, file_size:int, path:str): resp = requests.get(file_url, stream=True, headers=HEADERS) - with open(file_url.split("/")[-1],'wb') as file: + with open(path,'wb') as file: for data in tqdm(resp.iter_content(), total=file_size,\ unit_scale=True, unit="byte"): file.write(data) @@ -25,9 +26,14 @@ def __method(method:str, api_version:str=API_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) + print(resp.headers.get("X-Ratelimit-Remaining",-1)) match resp.status_code: case 200: print("200: OK") + if type(resp.json()) == list: + return [type(method, (object,), obj)\ + for obj in resp.json()] + return type(method, (object,), resp.json()) case 400: #invalid request print("400: ERROR") @@ -40,12 +46,12 @@ def __method(method:str, api_version:str=API_VERSION): no authorization to see this project ''' print("404: ERROR") - return type(method, (object,), resp.json()) + resp.raise_for_status() return request test = __method("", "") search = __method("/search") project = __method("/project") version = __method("/version") - +versions = __method("/versions") diff --git a/mc-get.py b/mc-get.py index d3ea972..83db306 100644 --- a/mc-get.py +++ b/mc-get.py @@ -1,18 +1,45 @@ import argparse import api import mcfs +import npyscreen +import os def validate(): pass +def version_selector_GUI(vers:list, project:str): + def form(*args): + F = npyscreen.Form(name=f"Select {project} version") + sel = F.add(npyscreen.TitleSelectOne, value=[1,], name="versions:",\ + values=[ver.version_number + " for " +\ + ", ".join(ver.game_versions)\ + for ver in vers[::-1]], scroll_exit=True) + F.edit() + for ver in vers: + if ver.version_number == sel.get_selected_objects()[0].split()[0]: + return ver + return vers[0] + return form + def install(projects:list): to_install = [] 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)) + to_install.append(project_data) + for project in to_install: + versions = api.versions(ids=str(project.versions).replace("'", '"')) #i hate this + version = npyscreen.wrapper_basic(version_selector_GUI(versions,\ + project.slug)) + file = type("mc_file", (object, ), version.files[0]) + filename = file.url.split("/")[-1] + cache_file_path = os.path.join(mcfs.cache_dir, filename) + if not mcfs.is_path_exist(mcfs.cache_dir): + os.mkdir(mcfs.cache_dir) + if not mcfs.is_path_exist(cache_file_path): + api.download(file.url, file.size, cache_file_path) + else: + print(f"{filename} is in cache.") + mcfs.install(filename) def search(): pass @@ -29,7 +56,7 @@ validate - validate mods installation\n search - search mods''' args = parser.parse_args() match args.method: case "install": - pass + install(args.method_args) case "search": search() case "validate": diff --git a/mcfs.py b/mcfs.py index f8333ee..76bd394 100644 --- a/mcfs.py +++ b/mcfs.py @@ -1,5 +1,6 @@ import os from sys import platform +import shutil def __get_mc_dir(): directory = "" @@ -35,5 +36,9 @@ def is_path_exist(path:str): def is_standart_dir_structure(): return not os.path.exists(os.path.join(directory, "home")) +def install(filename): + shutil.copy2(os.path.join(cache_dir,filename),\ + os.path.join(mc_dir,"mods",filename)) + mc_dir = __get_mc_dir() cache_dir = __get_cache_dir()