Nicer otput while dowloading VODs, bright colors

This commit is contained in:
Ivan Habunek 2020-04-11 14:05:23 +02:00
parent 042d35ba1e
commit c9547435df
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 40 additions and 23 deletions

View File

@ -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 <cyan>{}B</cyan> at <cyan>{}B/s</cyan> remaining <cyan>{}</cyan>".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),
"<cyan>{}</cyan>".format(format_size(downloaded_size)),
"of <cyan>~{}</cyan>".format(format_size(est_total_size)),
"at <cyan>{}/s</cyan>".format(format_size(speed)) if speed > 0 else "",
"remaining <cyan>~{}</cyan>".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):

View File

@ -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'

View File

@ -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):