Compare commits

..

4 Commits

Author SHA1 Message Date
kclauhk
573cdcfe20
Update yt_dlp/extractor/facebook.py
Co-authored-by: bashonly <88596187+bashonly@users.noreply.github.com>
2023-11-24 01:25:26 +08:00
kclauhk
a9a48d451b
Update yt_dlp/extractor/facebook.py
Co-authored-by: bashonly <88596187+bashonly@users.noreply.github.com>
2023-11-24 01:25:16 +08:00
kclauhk
f592d4d2f4
Update yt_dlp/extractor/facebook.py
Co-authored-by: bashonly <88596187+bashonly@users.noreply.github.com>
2023-11-24 01:25:03 +08:00
bashonly
67d79a6f80
refactor/cleanup 2023-11-23 15:29:10 +00:00

View File

@ -16,10 +16,10 @@ from ..utils import (
determine_ext, determine_ext,
error_to_compat_str, error_to_compat_str,
float_or_none, float_or_none,
format_field,
get_element_by_id, get_element_by_id,
get_first, get_first,
int_or_none, int_or_none,
join_nonempty,
js_to_json, js_to_json,
merge_dicts, merge_dicts,
parse_count, parse_count,
@ -421,26 +421,29 @@ class FacebookIE(InfoExtractor):
r'data-sjs>({.*?ScheduledServerJS.*?})</script>', webpage)] r'data-sjs>({.*?ScheduledServerJS.*?})</script>', webpage)]
post = traverse_obj(post_data, ( post = traverse_obj(post_data, (
..., 'require', ..., ..., ..., '__bbox', 'require', ..., ..., ..., '__bbox', 'result', 'data'), expected_type=dict) or [] ..., 'require', ..., ..., ..., '__bbox', 'require', ..., ..., ..., '__bbox', 'result', 'data'), expected_type=dict) or []
snippet = traverse_obj(post, (..., 'video', ..., 'attachments', ..., lambda k, v: (
automatic_captions, subtitles = {}, {}
subs_data = traverse_obj(post, (..., 'video', ..., 'attachments', ..., lambda k, v: (
k == 'media' and str(v['id']) == video_id and v['__typename'] == 'Video'))) k == 'media' and str(v['id']) == video_id and v['__typename'] == 'Video')))
locale = self._html_search_meta(['og:locale', 'twitter:locale'], webpage, 'locale', default='en_US') is_video_broadcast = get_first(subs_data, 'is_video_broadcast', expected_type=bool)
captions = get_first(snippet, 'video_available_captions_locales', 'captions_url') captions = get_first(subs_data, 'video_available_captions_locales', 'captions_url')
is_video_broadcast = get_first(snippet, 'is_video_broadcast', expected_type=bool) if url_or_none(captions): # if subs_data only had a 'captions_url'
automatic_captions = {} locale = self._html_search_meta(['og:locale', 'twitter:locale'], webpage, 'locale', default='en_US')
subtitles = {}
if url_or_none(captions): # snippet only had 'captions_url'
subtitles[locale] = [{'url': captions}] subtitles[locale] = [{'url': captions}]
# or else subs_data had 'video_available_captions_locales', a list of dicts
for caption in traverse_obj(captions, ( for caption in traverse_obj(captions, (
{lambda x: sorted(x, key=lambda c: c['locale'])}, lambda _, v: v['captions_url']) {lambda x: sorted(x, key=lambda c: c['locale'])}, lambda _, v: v['captions_url'])
): ):
lang = caption.get('localized_language') or ''
subs = { subs = {
'url': caption['captions_url'], 'url': caption['captions_url'],
'name': join_nonempty('localized_language', 'localized_country', from_dict=caption), 'name': format_field(caption, 'localized_country', f'{lang} (%s)', default=lang),
} }
if caption.get('localized_creation_method') or is_video_broadcast: if caption.get('localized_creation_method') or is_video_broadcast:
automatic_captions.setdefault(caption['locale'], []).append(subs) automatic_captions.setdefault(caption['locale'], []).append(subs)
else: else:
subtitles.setdefault(caption['locale'], []).append(subs) subtitles.setdefault(caption['locale'], []).append(subs)
media = traverse_obj(post, (..., 'attachments', ..., lambda k, v: ( media = traverse_obj(post, (..., 'attachments', ..., lambda k, v: (
k == 'media' and str(v['id']) == video_id and v['__typename'] == 'Video')), expected_type=dict) k == 'media' and str(v['id']) == video_id and v['__typename'] == 'Video')), expected_type=dict)
title = get_first(media, ('title', 'text')) title = get_first(media, ('title', 'text'))