1
0
mirror of https://github.com/l1ving/youtube-dl synced 2024-11-22 08:52:52 +08:00

[subtitles] Simplify the extraction of subtitles in subclasses and remove NoAutoSubtitlesInfoExtractor

Subclasses just need to call the method extract_subtitles, which will call _extract_subtitles and _request_automatic_caption
Now the default implementation of _request_automatic_caption returns {}.
This commit is contained in:
Jaime Marquínez Ferrándiz 2013-09-11 16:05:49 +02:00
parent 54d39d8b2f
commit d82134c339
3 changed files with 27 additions and 27 deletions

View File

@ -4,7 +4,7 @@ import itertools
import socket import socket
from .common import InfoExtractor from .common import InfoExtractor
from .subtitles import NoAutoSubtitlesInfoExtractor from .subtitles import SubtitlesInfoExtractor
from ..utils import ( from ..utils import (
compat_http_client, compat_http_client,
@ -18,7 +18,7 @@ from ..utils import (
) )
class DailymotionIE(NoAutoSubtitlesInfoExtractor): class DailymotionIE(SubtitlesInfoExtractor):
"""Information Extractor for Dailymotion""" """Information Extractor for Dailymotion"""
_VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)' _VALID_URL = r'(?i)(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/(?:embed/)?video/([^/]+)'
@ -81,14 +81,7 @@ class DailymotionIE(NoAutoSubtitlesInfoExtractor):
video_url = info[max_quality] video_url = info[max_quality]
# subtitles # subtitles
video_subtitles = None video_subtitles = self.extract_subtitles(video_id)
video_webpage = None
if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
video_subtitles = self._extract_subtitles(video_id)
elif self._downloader.params.get('writeautomaticsub', False):
video_subtitles = self._request_automatic_caption(video_id, video_webpage)
if self._downloader.params.get('listsubtitles', False): if self._downloader.params.get('listsubtitles', False):
self._list_available_subtitles(video_id) self._list_available_subtitles(video_id)
return return

View File

@ -62,19 +62,31 @@ class SubtitlesInfoExtractor(InfoExtractor):
return sub return sub
def _get_available_subtitles(self, video_id): def _get_available_subtitles(self, video_id):
""" returns {sub_lang: url} or {} if not available """ """
""" Must be redefined by the subclasses """ returns {sub_lang: url} or {} if not available
Must be redefined by the subclasses
"""
pass pass
def _request_automatic_caption(self, video_id, webpage): def _request_automatic_caption(self, video_id, webpage):
""" returns {sub_lang: sub} or {} if not available """ """
""" Must be redefined by the subclasses """ returns {sub_lang: sub} or {} if not available
pass Must be redefined by the subclasses that support automatic captions,
otherwise it will return {}
"""
class NoAutoSubtitlesInfoExtractor(SubtitlesInfoExtractor):
""" A subtitle class for the servers that don't support auto-captions"""
def _request_automatic_caption(self, video_id, webpage):
self._downloader.report_warning(u'Automatic Captions not supported by this server') self._downloader.report_warning(u'Automatic Captions not supported by this server')
return {} return {}
def extract_subtitles(self, video_id, video_webpage=None):
"""
Extract the subtitles and/or the automatic captions if requested.
Returns None or a dictionary in the format {sub_lang: sub}
"""
video_subtitles = None
if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
video_subtitles = self._extract_subtitles(video_id)
elif self._downloader.params.get('writeautomaticsub', False):
video_subtitles = self._request_automatic_caption(video_id, video_webpage)
return video_subtitles

View File

@ -707,12 +707,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor, SubtitlesInfoExtractor):
video_description = u'' video_description = u''
# subtitles # subtitles
video_subtitles = None video_subtitles = self.extract_subtitles(video_id, video_webpage)
if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
video_subtitles = self._extract_subtitles(video_id)
elif self._downloader.params.get('writeautomaticsub', False):
video_subtitles = self._request_automatic_caption(video_id, video_webpage)
if self._downloader.params.get('listsubtitles', False): if self._downloader.params.get('listsubtitles', False):
self._list_available_subtitles(video_id) self._list_available_subtitles(video_id)