2013-08-07 16:59:11 +00:00
|
|
|
from .common import InfoExtractor
|
|
|
|
|
|
|
|
from ..utils import (
|
|
|
|
compat_str,
|
2013-09-11 14:24:47 +00:00
|
|
|
ExtractorError,
|
2013-08-07 16:59:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-11 13:51:04 +00:00
|
|
|
class SubtitlesInfoExtractor(InfoExtractor):
|
2013-09-12 09:15:25 +00:00
|
|
|
@property
|
|
|
|
def _have_to_download_any_subtitles(self):
|
|
|
|
return any([self._downloader.params.get('writesubtitles', False),
|
2013-09-14 09:14:40 +00:00
|
|
|
self._downloader.params.get('writeautomaticsub')])
|
2013-08-07 16:59:11 +00:00
|
|
|
|
2013-11-02 17:01:05 +00:00
|
|
|
def _list_available_subtitles(self, video_id, webpage):
|
2013-08-08 09:20:56 +00:00
|
|
|
""" outputs the available subtitles for the video """
|
2013-11-02 17:01:05 +00:00
|
|
|
sub_lang_list = self._get_available_subtitles(video_id, webpage)
|
2013-09-11 17:17:30 +00:00
|
|
|
auto_captions_list = self._get_available_automatic_caption(video_id, webpage)
|
2013-08-07 16:59:11 +00:00
|
|
|
sub_lang = ",".join(list(sub_lang_list.keys()))
|
2013-08-08 06:54:10 +00:00
|
|
|
self.to_screen(u'%s: Available subtitles for video: %s' %
|
|
|
|
(video_id, sub_lang))
|
2013-09-11 17:17:30 +00:00
|
|
|
auto_lang = ",".join(auto_captions_list.keys())
|
|
|
|
self.to_screen(u'%s: Available automatic captions for video: %s' %
|
|
|
|
(video_id, auto_lang))
|
2013-08-07 16:59:11 +00:00
|
|
|
|
2013-11-02 17:01:05 +00:00
|
|
|
def extract_subtitles(self, video_id, webpage):
|
2013-09-12 09:15:25 +00:00
|
|
|
"""
|
|
|
|
returns {sub_lang: sub} ,{} if subtitles not found or None if the
|
|
|
|
subtitles aren't requested.
|
|
|
|
"""
|
|
|
|
if not self._have_to_download_any_subtitles:
|
2013-09-11 17:02:01 +00:00
|
|
|
return None
|
2013-09-12 09:15:25 +00:00
|
|
|
available_subs_list = {}
|
|
|
|
if self._downloader.params.get('writeautomaticsub', False):
|
2013-11-02 17:01:05 +00:00
|
|
|
available_subs_list.update(self._get_available_automatic_caption(video_id, webpage))
|
2013-09-14 09:14:40 +00:00
|
|
|
if self._downloader.params.get('writesubtitles', False):
|
2013-11-02 17:01:05 +00:00
|
|
|
available_subs_list.update(self._get_available_subtitles(video_id, webpage))
|
2013-09-11 17:02:01 +00:00
|
|
|
|
2013-09-06 14:26:22 +00:00
|
|
|
if not available_subs_list: # error, it didn't get the available subtitles
|
2013-08-07 16:59:11 +00:00
|
|
|
return {}
|
2013-09-06 14:26:22 +00:00
|
|
|
if self._downloader.params.get('allsubtitles', False):
|
|
|
|
sub_lang_list = available_subs_list
|
|
|
|
else:
|
2013-09-11 17:02:01 +00:00
|
|
|
if self._downloader.params.get('subtitleslangs', False):
|
|
|
|
requested_langs = self._downloader.params.get('subtitleslangs')
|
|
|
|
elif 'en' in available_subs_list:
|
|
|
|
requested_langs = ['en']
|
|
|
|
else:
|
|
|
|
requested_langs = [list(available_subs_list.keys())[0]]
|
2013-09-06 14:26:22 +00:00
|
|
|
|
2013-09-11 17:02:01 +00:00
|
|
|
sub_lang_list = {}
|
|
|
|
for sub_lang in requested_langs:
|
2014-11-23 19:41:03 +00:00
|
|
|
if sub_lang not in available_subs_list:
|
2013-09-11 17:02:01 +00:00
|
|
|
self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
|
|
|
|
continue
|
|
|
|
sub_lang_list[sub_lang] = available_subs_list[sub_lang]
|
2013-08-08 06:54:10 +00:00
|
|
|
|
2013-08-07 16:59:11 +00:00
|
|
|
subtitles = {}
|
2013-09-06 14:26:22 +00:00
|
|
|
for sub_lang, url in sub_lang_list.items():
|
2013-08-08 06:54:10 +00:00
|
|
|
subtitle = self._request_subtitle_url(sub_lang, url)
|
2013-08-07 16:59:11 +00:00
|
|
|
if subtitle:
|
|
|
|
subtitles[sub_lang] = subtitle
|
|
|
|
return subtitles
|
|
|
|
|
2014-02-03 04:18:30 +00:00
|
|
|
def _download_subtitle_url(self, sub_lang, url):
|
|
|
|
return self._download_webpage(url, None, note=False)
|
|
|
|
|
2013-08-08 06:54:10 +00:00
|
|
|
def _request_subtitle_url(self, sub_lang, url):
|
2013-08-08 09:20:56 +00:00
|
|
|
""" makes the http request for the subtitle """
|
2013-08-07 16:59:11 +00:00
|
|
|
try:
|
2014-02-04 09:24:17 +00:00
|
|
|
sub = self._download_subtitle_url(sub_lang, url)
|
2013-09-11 14:24:47 +00:00
|
|
|
except ExtractorError as err:
|
2013-08-07 16:59:11 +00:00
|
|
|
self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
|
|
|
|
return
|
|
|
|
if not sub:
|
|
|
|
self._downloader.report_warning(u'Did not fetch video subtitles')
|
|
|
|
return
|
2014-02-04 09:24:17 +00:00
|
|
|
return sub
|
2013-08-07 16:59:11 +00:00
|
|
|
|
2013-11-02 17:01:05 +00:00
|
|
|
def _get_available_subtitles(self, video_id, webpage):
|
2013-09-11 14:05:49 +00:00
|
|
|
"""
|
|
|
|
returns {sub_lang: url} or {} if not available
|
|
|
|
Must be redefined by the subclasses
|
|
|
|
"""
|
2014-02-03 04:18:30 +00:00
|
|
|
|
|
|
|
# By default, allow implementations to simply pass in the result
|
|
|
|
assert isinstance(webpage, dict), \
|
|
|
|
'_get_available_subtitles not implemented'
|
|
|
|
return webpage
|
2013-08-07 16:59:11 +00:00
|
|
|
|
2013-09-11 17:02:01 +00:00
|
|
|
def _get_available_automatic_caption(self, video_id, webpage):
|
2013-09-11 14:05:49 +00:00
|
|
|
"""
|
2013-09-11 17:02:01 +00:00
|
|
|
returns {sub_lang: url} or {} if not available
|
2013-09-11 14:05:49 +00:00
|
|
|
Must be redefined by the subclasses that support automatic captions,
|
|
|
|
otherwise it will return {}
|
|
|
|
"""
|
|
|
|
self._downloader.report_warning(u'Automatic Captions not supported by this server')
|
|
|
|
return {}
|