updated download progress bar

This commit is contained in:
katboi01 2025-01-25 02:46:23 +01:00
parent b398bcd054
commit c454d7b8be
1 changed files with 31 additions and 19 deletions

View File

@ -1,24 +1,10 @@
import os import os
import sys
import hashlib import hashlib
import requests import requests
import urllib.parse import urllib.parse
from urllib.request import urlretrieve 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): def get_file_list(url):
url = "https://apidgp-gameplayer.games.dmm.com" + url url = "https://apidgp-gameplayer.games.dmm.com" + url
print("Retrieving file list from " + 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)) print("Files to download:", len(files_to_download))
for file in progressbar(files_to_download, "Downloading: ", 40): 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"] url, path = file["url"], file["path"]
os.makedirs(os.path.dirname(path), exist_ok=True) os.makedirs(os.path.dirname(path), exist_ok=True)
urlretrieve(url, path) 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 # #files_to_delete is unused until fully tested
# for file in files_to_delete: # for file in files_to_delete: