add download meter

This commit is contained in:
Luna 2022-08-28 00:03:17 -03:00
parent 08f0dd27f7
commit ed0696721f

View file

@ -36,9 +36,22 @@ async def main():
async with ctx.session.get(url) as resp:
assert resp.status == 200
total_length = int(resp.headers["content-length"])
downloaded_bytes = 0
download_ratio = 0
log.info("to download %d bytes", total_length)
with tempfile.TemporaryFile() as temp_fd:
async for chunk in resp.content.iter_chunked(8192):
temp_fd.write(chunk)
downloaded_bytes += len(chunk)
new_download_ratio = round(
(downloaded_bytes / total_length) * 100
)
if new_download_ratio != download_ratio:
log.info("download at %d%%", download_ratio)
download_ratio = new_download_ratio
# write to output
log.info("copying temp to output")