3 from .common
import InfoExtractor
13 class SubtitlesIE(InfoExtractor
):
15 def report_video_subtitles_available(self
, video_id
, sub_lang_list
):
16 """Report available subtitles."""
17 sub_lang
= ",".join(list(sub_lang_list
.keys()))
18 self
.to_screen(u
'%s: Available subtitles for video: %s' %
21 def _list_available_subtitles(self
, video_id
):
22 sub_lang_list
= self
._get
_available
_subtitles
(video_id
)
23 self
.report_video_subtitles_available(video_id
, sub_lang_list
)
25 def _extract_subtitles(self
, video_id
):
27 Return a dictionary: {language: subtitles} or {} if the subtitles
30 sub_lang_list
= self
._get
_available
_subtitles
(video_id
)
31 if not sub_lang_list
: # error, it didn't get the available subtitles
34 if self
._downloader
.params
.get('writesubtitles', False):
35 if self
._downloader
.params
.get('subtitleslang', False):
36 sub_lang
= self
._downloader
.params
.get('subtitleslang')
37 elif 'en' in sub_lang_list
:
40 sub_lang
= list(sub_lang_list
.keys())[0]
41 if not sub_lang
in sub_lang_list
:
42 self
._downloader
.report_warning(u
'no closed captions found in the specified language "%s"' % sub_lang
)
44 sub_lang_list
= {sub_lang: sub_lang_list[sub_lang]}
47 for sub_lang
, url
in sub_lang_list
.iteritems():
48 subtitle
= self
._request
_subtitle
_url
(sub_lang
, url
)
50 subtitles
[sub_lang
] = subtitle
53 def _request_subtitle_url(self
, sub_lang
, url
):
55 sub
= compat_urllib_request
.urlopen(url
).read().decode('utf-8')
56 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
57 self
._downloader
.report_warning(u
'unable to download video subtitles for %s: %s' % (sub_lang
, compat_str(err
)))
60 self
._downloader
.report_warning(u
'Did not fetch video subtitles')
64 def _get_available_subtitles(self
, video_id
):
65 """returns the list of available subtitles like this {lang: url} """
66 """or {} if not available. Must be redefined by the subclasses."""
69 def _request_automatic_caption(self
, video_id
, webpage
):
70 """Request automatic caption. Redefine in subclasses."""
71 """returns a tuple of ... """
72 # return [(err_msg, None, None)]