[subtitles] Also list the available automatic captions languages with '--list-sub'
[youtube-dl] / youtube_dl / extractor / subtitles.py
1 from .common import InfoExtractor
2
3 from ..utils import (
4     compat_str,
5     ExtractorError,
6 )
7
8
9 class SubtitlesInfoExtractor(InfoExtractor):
10
11     def _list_available_subtitles(self, video_id, webpage=None):
12         """ outputs the available subtitles for the video """
13         sub_lang_list = self._get_available_subtitles(video_id)
14         auto_captions_list = self._get_available_automatic_caption(video_id, webpage)
15         sub_lang = ",".join(list(sub_lang_list.keys()))
16         self.to_screen(u'%s: Available subtitles for video: %s' %
17                        (video_id, sub_lang))
18         auto_lang = ",".join(auto_captions_list.keys())
19         self.to_screen(u'%s: Available automatic captions for video: %s' %
20                        (video_id, auto_lang))
21
22     def extract_subtitles(self, video_id, video_webpage=None):
23         """ returns {sub_lang: sub} or {} if subtitles not found """
24         if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
25             available_subs_list = self._get_available_subtitles(video_id)
26         elif self._downloader.params.get('writeautomaticsub', False):
27             available_subs_list = self._get_available_automatic_caption(video_id, video_webpage)
28         else:
29             return None
30
31         if not available_subs_list:  # error, it didn't get the available subtitles
32             return {}
33         if self._downloader.params.get('allsubtitles', False):
34             sub_lang_list = available_subs_list
35         else:
36             if self._downloader.params.get('subtitleslangs', False):
37                 requested_langs = self._downloader.params.get('subtitleslangs')
38             elif 'en' in available_subs_list:
39                 requested_langs = ['en']
40             else:
41                 requested_langs = [list(available_subs_list.keys())[0]]
42
43             sub_lang_list = {}
44             for sub_lang in requested_langs:
45                 if not sub_lang in available_subs_list:
46                     self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
47                     continue
48                 sub_lang_list[sub_lang] = available_subs_list[sub_lang]
49
50         subtitles = {}
51         for sub_lang, url in sub_lang_list.items():
52             subtitle = self._request_subtitle_url(sub_lang, url)
53             if subtitle:
54                 subtitles[sub_lang] = subtitle
55         return subtitles
56
57     def _request_subtitle_url(self, sub_lang, url):
58         """ makes the http request for the subtitle """
59         try:
60             sub = self._download_webpage(url, None, note=False)
61         except ExtractorError as err:
62             self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
63             return
64         if not sub:
65             self._downloader.report_warning(u'Did not fetch video subtitles')
66             return
67         return sub
68
69     def _get_available_subtitles(self, video_id):
70         """
71         returns {sub_lang: url} or {} if not available
72         Must be redefined by the subclasses
73         """
74         pass
75
76     def _get_available_automatic_caption(self, video_id, webpage):
77         """
78         returns {sub_lang: url} or {} if not available
79         Must be redefined by the subclasses that support automatic captions,
80         otherwise it will return {}
81         """
82         self._downloader.report_warning(u'Automatic Captions not supported by this server')
83         return {}