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.
This commit is contained in:
rlaphoenix 2024-01-14 01:24:51 +00:00
parent 0493d28914
commit 746c55d188
1 changed files with 8 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import math
import time import time
from functools import partial from functools import partial
from pathlib import Path from pathlib import Path
@ -65,9 +66,16 @@ def requests(
for url, out_path in uri: for url, out_path in uri:
out_path.parent.mkdir(parents=True, exist_ok=True) out_path.parent.mkdir(parents=True, exist_ok=True)
attempts = 1 attempts = 1
try: try:
stream = session.get(url, stream=True) stream = session.get(url, stream=True)
stream.raise_for_status() 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: with open(out_path, "wb") as f:
written = 0 written = 0
for chunk in stream.iter_content(chunk_size=1024): for chunk in stream.iter_content(chunk_size=1024):