Compare commits

...

3 Commits

Author SHA1 Message Date
sepro
ca29e999b0 Use set instead 2024-01-18 23:23:02 +01:00
sepro
1c1dfb5b3f Move subtitles object back 2024-01-18 23:15:09 +01:00
sepro
75796eac31 Simplify logic and no longer download duplicates 2024-01-18 23:13:40 +01:00

View File

@ -1649,7 +1649,6 @@ class BiliIntlBaseIE(InfoExtractor):
return data return data
def _get_subtitles(self, *, ep_id=None, aid=None): def _get_subtitles(self, *, ep_id=None, aid=None):
subtitles = {}
sub_json = self._call_api( sub_json = self._call_api(
'/web/v2/subtitle', ep_id or aid, fatal=False, '/web/v2/subtitle', ep_id or aid, fatal=False,
note='Downloading subtitles list', errnote='Unable to download subtitles list', note='Downloading subtitles list', errnote='Unable to download subtitles list',
@ -1659,43 +1658,34 @@ class BiliIntlBaseIE(InfoExtractor):
'episode_id': ep_id, 'episode_id': ep_id,
'aid': aid, 'aid': aid,
})) or {} })) or {}
subtitles = {}
for sub in traverse_obj(sub_json, ('subtitles', lambda _, v: v['url'])): fetched_urls = set()
sub_ext = determine_ext(sub['url']) for sub in traverse_obj(sub_json, (('subtitles', 'video_subtitle'), ..., {dict})):
for url in traverse_obj(sub, ((None, 'ass', 'srt'), 'url', {url_or_none})):
if url in fetched_urls:
continue
fetched_urls.add(url)
sub_ext = determine_ext(url)
sub_lang = sub.get('lang_key') or 'en' sub_lang = sub.get('lang_key') or 'en'
sub_data = self._download_webpage( if sub_ext == 'ass':
sub['url'], ep_id or aid, fatal=False,
encoding='utf-8-sig' if sub_ext == 'ass' else None,
note=f'Downloading subtitles{format_field(sub, "lang", " for %s")} ({sub_lang})',
errnote='Unable to download subtitles')
if sub_ext != 'ass':
sub_ext, sub_data = 'srt', self.json2srt(sub_data)
if sub_data:
subtitles.setdefault(sub_lang, []).append({
'ext': sub_ext,
'data': sub_data
})
for sub in traverse_obj(sub_json, ('video_subtitle', ..., {dict})):
sub_lang = sub.get('lang_key') or 'en'
if sub.get('ass'):
subtitles.setdefault(sub_lang, []).append({ subtitles.setdefault(sub_lang, []).append({
'ext': 'ass', 'ext': 'ass',
'url': traverse_obj(sub, ('ass', 'url')) 'url': url,
}) })
elif sub_ext == 'json':
if sub.get('srt'):
sub_data = self._download_json( sub_data = self._download_json(
traverse_obj(sub, ('srt', 'url')), ep_id or aid, fatal=False, url, ep_id or aid, fatal=False,
note=f'Downloading subtitles{format_field(sub, "lang", " for %s")} ({sub_lang})', note=f'Downloading subtitles{format_field(sub, "lang", " for %s")} ({sub_lang})',
errnote='Unable to download subtitles') errnote='Unable to download subtitles')
if sub_data: if sub_data:
subtitles.setdefault(sub_lang, []).append({ subtitles.setdefault(sub_lang, []).append({
'ext': 'srt', 'ext': 'srt',
'data': self.json2srt(sub_data) 'data': self.json2srt(sub_data),
}) })
else:
self.report_warning('Unexpected subtitle extension', ep_id or aid)
return subtitles return subtitles