diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/python/dlmanager/examples/dl_tqdm.py | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/python/dlmanager/examples/dl_tqdm.py')
-rw-r--r-- | third_party/python/dlmanager/examples/dl_tqdm.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/third_party/python/dlmanager/examples/dl_tqdm.py b/third_party/python/dlmanager/examples/dl_tqdm.py new file mode 100644 index 0000000000..a4e458a415 --- /dev/null +++ b/third_party/python/dlmanager/examples/dl_tqdm.py @@ -0,0 +1,45 @@ +import argparse +import tqdm + +from six.moves.urllib.parse import urlparse + +from dlmanager import Download + + +def parse_args(argv=None): + parser = argparse.ArgumentParser() + parser.add_argument("url", help="url to download") + return parser.parse_args(argv) + + +def download_progress(bar): + last_b = [0] + + def inner(_, current, total): + if total is not None: + bar.total = total + delta = current - last_b[0] + last_b[0] = current + + if delta > 0: + bar.update(delta) + return inner + + +def download_file(url, dest=None): + if dest is None: + dest = urlparse(url).path.split('/')[-1] + + with tqdm.tqdm(unit='B', unit_scale=True, miniters=1, dynamic_ncols=True, + desc=dest) as bar: + dl = Download(url, dest, progress=download_progress(bar)) + dl.start() + dl.wait() + + +if __name__ == '__main__': + options = parse_args() + try: + download_file(options.url) + except KeyboardInterrupt: + print("\nInterrupted.") |