Ensure sanitization of infodict before printing to stdout

* `filter_requested_info` is renamed to a more appropriate name `sanitize_info`
This commit is contained in:
pukkandan 2021-08-05 03:37:16 +05:30
parent 9d65e7bd6d
commit 8012d892bd
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

View File

@ -1461,7 +1461,7 @@ def get_entry(i):
else: else:
self.to_screen('[info] Writing playlist metadata as JSON to: ' + infofn) self.to_screen('[info] Writing playlist metadata as JSON to: ' + infofn)
try: 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): except (OSError, IOError):
self.report_error('Cannot write playlist metadata to JSON file ' + infofn) self.report_error('Cannot write playlist metadata to JSON file ' + infofn)
@ -2386,7 +2386,7 @@ def print_optional(field):
if self.params.get('forcejson', False): if self.params.get('forcejson', False):
self.post_extract(info_dict) 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): def dl(self, name, info, subtitle=False, test=False):
@ -2548,7 +2548,7 @@ def process_info(self, info_dict):
else: else:
self.to_screen('[info] Writing video metadata as JSON to: ' + infofn) self.to_screen('[info] Writing video metadata as JSON to: ' + infofn)
try: 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): except (OSError, IOError):
self.report_error('Cannot write video metadata to JSON file ' + infofn) self.report_error('Cannot write video metadata to JSON file ' + infofn)
return return
@ -2858,7 +2858,7 @@ def download(self, url_list):
else: else:
if self.params.get('dump_single_json', False): if self.params.get('dump_single_json', False):
self.post_extract(res) 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 return self._download_retcode
@ -2867,7 +2867,7 @@ def download_with_info_file(self, info_filename):
[info_filename], mode='r', [info_filename], mode='r',
openhook=fileinput.hook_encoded('utf-8'))) as f: openhook=fileinput.hook_encoded('utf-8'))) as f:
# FileInput doesn't have a read method, we can't call json.load # 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: try:
self.process_ie_result(info, download=True) self.process_ie_result(info, download=True)
except (DownloadError, EntryNotInPlaylist, ThrottledDownload): except (DownloadError, EntryNotInPlaylist, ThrottledDownload):
@ -2880,10 +2880,11 @@ def download_with_info_file(self, info_filename):
return self._download_retcode return self._download_retcode
@staticmethod @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 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 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') remove_keys += ('requested_formats', 'requested_subtitles', 'requested_entries', 'filepath', 'entries', 'original_url')
empty_values = (None, {}, [], set(), tuple()) empty_values = (None, {}, [], set(), tuple())
reject = lambda k, v: k not in keep_keys and ( reject = lambda k, v: k not in keep_keys and (
@ -2897,6 +2898,11 @@ def filter_requested_info(info_dict, actually_filter=True):
else dict((k, filter_fn(v)) for k, v in obj.items() if not reject(k, v))) else dict((k, filter_fn(v)) for k, v in obj.items() if not reject(k, v)))
return filter_fn(info_dict) 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): def run_pp(self, pp, infodict):
files_to_delete = [] files_to_delete = []
if '__files_to_move' not in infodict: if '__files_to_move' not in infodict: