Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to an user defined progress bar inside download function. #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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