[utils] Add `get_first`

pull/2908/head^2
pukkandan 3 years ago
parent a3b7dff015
commit ff91cf7483
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39

@ -18,6 +18,7 @@ from ..utils import (
ExtractorError, ExtractorError,
float_or_none, float_or_none,
get_element_by_id, get_element_by_id,
get_first,
int_or_none, int_or_none,
js_to_json, js_to_json,
merge_dicts, merge_dicts,
@ -405,11 +406,9 @@ class FacebookIE(InfoExtractor):
..., 'require', ..., ..., ..., '__bbox', 'result', 'data'), expected_type=dict) or [] ..., 'require', ..., ..., ..., '__bbox', 'result', 'data'), expected_type=dict) or []
media = [m for m in traverse_obj(post, (..., 'attachments', ..., 'media'), expected_type=dict) or [] media = [m for m in traverse_obj(post, (..., 'attachments', ..., 'media'), expected_type=dict) or []
if str(m.get('id')) == video_id and m.get('__typename') == 'Video'] if str(m.get('id')) == video_id and m.get('__typename') == 'Video']
title = traverse_obj(media, (..., 'title', 'text'), get_all=False) title = get_first(media, ('title', 'text'))
description = traverse_obj(media, ( description = get_first(media, ('creation_story', 'comet_sections', 'message', 'story', 'message', 'text'))
..., 'creation_story', 'comet_sections', 'message', 'story', 'message', 'text'), get_all=False) uploader_data = get_first(media, 'owner') or get_first(post, ('node', 'actors', ...)) or {}
uploader_data = (traverse_obj(media, (..., 'owner'), get_all=False)
or traverse_obj(post, (..., 'node', 'actors', ...), get_all=False) or {})
page_title = title or self._html_search_regex(( page_title = title or self._html_search_regex((
r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>(?P<content>[^<]*)</h2>', r'<h2\s+[^>]*class="uiHeaderTitle"[^>]*>(?P<content>[^<]*)</h2>',

@ -15,6 +15,7 @@ from ..compat import (
from ..utils import ( from ..utils import (
ExtractorError, ExtractorError,
HEADRequest, HEADRequest,
get_first,
int_or_none, int_or_none,
join_nonempty, join_nonempty,
LazyList, LazyList,
@ -816,8 +817,7 @@ class DouyinIE(TikTokIE):
render_data = self._parse_json( render_data = self._parse_json(
render_data_json, video_id, transform_source=compat_urllib_parse_unquote) render_data_json, video_id, transform_source=compat_urllib_parse_unquote)
return self._parse_aweme_video_web( return self._parse_aweme_video_web(get_first(render_data, ('aweme', 'detail')), url)
traverse_obj(render_data, (..., 'aweme', 'detail'), get_all=False), url)
class TikTokVMIE(InfoExtractor): class TikTokVMIE(InfoExtractor):

@ -39,6 +39,7 @@ from ..utils import (
ExtractorError, ExtractorError,
float_or_none, float_or_none,
format_field, format_field,
get_first,
int_or_none, int_or_none,
is_html, is_html,
join_nonempty, join_nonempty,
@ -72,10 +73,6 @@ from ..utils import (
) )
def get_first(obj, keys, **kwargs):
return traverse_obj(obj, (..., *variadic(keys)), **kwargs, get_all=False)
# any clients starting with _ cannot be explicity requested by the user # any clients starting with _ cannot be explicity requested by the user
INNERTUBE_CLIENTS = { INNERTUBE_CLIENTS = {
'web': { 'web': {

@ -5218,6 +5218,10 @@ def traverse_dict(dictn, keys, casesense=True):
return traverse_obj(dictn, keys, casesense=casesense, is_user_input=True, traverse_string=True) return traverse_obj(dictn, keys, casesense=casesense, is_user_input=True, traverse_string=True)
def get_first(obj, keys, **kwargs):
return traverse_obj(obj, (..., *variadic(keys)), **kwargs, get_all=False)
def variadic(x, allowed_types=(str, bytes, dict)): def variadic(x, allowed_types=(str, bytes, dict)):
return x if isinstance(x, collections.abc.Iterable) and not isinstance(x, allowed_types) else (x,) return x if isinstance(x, collections.abc.Iterable) and not isinstance(x, allowed_types) else (x,)

Loading…
Cancel
Save