Skip to content

Commit 7b57ec8

Browse files
committed
util.download: remove cgi dependency
1 parent 9a11cfd commit 7b57ec8

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

‎util.py‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,6 @@ def download(url, filename=None, save_path='.', cookies=None, session=None, dry_
962962
Returns:
963963
str: The status of the download. Can be 'Dry run', 'Exists', or the HTTP status code.
964964
"""
965-
from cgi import parse_header
966965
from mimetypes import guess_extension
967966
# it uses requests_retry_session, so
968967
# pip install requests
@@ -1146,20 +1145,20 @@ def check_dupe(f, dupe=dupe, size=0):
11461145
web_name = f'{prefix} ' + web_name
11471146
f = p / safeify(web_name)
11481147
if "Content-Disposition" in r.headers: # Get filename from the header
1149-
# TODO: write a parser myself since cgi.parse_header is deprecated and has its own problems as noted below in "HACK".
1148+
# Content-Disposition can have multiple formats, e.g.:
11501149

1150+
# Content-Disposition: inline
1151+
# Content-Disposition: attachment
1152+
# Content-Disposition: attachment; filename="file name.jpg"
1153+
# Content-Disposition: attachment; filename*=UTF-8''file%20name.jpg
1154+
# Content-Disposition: filename="file name.jpg" # non-standard, but some sites do this.
11511155
content_disposition = r.headers["Content-Disposition"]
1152-
# HACK: parse_header doesn't work if there is no value of "attachment" etc. exists.
1153-
# Like content_disposition == "filename=xxx". So we add a dummy value.
1154-
# I think it's non-standard, but it's relatively common.
1155-
if content_disposition.startswith('filename'):
1156-
content_disposition = 'attachment; ' + r.headers["Content-Disposition"]
1157-
_, params = parse_header(content_disposition)
1156+
# parse it to get params
11581157
header_name = ''
1159-
if 'filename*' in params:
1160-
header_name = unquote(params['filename*'].lstrip("UTF-8''"))
1161-
elif 'filename' in params:
1162-
header_name = params['filename']
1158+
if m := re.search(r'filename="(.+)"', content_disposition):
1159+
header_name = m[1]
1160+
elif m := re.search(r'filename\*=(.+)', content_disposition):
1161+
header_name = unquote(m[1].removeprefix("UTF-8''"))
11631162
if header_name:
11641163
if prefix:
11651164
header_name = f'{prefix} ' + header_name

0 commit comments

Comments
 (0)