From 746c55d188e4c0766cb3eb603cb474ad27e622f4 Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Sun, 14 Jan 2024 01:24:51 +0000 Subject: [PATCH] Fix progress total on single-URL requests downloads Previously, it would show the download as fully complete after the first 1024-byte chunk was downloaded, as the Progress Bar total value was set to the amount of URLs. This is because it assumed there would be multiple URLs to download at once, and would advance the progress bar each time one of the downloads completed instead. This changes it so that if there's only one URL to download, then it calculates the total amount of chunks to download which corrects the progress bar advances. --- devine/core/downloaders/requests.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/devine/core/downloaders/requests.py b/devine/core/downloaders/requests.py index 891807d..9c61cdd 100644 --- a/devine/core/downloaders/requests.py +++ b/devine/core/downloaders/requests.py @@ -1,3 +1,4 @@ +import math import time from functools import partial from pathlib import Path @@ -65,9 +66,16 @@ def requests( for url, out_path in uri: out_path.parent.mkdir(parents=True, exist_ok=True) attempts = 1 + try: stream = session.get(url, stream=True) stream.raise_for_status() + + if len(uri) == 1 and progress: + content_length = int(stream.headers.get("Content-Length", "0")) + if content_length > 0: + progress(total=math.ceil(content_length / 1024)) + with open(out_path, "wb") as f: written = 0 for chunk in stream.iter_content(chunk_size=1024):