Skip to content

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

Open

Description

@roniemartinez

Hi @verigak,

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

Activity

  1. TobiX commented on Dec 5, 2017

    @TobiX
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions