Skip to content

Commit 28e02a1

Browse files
committed
update file downloader tutorial
1 parent 406ef1d commit 28e02a1

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Diff for: general/file-downloader/download.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from tqdm import tqdm
22
import requests
3+
import cgi
34
import sys
45

56
# the url of file you want to download, passed from command line arguments
@@ -11,14 +12,23 @@
1112

1213
# get the total file size
1314
file_size = int(response.headers.get("Content-Length", 0))
14-
15-
# get the file name
16-
filename = url.split("/")[-1]
15+
# get the default filename
16+
default_filename = url.split("/")[-1]
17+
# get the content disposition header
18+
content_disposition = response.headers.get("Content-Disposition")
19+
if content_disposition:
20+
# parse the header using cgi
21+
value, params = cgi.parse_header(content_disposition)
22+
# extract filename from content disposition
23+
filename = params.get("filename", default_filename)
24+
else:
25+
# if content dispotion is not available, just use default from URL
26+
filename = default_filename
1727

1828
# progress bar, changing the unit to bytes instead of iteration (default by tqdm)
1929
progress = tqdm(response.iter_content(buffer_size), f"Downloading {filename}", total=file_size, unit="B", unit_scale=True, unit_divisor=1024)
2030
with open(filename, "wb") as f:
21-
for data in progress:
31+
for data in progress.iterable:
2232
# write data read to the file
2333
f.write(data)
2434
# update the progress bar manually

0 commit comments

Comments
 (0)