Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roniemartinez opened this issue May 7, 2017 · 1 comment
Open

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

roniemartinez opened this issue May 7, 2017 · 1 comment

Comments

@roniemartinez
Copy link

Hi @verigak,

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

@TobiX
Copy link
Contributor

TobiX commented Dec 5, 2017

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants