Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from tqdm import tqdm
- import requests
- def d(url, dst, desc=None):
- file_size = requests.head(url).headers.get("Content-Length", '-1')
- file_size = int(file_size)
- if os.path.exists(dst):
- first_byte = os.path.getsize(dst)
- else:
- first_byte = 0
- if first_byte > file_size:
- first_byte = 0
- elif first_byte == file_size:
- return file_size
- header = {"Range": "bytes=%s-%s" % (first_byte, file_size)}
- desc = desc or url.split('/')[-1]
- pbar = tqdm(
- total=file_size,
- initial=first_byte,
- ncols=75,
- unit='B', unit_scale=True, unit_divisor=1024,
- desc=desc)
- req = requests.get(url, headers=header, stream=True)
- with(open(dst, 'ab')) as f:
- for chunk in req.iter_content(chunk_size=1024):
- if chunk:
- f.write(chunk)
- pbar.update(1024)
- pbar.close()
- return file_size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement