import os
import time
import requests
from tqdm import tqdm # Progress bar module
def down_from_url(url, dst):
# Set up stream=True Parameter reads a large file
response = requests.get(url, stream=True)
# adopt header Of content-length Property to get the total capacity of the file
file_size = int(response.headers['content-length'])
if os.path.exists(dst):
# Gets the capacity of some files that have been downloaded locally , Easy to download , If it doesn't exist, download it from scratch .
first_byte = os.path.getsize(dst)
else:
first_byte = 0
# Greater than or equal to indicates that the download is complete , Otherwise continue
if first_byte >= file_size:
return file_size
header = {
"Range": f"bytes={
first_byte}-{
file_size}"}
pbar = tqdm(total=file_size, initial=first_byte, unit='B', unit_scale=True, desc=dst)
req = requests.get(url, headers=header, stream=True)
with open(dst, 'ab') as f:
# Read one at a time 1024 Bytes
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(1024)
pbar.close()
return file_size
if __name__ == '__main__':
urlList = []
for url in urlList:
# Generate file name based on timestamp
down_from_url(url, str(time.time()) + ".mp4")