Better computation of speed

Distinct VODs took from disk and freshly downloaded
This commit is contained in:
David the Intrepid 2021-09-16 02:53:02 +01:00 committed by Ivan Habunek
parent caabe3138c
commit dd1f4e0d26
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 22 additions and 6 deletions

View File

@ -1,6 +1,11 @@
Twitch Downloader change log
============================
1.17.0 (TBA)
------------
* Fix speed calculation when resuming download (#75, thanks CroquetteTheThe)
1.16.1 (2021-07-31)
-------------------

View File

@ -34,11 +34,13 @@ def _download(url, path):
def download_file(url, path, retries=RETRY_COUNT):
if os.path.exists(path):
return os.path.getsize(path)
from_disk = True
return (os.path.getsize(path), from_disk)
from_disk = False
for _ in range(retries):
try:
return _download(url, path)
return (_download(url, path), from_disk)
except RequestException:
pass
@ -51,17 +53,26 @@ def _print_progress(futures):
max_msg_size = 0
start_time = datetime.now()
total_count = len(futures)
current_download_size = 0
current_downloaded_count = 0
for future in as_completed(futures):
size = future.result()
size, from_disk = future.result()
downloaded_count += 1
downloaded_size += size
# If we find something on disk, we don't want to take it in account in
# the speed calculation
if not from_disk:
current_download_size += size
current_downloaded_count += 1
percentage = 100 * downloaded_count // total_count
est_total_size = int(total_count * downloaded_size / downloaded_count)
duration = (datetime.now() - start_time).seconds
speed = downloaded_size // duration if duration else 0
remaining = (total_count - downloaded_count) * duration / downloaded_count
speed = current_download_size // duration if duration else 0
remaining = (total_count - downloaded_count) * duration / current_downloaded_count \
if current_downloaded_count else 0
msg = " ".join([
"Downloaded VOD {}/{}".format(downloaded_count, total_count),
@ -69,7 +80,7 @@ def _print_progress(futures):
"<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 "",
"remaining <cyan>~{}</cyan>".format(format_duration(remaining)) if remaining > 0 else "",
])
max_msg_size = max(len(msg), max_msg_size)