1 from .common import InfoExtractor
9 class SubtitlesInfoExtractor(InfoExtractor):
11 def _list_available_subtitles(self, video_id, webpage=None):
12 """ outputs the available subtitles for the video """
13 sub_lang_list = self._get_available_subtitles(video_id)
14 auto_captions_list = self._get_available_automatic_caption(video_id, webpage)
15 sub_lang = ",".join(list(sub_lang_list.keys()))
16 self.to_screen(u'%s: Available subtitles for video: %s' %
18 auto_lang = ",".join(auto_captions_list.keys())
19 self.to_screen(u'%s: Available automatic captions for video: %s' %
20 (video_id, auto_lang))
22 def extract_subtitles(self, video_id, video_webpage=None):
23 """ returns {sub_lang: sub} or {} if subtitles not found """
24 if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
25 available_subs_list = self._get_available_subtitles(video_id)
26 elif self._downloader.params.get('writeautomaticsub', False):
27 available_subs_list = self._get_available_automatic_caption(video_id, video_webpage)
31 if not available_subs_list: # error, it didn't get the available subtitles
33 if self._downloader.params.get('allsubtitles', False):
34 sub_lang_list = available_subs_list
36 if self._downloader.params.get('subtitleslangs', False):
37 requested_langs = self._downloader.params.get('subtitleslangs')
38 elif 'en' in available_subs_list:
39 requested_langs = ['en']
41 requested_langs = [list(available_subs_list.keys())[0]]
44 for sub_lang in requested_langs:
45 if not sub_lang in available_subs_list:
46 self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
48 sub_lang_list[sub_lang] = available_subs_list[sub_lang]
51 for sub_lang, url in sub_lang_list.items():
52 subtitle = self._request_subtitle_url(sub_lang, url)
54 subtitles[sub_lang] = subtitle
57 def _request_subtitle_url(self, sub_lang, url):
58 """ makes the http request for the subtitle """
60 sub = self._download_webpage(url, None, note=False)
61 except ExtractorError as err:
62 self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
65 self._downloader.report_warning(u'Did not fetch video subtitles')
69 def _get_available_subtitles(self, video_id):
71 returns {sub_lang: url} or {} if not available
72 Must be redefined by the subclasses
76 def _get_available_automatic_caption(self, video_id, webpage):
78 returns {sub_lang: url} or {} if not available
79 Must be redefined by the subclasses that support automatic captions,
80 otherwise it will return {}
82 self._downloader.report_warning(u'Automatic Captions not supported by this server')