Skip to content

Commit

Permalink
download: Add support to an user defined progress bar
Browse files Browse the repository at this point in the history
This commit just introduces a new argument to the download function: `bar`.
The argument let the person who is developing code define his own progress
bar using whatever he wants to. It also requires the pattern of passing
the current size of the downloaded chunk. This callable function respects the
tqdm API.

Signed-off-by: Julio Faracco <[email protected]>
  • Loading branch information
jcfaracco committed Sep 29, 2024
1 parent eeb6995 commit c08058e
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions gdown/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def download(
format=None,
user_agent=None,
log_messages=None,
bar=None,
):
"""Download file from URL.
Expand Down Expand Up @@ -165,6 +166,10 @@ def download(
Log messages to customize. Currently it supports:
- 'start': the message to show the start of the download
- 'output': the message to show the output filename
bar: type
A user defined progress bar. It should be followed by the argument
of the current chunk size downloaded. The total size should be defined
previously in order to make the API consistent to tqdm.
Returns
-------
Expand All @@ -180,6 +185,8 @@ def download(
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36" # NOQA: E501
if log_messages is None:
log_messages = {}
if bar is not None and not callable(bar):
raise ValueError("Parameter bar should be callable and respect arguments of cur_size")

url_origin = url

Expand Down Expand Up @@ -362,19 +369,25 @@ def download(
total = res.headers.get("Content-Length")
if total is not None:
total = int(total) + start_size
if not quiet:
if not quiet and bar is None:
pbar = tqdm.tqdm(total=total, unit="B", initial=start_size, unit_scale=True)

percent = 0.0
t_start = time.time()
for chunk in res.iter_content(chunk_size=CHUNK_SIZE):
f.write(chunk)
if not quiet:
pbar.update(len(chunk))
if bar is None:
pbar.update(len(chunk))
else:
bar(len(chunk))
percent += len(chunk) / total
if speed is not None:
elapsed_time_expected = 1.0 * pbar.n / speed
elapsed_time_expected = 1.0 * percent / speed
elapsed_time = time.time() - t_start
if elapsed_time < elapsed_time_expected:
time.sleep(elapsed_time_expected - elapsed_time)
if not quiet:
if not quiet and bar is None:
pbar.close()
if tmp_file:
f.close()
Expand Down

0 comments on commit c08058e

Please sign in to comment.