File: tqdm_requests.py

package info (click to toggle)
tqdm 4.67.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: python: 5,621; makefile: 159; sh: 16
file content (49 lines) | stat: -rw-r--r-- 1,466 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""An example of wrapping manual tqdm updates for `requests.get`.
See also: tqdm_wget.py.

Usage:
    tqdm_requests.py [options]

Options:
-h, --help
    Print this help message and exit
-u URL, --url URL  : string, optional
    The url to fetch.
    [default: https://caspersci.uk.to/matryoshka.zip]
-o FILE, --output FILE  : string, optional
    The local file path in which to save the url [default: /dev/null].
"""

from os import devnull

import requests
from docopt import docopt

from tqdm.auto import tqdm

opts = docopt(__doc__)

eg_link = opts['--url']
eg_file = eg_link.replace('/', ' ').split()[-1]
eg_out = opts['--output'].replace("/dev/null", devnull)

response = requests.get(eg_link, stream=True)
with open(eg_out, "wb") as fout:
    with tqdm(
        # all optional kwargs
        unit='B', unit_scale=True, unit_divisor=1024, miniters=1,
        desc=eg_file, total=int(response.headers.get('content-length', 0))
    ) as pbar:
        for chunk in response.iter_content(chunk_size=4096):
            fout.write(chunk)
            pbar.update(len(chunk))

# Even simpler progress by wrapping the output file's `write()`
response = requests.get(eg_link, stream=True)
with tqdm.wrapattr(
    open(eg_out, "wb"), "write",
    unit='B', unit_scale=True, unit_divisor=1024, miniters=1,
    desc=eg_file, total=int(response.headers.get('content-length', 0))
) as fout:
    for chunk in response.iter_content(chunk_size=4096):
        fout.write(chunk)