Initial install method implementation

This commit is contained in:
Евгений Титаренко 2022-10-01 18:49:58 +03:00
parent c339f1ea08
commit 2ebc84ca2c
3 changed files with 47 additions and 9 deletions

14
api.py
View file

@ -1,15 +1,16 @@
#from objects.api-objects import * #from objects.api-objects import *
import requests import requests
from tqdm import tqdm from tqdm import tqdm
import json
API_VERSION = "v2" API_VERSION = "v2"
HEADERS = { HEADERS = {
'User-Agent': 'mc-get-testing' '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) 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,\ for data in tqdm(resp.iter_content(), total=file_size,\
unit_scale=True, unit="byte"): unit_scale=True, unit="byte"):
file.write(data) 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}") print(f"{api_url}/{api_version}{method}{sub_method}")
resp = requests.get(f"{api_url}/{api_version}{method}{sub_method}",\ resp = requests.get(f"{api_url}/{api_version}{method}{sub_method}",\
params=args, headers=HEADERS) params=args, headers=HEADERS)
print(resp.headers.get("X-Ratelimit-Remaining",-1))
match resp.status_code: match resp.status_code:
case 200: case 200:
print("200: OK") 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: case 400:
#invalid request #invalid request
print("400: ERROR") print("400: ERROR")
@ -40,12 +46,12 @@ def __method(method:str, api_version:str=API_VERSION):
no authorization to see this project no authorization to see this project
''' '''
print("404: ERROR") print("404: ERROR")
return type(method, (object,), resp.json()) resp.raise_for_status()
return request return request
test = __method("", "") test = __method("", "")
search = __method("/search") search = __method("/search")
project = __method("/project") project = __method("/project")
version = __method("/version") version = __method("/version")
versions = __method("/versions")

View file

@ -1,18 +1,45 @@
import argparse import argparse
import api import api
import mcfs import mcfs
import npyscreen
import os
def validate(): def validate():
pass 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): def install(projects:list):
to_install = [] to_install = []
for project in projects: for project in projects:
project_data = api.project(project=project) project_data = api.project(project=project)
version = api.version(version=project_data.versions[0]) to_install.append(project_data)
file = version.files[0].get("url", "NO URL") for project in to_install:
print(file) versions = api.versions(ids=str(project.versions).replace("'", '"')) #i hate this
api.download(file, version.files[0].get("size", 0)) 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(): def search():
pass pass
@ -29,7 +56,7 @@ validate - validate mods installation\n search - search mods'''
args = parser.parse_args() args = parser.parse_args()
match args.method: match args.method:
case "install": case "install":
pass install(args.method_args)
case "search": case "search":
search() search()
case "validate": case "validate":

View file

@ -1,5 +1,6 @@
import os import os
from sys import platform from sys import platform
import shutil
def __get_mc_dir(): def __get_mc_dir():
directory = "" directory = ""
@ -35,5 +36,9 @@ def is_path_exist(path:str):
def is_standart_dir_structure(): def is_standart_dir_structure():
return not os.path.exists(os.path.join(directory, "home")) 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() mc_dir = __get_mc_dir()
cache_dir = __get_cache_dir() cache_dir = __get_cache_dir()