diff options
author | dirkf <fieldhouse@gmx.net> | 2023-07-30 21:49:58 +0100 |
---|---|---|
committer | dirkf <fieldhouse@gmx.net> | 2023-08-01 01:05:09 +0100 |
commit | e4178b5af3428f29feca622d531090f10f54af35 (patch) | |
tree | fa3671c3a363b2e197cd1eb101fbbcfe8eae36da | |
parent | 2d2a4bc8324fc4bc5a235cbd1ee0b0769912bfd1 (diff) |
[utils] Add and use `filter_dict()` from yt-dlp
-rw-r--r-- | youtube_dl/utils.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 94b339b1d..c530ed5a2 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -2586,7 +2586,7 @@ def handle_youtubedl_headers(headers): filtered_headers = headers if 'Youtubedl-no-compression' in filtered_headers: - filtered_headers = dict((k, v) for k, v in filtered_headers.items() if k.lower() != 'accept-encoding') + filtered_headers = filter_dict(filtered_headers, cndn=lambda k, _: k.lower() != 'accept-encoding') del filtered_headers['Youtubedl-no-compression'] return filtered_headers @@ -3102,9 +3102,7 @@ class YoutubeDLRedirectHandler(compat_urllib_request.HTTPRedirectHandler): new_data = None remove_headers.extend(['Content-Length', 'Content-Type']) - # NB: don't use dict comprehension for python 2.6 compatibility - new_headers = dict((k, v) for k, v in req.headers.items() - if k.title() not in remove_headers) + new_headers = filter_dict(req.headers, cndn=lambda k, _: k.title() not in remove_headers) return compat_urllib_request.Request( newurl, headers=new_headers, origin_req_host=req.origin_req_host, @@ -4377,6 +4375,11 @@ def try_get(src, getter, expected_type=None): return v +def filter_dict(dct, cndn=lambda _, v: v is not None): + # NB: don't use dict comprehension for python 2.6 compatibility + return dict((k, v) for k, v in dct.items() if cndn(k, v)) + + def merge_dicts(*dicts, **kwargs): """ Merge the `dict`s in `dicts` using the first valid value for each key. |