Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import os
  2.  
  3. from tqdm import tqdm
  4. import requests
  5.  
  6.  
  7. def d(url, dst, desc=None):
  8. file_size = requests.head(url).headers.get("Content-Length", '-1')
  9. file_size = int(file_size)
  10. if os.path.exists(dst):
  11. first_byte = os.path.getsize(dst)
  12. else:
  13. first_byte = 0
  14. if first_byte > file_size:
  15. first_byte = 0
  16. elif first_byte == file_size:
  17. return file_size
  18. header = {"Range": "bytes=%s-%s" % (first_byte, file_size)}
  19. desc = desc or url.split('/')[-1]
  20. pbar = tqdm(
  21. total=file_size,
  22. initial=first_byte,
  23. ncols=75,
  24. unit='B', unit_scale=True, unit_divisor=1024,
  25. desc=desc)
  26. req = requests.get(url, headers=header, stream=True)
  27. with(open(dst, 'ab')) as f:
  28. for chunk in req.iter_content(chunk_size=1024):
  29. if chunk:
  30. f.write(chunk)
  31. pbar.update(1024)
  32. pbar.close()
  33. return file_size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement