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

Progress bar for downloads #389

Merged
merged 8 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pybrowsers = "*"
[packages]
requests = "*"
python-dotenv = "*"
tqdm = "*"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
install_requires=[
'requests',
'python-dotenv',
'tqdm'
],
)
5 changes: 4 additions & 1 deletion webdriver_manager/core/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from requests import Response

from webdriver_manager.core.config import ssl_verify
from webdriver_manager.core.utils import show_download_progress


class HttpClient:
Expand All @@ -28,6 +29,8 @@ def __init__(self):
self._ssl_verify = ssl_verify()

def get(self, url, **kwargs) -> Response:
resp = requests.get(url=url, verify=self._ssl_verify, **kwargs)
resp = requests.get(url=url, verify=self._ssl_verify, stream=True, **kwargs)
self.validate_response(resp)
show_download_progress(resp)
return resp

16 changes: 16 additions & 0 deletions webdriver_manager/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import subprocess
import sys

from tqdm import tqdm

from webdriver_manager.core.archive import Archive
from webdriver_manager.core.logger import log

Expand Down Expand Up @@ -276,3 +278,17 @@ def determine_powershell():
) as stream:
stdout = stream.communicate()[0].decode()
return "" if stdout == "powershell" else "powershell"


def show_download_progress(response, _bytes_threshold=100):
""" Opens up a response's content in chunks to show a progress bar with tqdm.
Resets response._content when done so that response can be consumed again as normal. """
total = int(response.headers.get("Content-Length", 0))
if total > _bytes_threshold:
content = bytearray()
progress_bar = tqdm(desc="[WDM] - Downloading", total=total, unit_scale=True, unit_divisor=1024, unit="B")
for chunk in response.iter_content(chunk_size=8192):
if chunk: # Filter out keep-alive new chunks
progress_bar.update(len(chunk))
content.extend(chunk)
response._content = content # To allow content to be "consumed" again