diff --git a/CHANGELOG.md b/CHANGELOG.md index f9a0d68..afc9d2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------- diff --git a/twitchdl/download.py b/twitchdl/download.py index c5c8365..49552b4 100644 --- a/twitchdl/download.py +++ b/twitchdl/download.py @@ -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): "{}".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 "", + "remaining ~{}".format(format_duration(remaining)) if remaining > 0 else "", ]) max_msg_size = max(len(msg), max_msg_size)