mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-07 20:30:41 -05:00
[utils] Add get_first
This commit is contained in:
parent
a3b7dff015
commit
ff91cf7483
4 changed files with 11 additions and 11 deletions
|
@ -18,6 +18,7 @@
|
||||||
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 @@ def extract_metadata(webpage):
|
||||||
..., '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 ..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 @@ def _real_extract(self, url):
|
||||||
|
|
||||||
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 @@
|
||||||
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 @@
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
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…
Reference in a new issue