From 8012d892bd38af731357a61e071e0a0d01bc41b4 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Thu, 5 Aug 2021 03:37:16 +0530 Subject: [PATCH] Ensure sanitization of infodict before printing to stdout * `filter_requested_info` is renamed to a more appropriate name `sanitize_info` --- yt_dlp/YoutubeDL.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 71b8b2f3d..7461082d7 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -1461,7 +1461,7 @@ class YoutubeDL(object): else: self.to_screen('[info] Writing playlist metadata as JSON to: ' + infofn) try: - write_json_file(self.filter_requested_info(ie_result, self.params.get('clean_infojson', True)), infofn) + write_json_file(self.sanitize_info(ie_result, self.params.get('clean_infojson', True)), infofn) except (OSError, IOError): self.report_error('Cannot write playlist metadata to JSON file ' + infofn) @@ -2386,7 +2386,7 @@ class YoutubeDL(object): if self.params.get('forcejson', False): self.post_extract(info_dict) - self.to_stdout(json.dumps(info_dict, default=repr)) + self.to_stdout(json.dumps(self.sanitize_info(info_dict), default=repr)) def dl(self, name, info, subtitle=False, test=False): @@ -2548,7 +2548,7 @@ class YoutubeDL(object): else: self.to_screen('[info] Writing video metadata as JSON to: ' + infofn) try: - write_json_file(self.filter_requested_info(info_dict, self.params.get('clean_infojson', True)), infofn) + write_json_file(self.sanitize_info(info_dict, self.params.get('clean_infojson', True)), infofn) except (OSError, IOError): self.report_error('Cannot write video metadata to JSON file ' + infofn) return @@ -2858,7 +2858,7 @@ class YoutubeDL(object): else: if self.params.get('dump_single_json', False): self.post_extract(res) - self.to_stdout(json.dumps(res, default=repr)) + self.to_stdout(json.dumps(self.filter_requested_info(res), default=repr)) return self._download_retcode @@ -2867,7 +2867,7 @@ class YoutubeDL(object): [info_filename], mode='r', openhook=fileinput.hook_encoded('utf-8'))) as f: # FileInput doesn't have a read method, we can't call json.load - info = self.filter_requested_info(json.loads('\n'.join(f)), self.params.get('clean_infojson', True)) + info = self.sanitize_info(json.loads('\n'.join(f)), self.params.get('clean_infojson', True)) try: self.process_ie_result(info, download=True) except (DownloadError, EntryNotInPlaylist, ThrottledDownload): @@ -2880,10 +2880,11 @@ class YoutubeDL(object): return self._download_retcode @staticmethod - def filter_requested_info(info_dict, actually_filter=True): + def sanitize_info(info_dict, remove_private_keys=False): + ''' Sanitize the infodict for converting to json ''' 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: + if remove_private_keys: remove_keys += ('requested_formats', 'requested_subtitles', 'requested_entries', 'filepath', 'entries', 'original_url') empty_values = (None, {}, [], set(), tuple()) reject = lambda k, v: k not in keep_keys and ( @@ -2897,6 +2898,11 @@ class YoutubeDL(object): else dict((k, filter_fn(v)) for k, v in obj.items() if not reject(k, v))) return filter_fn(info_dict) + @staticmethod + def filter_requested_info(info_dict, actually_filter=True): + ''' Alias of sanitize_info for backward compatibility ''' + return YoutubeDL.sanitize_info(info_dict, actually_filter) + def run_pp(self, pp, infodict): files_to_delete = [] if '__files_to_move' not in infodict: