Added loading bars to downloads.

This commit is contained in:
2024-02-20 22:19:47 -05:00
parent 49a2612249
commit 2557c6cd7d

47
main.py
View File

@@ -3,8 +3,8 @@ import shutil
import requests import requests
import zipfile import zipfile
from io import BytesIO
from pathlib import Path from pathlib import Path
from tqdm import tqdm
# Minecraft installation folder. # Minecraft installation folder.
minecraft_dir = os.path.join(os.getenv('APPDATA'), ".minecraft") minecraft_dir = os.path.join(os.getenv('APPDATA'), ".minecraft")
@@ -35,6 +35,21 @@ def check_minecraft_installation(minecraft_dir):
else: else:
print("Minecraft is already installed.") 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. # Installs forge files and places them in the correct version folder.
def install_forge(forge_dir, forge_url): def install_forge(forge_dir, forge_url):
print("Checking for existing Forge installation...") print("Checking for existing Forge installation...")
@@ -43,9 +58,9 @@ def install_forge(forge_dir, forge_url):
os.makedirs(forge_dir) os.makedirs(forge_dir)
try: try:
print("Downloading Forge...") print("Downloading Forge...")
response = requests.get(forge_url) zip_path = os.path.join(forge_dir, "forge.zip")
if response.status_code == 200: download_with_progress_bar(forge_url, zip_path)
with zipfile.ZipFile(BytesIO(response.content)) as z: with zipfile.ZipFile(zip_path) as z:
print("Extracting Forge...") print("Extracting Forge...")
common_prefix = os.path.commonprefix(z.namelist()) common_prefix = os.path.commonprefix(z.namelist())
for file_info in z.infolist(): for file_info in z.infolist():
@@ -56,8 +71,7 @@ def install_forge(forge_dir, forge_url):
with z.open(file_info) as source, open(target_path, 'wb') as target: with z.open(file_info) as source, open(target_path, 'wb') as target:
shutil.copyfileobj(source, target) shutil.copyfileobj(source, target)
print("Forge installed successfully.") print("Forge installed successfully.")
else: os.remove(zip_path) # Don't forget to remove the temporary zip file
print("Error downloading Forge. Please check the URL or your internet connection.")
except Exception as e: except Exception as e:
print(f"An error occurred during Forge installation: {e}") print(f"An error occurred during Forge installation: {e}")
else: else:
@@ -71,9 +85,9 @@ def install_forge_libraries(forge_libraries_dir, forge_libraries_url):
os.makedirs(forge_libraries_dir) os.makedirs(forge_libraries_dir)
try: try:
print("Downloading Forge libraries...") print("Downloading Forge libraries...")
response = requests.get(forge_libraries_url) zip_path = os.path.join(forge_libraries_dir, "forge_libraries.zip")
if response.status_code == 200: download_with_progress_bar(forge_libraries_url, zip_path)
with zipfile.ZipFile(BytesIO(response.content)) as z: with zipfile.ZipFile(zip_path) as z:
print("Extracting Forge libraries...") print("Extracting Forge libraries...")
common_prefix = os.path.commonprefix(z.namelist()) common_prefix = os.path.commonprefix(z.namelist())
for file_info in z.infolist(): for file_info in z.infolist():
@@ -84,8 +98,7 @@ def install_forge_libraries(forge_libraries_dir, forge_libraries_url):
with z.open(file_info) as source_file, open(target_path, 'wb') as dest_file: with z.open(file_info) as source_file, open(target_path, 'wb') as dest_file:
shutil.copyfileobj(source_file, dest_file) shutil.copyfileobj(source_file, dest_file)
print("Forge libraries installed successfully.") print("Forge libraries installed successfully.")
else: os.remove(zip_path)
print("Error downloading Forge libraries. Please check the URL or your internet connection.")
except Exception as e: except Exception as e:
print(f"An error occurred during Forge libraries installation: {e}") print(f"An error occurred during Forge libraries installation: {e}")
else: else:
@@ -103,18 +116,18 @@ def backup_mods(modpack_dir, mods_backup_dir):
shutil.move(source_path, destination_path) shutil.move(source_path, destination_path)
else: else:
os.remove(source_path) os.remove(source_path)
print(f"Duplicate found, not moving '{item}'.")
print(f"Mods backed up to '{mods_backup_dir}'.") print(f"Mods backed up to '{mods_backup_dir}'.")
else: else:
print("No mods directory found. No backup required.") print("No mods directory found. No backup required.")
# Downloads the modpack.
def install_modpack(modpack_url, modpack_dir): def install_modpack(modpack_url, modpack_dir):
print("Downloading modpack...") print("Downloading modpack...")
try: try:
response = requests.get(modpack_url) zip_path = os.path.join(modpack_dir, "modpack.zip")
if response.status_code == 200: download_with_progress_bar(modpack_url, zip_path)
print("Extracting modpack...") print("Extracting modpack...")
modpack_zip = zipfile.ZipFile(BytesIO(response.content)) with zipfile.ZipFile(zip_path) as modpack_zip:
for member in modpack_zip.namelist(): for member in modpack_zip.namelist():
filename = os.path.basename(member) filename = os.path.basename(member)
if not filename: if not filename:
@@ -125,10 +138,8 @@ def install_modpack(modpack_url, modpack_dir):
with source, target: with source, target:
shutil.copyfileobj(source, target) shutil.copyfileobj(source, target)
print("Modpack installed successfully.") print("Modpack installed successfully.")
else: os.remove(zip_path)
print("Failed to download modpack. Please check the URL or your internet connection.")
except Exception as e: except Exception as e:
print(f"An error occurred during modpack installation: {e}") print(f"An error occurred during modpack installation: {e}")