diff --git a/twitchdl/download.py b/twitchdl/download.py index dc1a87d..37b5166 100644 --- a/twitchdl/download.py +++ b/twitchdl/download.py @@ -33,7 +33,7 @@ def _download(url, path): def download_file(url, path, retries=RETRY_COUNT): if os.path.exists(path): - return 0 + return os.path.getsize(path) for _ in range(retries): try: @@ -45,24 +45,34 @@ def download_file(url, path, retries=RETRY_COUNT): def _print_progress(futures): - counter = 1 - total = len(futures) - total_size = 0 + downloaded_count = 0 + downloaded_size = 0 + max_msg_size = 0 start_time = datetime.now() + total_count = len(futures) for future in as_completed(futures): size = future.result() - percentage = 100 * counter // total - total_size += size + downloaded_count += 1 + downloaded_size += size + + percentage = 100 * downloaded_count // total_count + est_total_size = int(total_count * downloaded_size / downloaded_count) duration = (datetime.now() - start_time).seconds - speed = total_size // duration if duration else 0 - remaining = (total - counter) * duration / counter + speed = downloaded_size // duration if duration else 0 + remaining = (total_count - downloaded_count) * duration / downloaded_count - msg = "Downloaded VOD {}/{} ({}%) total {}B at {}B/s remaining {}".format( - counter, total, percentage, format_size(total_size), format_size(speed), format_duration(remaining)) + msg = " ".join([ + "Downloaded VOD {}/{}".format(downloaded_count, total_count), + "({}%)".format(percentage), + "{}".format(format_size(downloaded_size)), + "of ~{}".format(format_size(est_total_size)), + "at {}/s".format(format_size(speed)) if speed > 0 else "", + "remaining ~{}".format(format_duration(remaining)) if speed > 0 else "", + ]) - print_out("\r" + msg.ljust(80), end='') - counter += 1 + max_msg_size = max(len(msg), max_msg_size) + print_out("\r" + msg.ljust(max_msg_size), end="") def download_files(base_url, directory, filenames, max_workers): diff --git a/twitchdl/output.py b/twitchdl/output.py index 6fb05a0..9e8ca33 100644 --- a/twitchdl/output.py +++ b/twitchdl/output.py @@ -10,12 +10,12 @@ START_CODES = { 'bold': '\033[1m', 'i': '\033[3m', 'u': '\033[4m', - 'red': '\033[31m', - 'green': '\033[32m', - 'yellow': '\033[33m', - 'blue': '\033[34m', - 'magenta': '\033[35m', - 'cyan': '\033[36m', + 'red': '\033[91m', + 'green': '\033[92m', + 'yellow': '\033[93m', + 'blue': '\033[94m', + 'magenta': '\033[95m', + 'cyan': '\033[96m', } END_CODE = '\033[0m' diff --git a/twitchdl/utils.py b/twitchdl/utils.py index 37fbb4c..a7724a8 100644 --- a/twitchdl/utils.py +++ b/twitchdl/utils.py @@ -2,19 +2,26 @@ import re import unicodedata -def format_size(bytes_): +def _format_size(value, digits, unit): + if digits > 0: + return "{{:.{}f}}{}".format(digits, unit).format(value) + else: + return "{{:d}}{}".format(unit).format(value) + + +def format_size(bytes_, digits=1): if bytes_ < 1024: - return str(bytes_) + return _format_size(bytes_, digits, "B") kilo = bytes_ / 1024 if kilo < 1024: - return "{:.1f}K".format(kilo) + return _format_size(kilo, digits, "kB") mega = kilo / 1024 if mega < 1024: - return "{:.1f}M".format(mega) + return _format_size(mega, digits, "MB") - return "{:.1f}G".format(mega / 1024) + return _format_size(mega / 1024, digits, "GB") def format_duration(total_seconds):