treq icon indicating copy to clipboard operation
treq copied to clipboard

QUESTION: progress bar with download

Open mzfr opened this issue 6 years ago • 5 comments

Hi, I am trying to make a progress bar with treq download this is the code that I have written so far but there's a problem in line 14 because I can't find anything equivalent of response.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1)(this is from requests) in treq.

Is there any other way of making this possible or is there any other way to make progress bars with treq download?

mzfr avatar Feb 09 '19 17:02 mzfr

I have figured out how to do the chunks part but now I am stuck on finding the content size of the received content.

mzfr avatar Feb 10 '19 18:02 mzfr

If the server set a Content-Length header you can look there. If it didn't then that information isn't there at the HTTP level.

twm avatar Jun 05 '19 02:06 twm

Perhaps we should be exporting the parsed content-length header as an attribute somewhere?

glyph avatar Aug 03 '19 21:08 glyph

I am struggling with the same thing. I can't figure out how to get IResponse.length AND use treq.collect at the same time.

  • Is there an example somewhere?
  • If it's not possible, can length be exposed somewhere?

scriptmelvin avatar Nov 15 '22 22:11 scriptmelvin

I figured it out, maybe it's useful to someone:

currentbytes = 0
totalbytes = None

def headers(response):
    if response.length != twisted.web.iweb.UNKNOWN_LENGTH:
        totalbytes = response.length
    return response

def progress(packet):
    # f.write(packet)
    if totalbytes is not None:
        currentbytes += len(packet)
        percent = 100 * currentbytes / totalbytes

treq.get(url, unbuffered=True).addCallback(headers).addCallback(treq.collect, progress)

scriptmelvin avatar Nov 16 '22 15:11 scriptmelvin