forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_downloader.py
40 lines (29 loc) · 930 Bytes
/
file_downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
File Downloader
This python script will help users to download any kind of files, irrespective of their size from the internet.<br>
You just need to have the url and you are good to go!
"""
import os
import requests
from tqdm import tqdm
import math
import time
url = input("Enter the url of the file you want to download: ")
r = requests.get(url)
# receives data from the url
file_size = int(r.headers['Content-Length'])
chunk_size = 256
"""Chunk size is the
number of bytes downloaded at a time
"""
r = requests.get(url, stream=True)
"""streams=True ensures that
will not get data at once, but will get data one by one
"""
extension = (os.path.splitext(url))[-1]
file = "file"+extension
iterations = math.ceil(file_size/chunk_size)
with open(file, "wb") as file:
for chunk in tqdm(r.iter_content(chunk_size=chunk_size), total=iterations):
time.sleep(0.5)
file.write(chunk)