SHARE
TWEET

Untitled

a guest Jun 25th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top