Function
download_url_with_progress
(url: str, progress: bool = False)
Source from the content-addressed store, hash-verified
| 45 | |
| 46 | |
| 47 | def download_url_with_progress(url: str, progress: bool = False) -> BytesIO: |
| 48 | buf = BytesIO() |
| 49 | with requests.get(url, stream=True, allow_redirects=True) as response: |
| 50 | total_size = int(response.headers.get("content-length", 0)) |
| 51 | progress_bar = tqdm( |
| 52 | total=total_size, |
| 53 | unit="B", |
| 54 | unit_scale=True, |
| 55 | unit_divisor=1024, |
| 56 | disable=(not progress), |
| 57 | ) |
| 58 | |
| 59 | for chunk in response.iter_content(10 * 1024): |
| 60 | buf.write(chunk) |
| 61 | progress_bar.update(len(chunk)) |
| 62 | progress_bar.close() |
| 63 | |
| 64 | buf.seek(0) |
| 65 | return buf |
Tested by
no test coverage detected