utils_16.py 865 B

1234567891011121314151617181920212223
  1. def save_response_content(response,
  2. destination,
  3. file_size=None,
  4. chunk_size=32768):
  5. if file_size is not None:
  6. pbar = tqdm(total=math.ceil(file_size / chunk_size), unit='chunk')
  7. readable_file_size = sizeof_fmt(file_size)
  8. else:
  9. pbar = None
  10. with open(destination, 'wb') as f:
  11. downloaded_size = 0
  12. for chunk in response.iter_content(chunk_size):
  13. downloaded_size += chunk_size
  14. if pbar is not None:
  15. pbar.update(1)
  16. pbar.set_description(f'Download {sizeof_fmt(downloaded_size)} '
  17. f'/ {readable_file_size}')
  18. if chunk: # filter out keep-alive new chunks
  19. f.write(chunk)
  20. if pbar is not None:
  21. pbar.close()