verigak / progress

Easy to use progress bars for Python
ISC License
1.41k stars 179 forks source link

Sample usage with urllib.request.urlretrieve() reporthook #39

Open roniemartinez opened 7 years ago

roniemartinez commented 7 years ago

Hi @verigak,

How do you integrate this library with urllib.request.urlretrieve?

TobiX commented 6 years ago

Probably something like this:

from urllib.request import urlretrieve
from progress.bar import Bar
from progress.spinner import Spinner

class Getter:
    def get(self, url, to):
        self.p = None

        def update(blocks, bs, size):
            if not self.p:
                if size < 0:
                    print("spinner")
                    self.p = Spinner(to)
                else:
                    print("bar")
                    self.p = Bar(to, max=size)
            else:
                if size < 0:
                    self.p.update()
                else:
                    self.p.goto(blocks * bs)

        urlretrieve(url, to, update)
        self.p.finish()

Getter().get("https://www.python.org/ftp/python/3.6.3/python-3.6.3.exe", "py3.exe")

But you really should use requests:

import requests
from progress.bar import Bar
from progress.spinner import Spinner

r = requests.get("https://www.python.org/ftp/python/3.6.3/python-3.6.3.exe", stream=True)

size = r.headers['content-length']
if size:
    p = Bar('py3.exe', max=int(size))
else:
    p = Spinner('py3.exe')

with open('py3.exe', 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024*50):
        if chunk: # filter out keep-alive new chunks
            p.next(len(chunk))
            f.write(chunk)

p.finish()

Both Spinner-codepaths are not tested...