diff --git a/main.py b/main.py index 11f0456..06cba56 100644 --- a/main.py +++ b/main.py @@ -3,8 +3,8 @@ import shutil import requests import zipfile -from io import BytesIO from pathlib import Path +from tqdm import tqdm # Minecraft installation folder. minecraft_dir = os.path.join(os.getenv('APPDATA'), ".minecraft") @@ -35,6 +35,21 @@ def check_minecraft_installation(minecraft_dir): else: print("Minecraft is already installed.") +# Diplays a progress bar. +def download_with_progress_bar(url, destination_path): + response = requests.get(url, stream=True) + total_size_in_bytes = int(response.headers.get('content-length', 0)) + block_size = 1024 + + progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) + with open(destination_path, 'wb') as file: + for data in response.iter_content(block_size): + progress_bar.update(len(data)) + file.write(data) + progress_bar.close() + if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes: + print("ERROR, something went wrong") + # Installs forge files and places them in the correct version folder. def install_forge(forge_dir, forge_url): print("Checking for existing Forge installation...") @@ -43,21 +58,20 @@ def install_forge(forge_dir, forge_url): os.makedirs(forge_dir) try: print("Downloading Forge...") - response = requests.get(forge_url) - if response.status_code == 200: - with zipfile.ZipFile(BytesIO(response.content)) as z: - print("Extracting Forge...") - common_prefix = os.path.commonprefix(z.namelist()) - for file_info in z.infolist(): - target_path = os.path.join(forge_dir, file_info.filename[len(common_prefix):]) - if file_info.filename.endswith('/'): - os.makedirs(target_path, exist_ok=True) - else: - with z.open(file_info) as source, open(target_path, 'wb') as target: - shutil.copyfileobj(source, target) - print("Forge installed successfully.") - else: - print("Error downloading Forge. Please check the URL or your internet connection.") + zip_path = os.path.join(forge_dir, "forge.zip") + download_with_progress_bar(forge_url, zip_path) + with zipfile.ZipFile(zip_path) as z: + print("Extracting Forge...") + common_prefix = os.path.commonprefix(z.namelist()) + for file_info in z.infolist(): + target_path = os.path.join(forge_dir, file_info.filename[len(common_prefix):]) + if file_info.filename.endswith('/'): + os.makedirs(target_path, exist_ok=True) + else: + with z.open(file_info) as source, open(target_path, 'wb') as target: + shutil.copyfileobj(source, target) + print("Forge installed successfully.") + os.remove(zip_path) # Don't forget to remove the temporary zip file except Exception as e: print(f"An error occurred during Forge installation: {e}") else: @@ -71,21 +85,20 @@ def install_forge_libraries(forge_libraries_dir, forge_libraries_url): os.makedirs(forge_libraries_dir) try: print("Downloading Forge libraries...") - response = requests.get(forge_libraries_url) - if response.status_code == 200: - with zipfile.ZipFile(BytesIO(response.content)) as z: - print("Extracting Forge libraries...") - common_prefix = os.path.commonprefix(z.namelist()) - for file_info in z.infolist(): - target_path = os.path.join(forge_libraries_dir, file_info.filename[len(common_prefix):]) - if file_info.filename.endswith('/'): - os.makedirs(target_path, exist_ok=True) - else: - with z.open(file_info) as source_file, open(target_path, 'wb') as dest_file: - shutil.copyfileobj(source_file, dest_file) - print("Forge libraries installed successfully.") - else: - print("Error downloading Forge libraries. Please check the URL or your internet connection.") + zip_path = os.path.join(forge_libraries_dir, "forge_libraries.zip") + download_with_progress_bar(forge_libraries_url, zip_path) + with zipfile.ZipFile(zip_path) as z: + print("Extracting Forge libraries...") + common_prefix = os.path.commonprefix(z.namelist()) + for file_info in z.infolist(): + target_path = os.path.join(forge_libraries_dir, file_info.filename[len(common_prefix):]) + if file_info.filename.endswith('/'): + os.makedirs(target_path, exist_ok=True) + else: + with z.open(file_info) as source_file, open(target_path, 'wb') as dest_file: + shutil.copyfileobj(source_file, dest_file) + print("Forge libraries installed successfully.") + os.remove(zip_path) except Exception as e: print(f"An error occurred during Forge libraries installation: {e}") else: @@ -103,18 +116,18 @@ def backup_mods(modpack_dir, mods_backup_dir): shutil.move(source_path, destination_path) else: os.remove(source_path) - print(f"Duplicate found, not moving '{item}'.") print(f"Mods backed up to '{mods_backup_dir}'.") else: print("No mods directory found. No backup required.") +# Downloads the modpack. def install_modpack(modpack_url, modpack_dir): print("Downloading modpack...") try: - response = requests.get(modpack_url) - if response.status_code == 200: - print("Extracting modpack...") - modpack_zip = zipfile.ZipFile(BytesIO(response.content)) + zip_path = os.path.join(modpack_dir, "modpack.zip") + download_with_progress_bar(modpack_url, zip_path) + print("Extracting modpack...") + with zipfile.ZipFile(zip_path) as modpack_zip: for member in modpack_zip.namelist(): filename = os.path.basename(member) if not filename: @@ -125,10 +138,8 @@ def install_modpack(modpack_url, modpack_dir): with source, target: shutil.copyfileobj(source, target) - - print("Modpack installed successfully.") - else: - print("Failed to download modpack. Please check the URL or your internet connection.") + print("Modpack installed successfully.") + os.remove(zip_path) except Exception as e: print(f"An error occurred during modpack installation: {e}")