3 from .common import InfoExtractor
13 class SubtitlesIE(InfoExtractor):
15 def _list_available_subtitles(self, video_id):
16 """ outputs the available subtitles for the video """
17 sub_lang_list = self._get_available_subtitles(video_id)
18 sub_lang = ",".join(list(sub_lang_list.keys()))
19 self.to_screen(u'%s: Available subtitles for video: %s' %
22 def _extract_subtitles(self, video_id):
23 """ returns {sub_lang: sub} or {} if subtitles not found """
24 sub_lang_list = self._get_available_subtitles(video_id)
25 if not sub_lang_list: # error, it didn't get the available subtitles
28 if self._downloader.params.get('writesubtitles', False):
29 if self._downloader.params.get('subtitleslang', False):
30 sub_lang = self._downloader.params.get('subtitleslang')
31 elif 'en' in sub_lang_list:
34 sub_lang = list(sub_lang_list.keys())[0]
35 if not sub_lang in sub_lang_list:
36 self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
38 sub_lang_list = {sub_lang: sub_lang_list[sub_lang]}
41 for sub_lang, url in sub_lang_list.iteritems():
42 subtitle = self._request_subtitle_url(sub_lang, url)
44 subtitles[sub_lang] = subtitle
47 def _request_subtitle_url(self, sub_lang, url):
48 """ makes the http request for the subtitle """
50 sub = compat_urllib_request.urlopen(url).read().decode('utf-8')
51 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
52 self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
55 self._downloader.report_warning(u'Did not fetch video subtitles')
59 def _get_available_subtitles(self, video_id):
60 """ returns {sub_lang: url} or {} if not available """
61 """ Must be redefined by the subclasses """
64 def _request_automatic_caption(self, video_id, webpage):
65 """ returns {sub_lang: sub} or {} if not available """
66 """ Must be redefined by the subclasses """
70 class NoAutoSubtitlesIE(SubtitlesIE):
71 """ A subtitle class for the servers that don't support auto-captions"""
73 def _request_automatic_caption(self, video_id, webpage):
74 self._downloader.report_warning(u'Automatic Captions not supported by this server')