2013-08-07 18:59:11 +02:00
|
|
|
from .common import InfoExtractor
|
|
|
|
|
|
|
|
from ..utils import (
|
|
|
|
compat_str,
|
2013-09-11 16:24:47 +02:00
|
|
|
ExtractorError,
|
2013-08-07 18:59:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-11 15:51:04 +02:00
|
|
|
class SubtitlesInfoExtractor(InfoExtractor):
|
2013-08-07 18:59:11 +02:00
|
|
|
|
2013-09-11 19:17:30 +02:00
|
|
|
def _list_available_subtitles(self, video_id, webpage=None):
|
2013-08-08 11:20:56 +02:00
|
|
|
""" outputs the available subtitles for the video """
|
|
|
|
sub_lang_list = self._get_available_subtitles(video_id)
|
2013-09-11 19:17:30 +02:00
|
|
|
auto_captions_list = self._get_available_automatic_caption(video_id, webpage)
|
2013-08-07 18:59:11 +02:00
|
|
|
sub_lang = ",".join(list(sub_lang_list.keys()))
|
2013-08-08 08:54:10 +02:00
|
|
|
self.to_screen(u'%s: Available subtitles for video: %s' %
|
|
|
|
(video_id, sub_lang))
|
2013-09-11 19:17:30 +02: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 18:59:11 +02:00
|
|
|
|
2013-09-11 19:02:01 +02:00
|
|
|
def extract_subtitles(self, video_id, video_webpage=None):
|
2013-08-08 11:20:56 +02:00
|
|
|
""" returns {sub_lang: sub} or {} if subtitles not found """
|
2013-09-11 19:02:01 +02:00
|
|
|
if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
|
|
|
|
available_subs_list = self._get_available_subtitles(video_id)
|
|
|
|
elif self._downloader.params.get('writeautomaticsub', False):
|
|
|
|
available_subs_list = self._get_available_automatic_caption(video_id, video_webpage)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-09-06 16:26:22 +02:00
|
|
|
if not available_subs_list: # error, it didn't get the available subtitles
|
2013-08-07 18:59:11 +02:00
|
|
|
return {}
|
2013-09-06 16:26:22 +02:00
|
|
|
if self._downloader.params.get('allsubtitles', False):
|
|
|
|
sub_lang_list = available_subs_list
|
|
|
|
else:
|
2013-09-11 19:02:01 +02: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 16:26:22 +02:00
|
|
|
|
2013-09-11 19:02:01 +02:00
|
|
|
sub_lang_list = {}
|
|
|
|
for sub_lang in requested_langs:
|
|
|
|
if not sub_lang in available_subs_list:
|
|
|
|
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 08:54:10 +02:00
|
|
|
|
2013-08-07 18:59:11 +02:00
|
|
|
subtitles = {}
|
2013-09-06 16:26:22 +02:00
|
|
|
for sub_lang, url in sub_lang_list.items():
|
2013-08-08 08:54:10 +02:00
|
|
|
subtitle = self._request_subtitle_url(sub_lang, url)
|
2013-08-07 18:59:11 +02:00
|
|
|
if subtitle:
|
|
|
|
subtitles[sub_lang] = subtitle
|
|
|
|
return subtitles
|
|
|
|
|
2013-08-08 08:54:10 +02:00
|
|
|
def _request_subtitle_url(self, sub_lang, url):
|
2013-08-08 11:20:56 +02:00
|
|
|
""" makes the http request for the subtitle """
|
2013-08-07 18:59:11 +02:00
|
|
|
try:
|
2013-09-11 16:24:47 +02:00
|
|
|
sub = self._download_webpage(url, None, note=False)
|
|
|
|
except ExtractorError as err:
|
2013-08-07 18:59:11 +02: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
|
|
|
|
return sub
|
|
|
|
|
|
|
|
def _get_available_subtitles(self, video_id):
|
2013-09-11 16:05:49 +02:00
|
|
|
"""
|
|
|
|
returns {sub_lang: url} or {} if not available
|
|
|
|
Must be redefined by the subclasses
|
|
|
|
"""
|
2013-08-07 18:59:11 +02:00
|
|
|
pass
|
|
|
|
|
2013-09-11 19:02:01 +02:00
|
|
|
def _get_available_automatic_caption(self, video_id, webpage):
|
2013-09-11 16:05:49 +02:00
|
|
|
"""
|
2013-09-11 19:02:01 +02:00
|
|
|
returns {sub_lang: url} or {} if not available
|
2013-09-11 16:05:49 +02: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 {}
|