From c454d7b8be93f40e0d70b6f99f8b0a5794c9d1ca Mon Sep 17 00:00:00 2001 From: katboi01 Date: Sat, 25 Jan 2025 02:46:23 +0100 Subject: [PATCH] updated download progress bar --- dmmUpdater.py | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/dmmUpdater.py b/dmmUpdater.py index fac0053..f339c12 100644 --- a/dmmUpdater.py +++ b/dmmUpdater.py @@ -1,24 +1,10 @@ import os +import sys import hashlib import requests import urllib.parse from urllib.request import urlretrieve -import sys -def progressbar(it, prefix="", size=60, out=sys.stdout): # Python3.3+ - """https://stackoverflow.com/questions/3160699/python-progress-bar""" - count = len(it) - if count == 0: return - def show(j): - x = int(size*j/count) - print("{}[{}{}] {}/{}".format(prefix, "#"*x, "."*(size-x), j, count), - end='\r', file=out, flush=True) - show(0) - for i, item in enumerate(it): - yield item - show(i+1) - print("\n", flush=True, file=out) - def get_file_list(url): url = "https://apidgp-gameplayer.games.dmm.com" + url print("Retrieving file list from " + url) @@ -71,10 +57,36 @@ def update_game(game_path, files_url, files_param): print("Files to download:", len(files_to_download)) - for file in progressbar(files_to_download, "Downloading: ", 40): - url, path = file["url"], file["path"] - os.makedirs(os.path.dirname(path), exist_ok=True) - urlretrieve(url, path) + count = len(files_to_download) + if count > 0: + index = 0 + max_len = 0 + def show(j, downloaded, total_size): + nonlocal max_len + x = int(40*j/count) + string = "Downloading: [{}{}] {}/{} ({}/{})".format("#"*x, "."*(40-x), j, count, min(downloaded,total_size), total_size) + max_len = max(len(string), max_len) + print(string.ljust(max_len, ' '), end='\r', file=sys.stdout, flush=True) + def show_progress(block_num, block_size, total_size): + downloaded = block_num * block_size + show(index + 1, downloaded, total_size) + + retries = 3 + show(0, 0, 0) + while index < len(files_to_download): + try: + file = files_to_download[index] + url, path = file["url"], file["path"] + os.makedirs(os.path.dirname(path), exist_ok=True) + urlretrieve(url, path, reporthook=show_progress) + index += 1 + retries = 3 + except: + retries -= 1 + if retries == 0: + print(f'Retry for file {file["url"]} failed 3 times') + return False + print("\n", flush=True, file=sys.stdout) # #files_to_delete is unused until fully tested # for file in files_to_delete: