Files
minecraft_simple_mod_sync/updater/updater.py

73 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
import json
import os
import requests
CONFIG_FILE: Path = Path(__file__).parent / "config.json"
CONFIG: dict = {}
with CONFIG_FILE.open() as f:
CONFIG = json.load(f)
TOKEN: str = CONFIG["token"]
MINECRAFT_FOLDER: Path = Path(CONFIG["minecraft"]).resolve()
MODS_FOLDER: Path = MINECRAFT_FOLDER / "mods"
# Создаем папку модов если ее нет
if not MODS_FOLDER.exists():
MODS_FOLDER.mkdir(parents=True)
CURRENT_MODS: list["Path"] = [Path(p).name for p in os.listdir(MODS_FOLDER)]
def get_mod_list():
headers = {'Authorization': TOKEN}
response = requests.get(f'{CONFIG["server.url"]}/mods/files', headers=headers)
if response.status_code == 200:
return response.json().get('files', [])
else:
print(f'Ошибка при получении списка файлов: {response.text}')
return []
def download_file(filename: str, dest: Path):
headers = {'Authorization': TOKEN}
url = f'{CONFIG["server.url"]}/mods/download/{filename}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
with open(dest, 'wb') as f:
f.write(response.content)
print(f'Файл {filename} успешно загружен.')
else:
print(f'Не удалось загрузить файл {filename}: {response.text}')
ACTUAL_MODS: list["str"] = get_mod_list()
print(f"Актуальные моды: {ACTUAL_MODS}\n\n")
print(f"Текущие моды: {CURRENT_MODS}\n\n")
TO_DELETE: list["Path"] = []
TO_DOWNLOAD: list["str"] = []
# Проверяем лишние или старые моды
for cur_mod in CURRENT_MODS:
# Проверяем лишние или старые моды
if cur_mod not in ACTUAL_MODS:
TO_DELETE.append(MODS_FOLDER / cur_mod)
for amod in ACTUAL_MODS:
if amod not in CURRENT_MODS:
TO_DOWNLOAD.append(amod)
# Применение действий
# Удаление лишнего
for file in TO_DELETE:
print(f"Удаление {file}")
os.remove(str(file))
# Скачивание нужного
for filename in TO_DOWNLOAD:
download_file(filename, MODS_FOLDER / filename)
print("Обновление завершено!")