From e4178b5af3428f29feca622d531090f10f54af35 Mon Sep 17 00:00:00 2001 From: dirkf Date: Sun, 30 Jul 2023 21:49:58 +0100 Subject: [PATCH] [utils] Add and use `filter_dict()` from yt-dlp --- youtube_dl/utils.py | 11 +++++++---- 1 file 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. -- 2.22.2