From 02f3e24d9febe5c28da19fde99d30ebf4b6c455c Mon Sep 17 00:00:00 2001 From: Olivia Kinnear <51250849+superatomic@users.noreply.github.com> Date: Tue, 21 Nov 2023 21:20:25 -0600 Subject: [PATCH] Fix inaccurate download progressbar for ZIP archive --- src/tldr_man/pages.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tldr_man/pages.py b/src/tldr_man/pages.py index fe0a805..1fa9928 100644 --- a/src/tldr_man/pages.py +++ b/src/tldr_man/pages.py @@ -19,6 +19,7 @@ import zipfile from concurrent.futures import ThreadPoolExecutor from contextlib import suppress, contextmanager +from math import ceil from pathlib import Path from os import makedirs, getenv from shutil import rmtree, move, which @@ -38,6 +39,7 @@ CACHE_DIR_NAME = 'tldr-man' ZIP_ARCHIVE_URL = getenv('TLDR_MAN_ARCHIVE_URL', "https://tldr.sh/assets/tldr.zip") +ZIP_ARCHIVE_CHUNK_SIZE = 8192 MANPAGE_SECTION = '1' @@ -98,12 +100,12 @@ def pages_archive(url: str = ZIP_ARCHIVE_URL) -> Iterator[zipfile.Path]: with requests.get(url, stream=True, timeout=10) as r: r.raise_for_status() try: - length = int(r.headers['Content-Length']) + length = ceil(int(r.headers['Content-Length']) / ZIP_ARCHIVE_CHUNK_SIZE) except (KeyError, ValueError): # KeyError if lookup failed and ValueError if `int()` failed. length = None with (open(zip_file, 'wb') as file, progressbar( - r.iter_content(chunk_size=8192), + r.iter_content(chunk_size=ZIP_ARCHIVE_CHUNK_SIZE), label=style_task("Downloading ZIP"), length=length, ) as chunks):