Skip to content

Commit

Permalink
Progress bar for downloads (#389)
Browse files Browse the repository at this point in the history
* Initial version for progress bar when downloading

Co-authored-by: Sergey Pirogov <serhii.pirohov@gmail.com>
  • Loading branch information
Mandera and SergeyPirogov authored Jul 30, 2022
1 parent 9488862 commit 3d4d9b2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
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

0 comments on commit 3d4d9b2

Please sign in to comment.