From 2480f4e690d6a8b2705948830d9beb4671c80ce5 Mon Sep 17 00:00:00 2001 From: Azalea Date: Sat, 29 Oct 2022 17:02:20 -0400 Subject: [PATCH] [+] Downloader --- README.md | 8 ++++++++ hypy_utils/downloader.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 hypy_utils/downloader.py diff --git a/README.md b/README.md index e0cf47c..aafaedd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # HyPyUtils HyDEV Utils for Python + +## Modules + +| Module | Requirements | +|--------------------|--------------------------| +| `tqdm_utils` | tqdm | +| `downloader` | tqdm, requests | +| `scientific_utils` | numpy, numba, matplotlib | diff --git a/hypy_utils/downloader.py b/hypy_utils/downloader.py new file mode 100644 index 0000000..d319f84 --- /dev/null +++ b/hypy_utils/downloader.py @@ -0,0 +1,28 @@ +from __future__ import annotations + +from pathlib import Path + +import requests +import tqdm + + +def download_file(url: str, file: str | Path): + """ + Helper method handling downloading large files from `url` to `filename`. + Returns a pointer to `filename`. + https://stackoverflow.com/a/42071418/7346633 + """ + file = Path(file) + if file.is_file(): + return file + + chunk_size = 1024 + r = requests.get(url, stream=True) + with open(file, 'wb') as f: + pbar = tqdm.tqdm(unit=" MB", total=int(r.headers['Content-Length']) / 1024 / 1024, + bar_format='{desc} {rate_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #', desc=file.name[:bar_len].ljust(bar_len)) + for chunk in r.iter_content(chunk_size=chunk_size): + if chunk: + pbar.update(len(chunk) / 1024 / 1024) + f.write(chunk) + return file