Skip to content

Commit

Permalink
Fix inaccurate download progressbar for ZIP archive
Browse files Browse the repository at this point in the history
  • Loading branch information
superatomic committed Nov 22, 2023
1 parent f9cce06 commit 02f3e24
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/tldr_man/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 02f3e24

Please sign in to comment.