From ae8f99e64856260d97d363233b365a6521167b2b Mon Sep 17 00:00:00 2001 From: pukkandan Date: Fri, 28 May 2021 21:45:06 +0530 Subject: [PATCH] Remove `None` values from `info.json` --- yt_dlp/YoutubeDL.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 3c237212a..aa0a77d15 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -2777,19 +2777,20 @@ class YoutubeDL(object): @staticmethod def filter_requested_info(info_dict, actually_filter=True): - info_dict.pop('__original_infodict', None) # Always remove this - if not actually_filter: + remove_keys = ['__original_infodict'] # Always remove this since this may contain a copy of the entire dict + keep_keys = ['_type'], # Always keep this to facilitate load-info-json + if actually_filter: + remove_keys += ('requested_formats', 'requested_subtitles', 'requested_entries', 'filepath', 'entries') + empty_values = (None, {}, [], set(), tuple()) + reject = lambda k, v: k not in keep_keys and ( + k.startswith('_') or k in remove_keys or v in empty_values) + else: info_dict['epoch'] = int(time.time()) - return info_dict - exceptions = { - 'remove': ['requested_formats', 'requested_subtitles', 'requested_entries', 'filepath', 'entries'], - 'keep': ['_type'], - } - keep_key = lambda k: k in exceptions['keep'] or not (k.startswith('_') or k in exceptions['remove']) + reject = lambda k, v: k in remove_keys filter_fn = lambda obj: ( - list(map(filter_fn, obj)) if isinstance(obj, (list, tuple)) + list(map(filter_fn, obj)) if isinstance(obj, (list, tuple, set)) else obj if not isinstance(obj, dict) - else dict((k, filter_fn(v)) for k, v in obj.items() if keep_key(k))) + else dict((k, filter_fn(v)) for k, v in obj.items() if not reject(k, v))) return filter_fn(info_dict) def run_pp(self, pp, infodict):