[utils] Add `filter_dict`

This commit is contained in:
pukkandan 2022-03-28 08:21:45 +05:30
parent 1c1b2f96ae
commit 90137ca4be
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
4 changed files with 14 additions and 15 deletions

View File

@ -65,6 +65,7 @@ from .utils import (
ExistingVideoReached, ExistingVideoReached,
expand_path, expand_path,
ExtractorError, ExtractorError,
filter_dict,
float_or_none, float_or_none,
format_bytes, format_bytes,
format_field, format_field,
@ -1574,13 +1575,9 @@ class YoutubeDL(object):
if not info: if not info:
return info return info
force_properties = dict(
(k, v) for k, v in ie_result.items() if v is not None)
for f in ('_type', 'url', 'id', 'extractor', 'extractor_key', 'ie_key'):
if f in force_properties:
del force_properties[f]
new_result = info.copy() new_result = info.copy()
new_result.update(force_properties) new_result.update(filter_dict(ie_result, lambda k, v: (
v is not None and k not in {'_type', 'url', 'id', 'extractor', 'extractor_key', 'ie_key'})))
# Extracted info may not be a video result (i.e. # Extracted info may not be a video result (i.e.
# info.get('_type', 'video') != video) but rather an url or # info.get('_type', 'video') != video) but rather an url or

View File

@ -49,6 +49,7 @@ from ..utils import (
error_to_compat_str, error_to_compat_str,
extract_attributes, extract_attributes,
ExtractorError, ExtractorError,
filter_dict,
fix_xml_ampersands, fix_xml_ampersands,
float_or_none, float_or_none,
format_field, format_field,
@ -1588,7 +1589,7 @@ class InfoExtractor(object):
break break
traverse_json_ld(json_ld) traverse_json_ld(json_ld)
return dict((k, v) for k, v in info.items() if v is not None) return filter_dict(info)
def _search_nextjs_data(self, webpage, video_id, *, transform_source=None, fatal=True, **kw): def _search_nextjs_data(self, webpage, video_id, *, transform_source=None, fatal=True, **kw):
return self._parse_json( return self._parse_json(

View File

@ -11,6 +11,7 @@ from ..compat import (
from ..utils import ( from ..utils import (
determine_ext, determine_ext,
ExtractorError, ExtractorError,
filter_dict,
find_xpath_attr, find_xpath_attr,
fix_xml_ampersands, fix_xml_ampersands,
GeoRestrictedError, GeoRestrictedError,
@ -110,11 +111,11 @@ class RaiBaseIE(InfoExtractor):
if not audio_only: if not audio_only:
formats.extend(self._create_http_urls(relinker_url, formats)) formats.extend(self._create_http_urls(relinker_url, formats))
return dict((k, v) for k, v in { return filter_dict({
'is_live': is_live, 'is_live': is_live,
'duration': duration, 'duration': duration,
'formats': formats, 'formats': formats,
}.items() if v is not None) })
def _create_http_urls(self, relinker_url, fmts): def _create_http_urls(self, relinker_url, fmts):
_RELINKER_REG = r'https?://(?P<host>[^/]+?)/(?:i/)?(?P<extra>[^/]+?)/(?P<path>.+?)/(?P<id>\d+)(?:_(?P<quality>[\d\,]+))?(?:\.mp4|/playlist\.m3u8).+?' _RELINKER_REG = r'https?://(?P<host>[^/]+?)/(?:i/)?(?P<extra>[^/]+?)/(?P<path>.+?)/(?P<id>\d+)(?:_(?P<quality>[\d\,]+))?(?:\.mp4|/playlist\.m3u8).+?'

View File

@ -3105,16 +3105,16 @@ def try_get(src, getter, expected_type=None):
return v return v
def filter_dict(dct, cndn=lambda _, v: v is not None):
return {k: v for k, v in dct.items() if cndn(k, v)}
def merge_dicts(*dicts): def merge_dicts(*dicts):
merged = {} merged = {}
for a_dict in dicts: for a_dict in dicts:
for k, v in a_dict.items(): for k, v in a_dict.items():
if v is None: if (v is not None and k not in merged
continue or isinstance(v, str) and merged[k] == ''):
if (k not in merged
or (isinstance(v, compat_str) and v
and isinstance(merged[k], compat_str)
and not merged[k])):
merged[k] = v merged[k] = v
return merged return merged