]>
Commit | Line | Data |
---|---|---|
855dc479 | 1 | from __future__ import unicode_literals |
953e32b2 IM |
2 | from .common import InfoExtractor |
3 | ||
855dc479 | 4 | from ..compat import compat_str |
953e32b2 | 5 | from ..utils import ( |
7fad1c63 | 6 | ExtractorError, |
953e32b2 IM |
7 | ) |
8 | ||
9 | ||
54d39d8b | 10 | class SubtitlesInfoExtractor(InfoExtractor): |
6bc520c2 JMF |
11 | @property |
12 | def _have_to_download_any_subtitles(self): | |
13 | return any([self._downloader.params.get('writesubtitles', False), | |
0b7f3118 | 14 | self._downloader.params.get('writeautomaticsub')]) |
953e32b2 | 15 | |
1f343eaa | 16 | def _list_available_subtitles(self, video_id, webpage): |
69df680b | 17 | """ outputs the available subtitles for the video """ |
1f343eaa | 18 | sub_lang_list = self._get_available_subtitles(video_id, webpage) |
d665f8d3 | 19 | auto_captions_list = self._get_available_automatic_caption(video_id, webpage) |
953e32b2 | 20 | sub_lang = ",".join(list(sub_lang_list.keys())) |
855dc479 | 21 | self.to_screen('%s: Available subtitles for video: %s' % |
8377574c | 22 | (video_id, sub_lang)) |
d665f8d3 | 23 | auto_lang = ",".join(auto_captions_list.keys()) |
855dc479 | 24 | self.to_screen('%s: Available automatic captions for video: %s' % |
d665f8d3 | 25 | (video_id, auto_lang)) |
953e32b2 | 26 | |
1f343eaa | 27 | def extract_subtitles(self, video_id, webpage): |
6bc520c2 JMF |
28 | """ |
29 | returns {sub_lang: sub} ,{} if subtitles not found or None if the | |
30 | subtitles aren't requested. | |
31 | """ | |
32 | if not self._have_to_download_any_subtitles: | |
055e6f36 | 33 | return None |
6bc520c2 JMF |
34 | available_subs_list = {} |
35 | if self._downloader.params.get('writeautomaticsub', False): | |
1f343eaa | 36 | available_subs_list.update(self._get_available_automatic_caption(video_id, webpage)) |
0b7f3118 | 37 | if self._downloader.params.get('writesubtitles', False): |
1f343eaa | 38 | available_subs_list.update(self._get_available_subtitles(video_id, webpage)) |
055e6f36 | 39 | |
d6e203b3 | 40 | if not available_subs_list: # error, it didn't get the available subtitles |
953e32b2 | 41 | return {} |
d6e203b3 IM |
42 | if self._downloader.params.get('allsubtitles', False): |
43 | sub_lang_list = available_subs_list | |
44 | else: | |
055e6f36 JMF |
45 | if self._downloader.params.get('subtitleslangs', False): |
46 | requested_langs = self._downloader.params.get('subtitleslangs') | |
47 | elif 'en' in available_subs_list: | |
48 | requested_langs = ['en'] | |
49 | else: | |
50 | requested_langs = [list(available_subs_list.keys())[0]] | |
d6e203b3 | 51 | |
055e6f36 JMF |
52 | sub_lang_list = {} |
53 | for sub_lang in requested_langs: | |
5f6a1245 | 54 | if sub_lang not in available_subs_list: |
855dc479 | 55 | self._downloader.report_warning('no closed captions found in the specified language "%s"' % sub_lang) |
055e6f36 JMF |
56 | continue |
57 | sub_lang_list[sub_lang] = available_subs_list[sub_lang] | |
8377574c | 58 | |
953e32b2 | 59 | subtitles = {} |
d6e203b3 | 60 | for sub_lang, url in sub_lang_list.items(): |
8377574c | 61 | subtitle = self._request_subtitle_url(sub_lang, url) |
953e32b2 IM |
62 | if subtitle: |
63 | subtitles[sub_lang] = subtitle | |
64 | return subtitles | |
65 | ||
b4bcffef PH |
66 | def _download_subtitle_url(self, sub_lang, url): |
67 | return self._download_webpage(url, None, note=False) | |
68 | ||
8377574c | 69 | def _request_subtitle_url(self, sub_lang, url): |
69df680b | 70 | """ makes the http request for the subtitle """ |
953e32b2 | 71 | try: |
5cef4ff0 | 72 | sub = self._download_subtitle_url(sub_lang, url) |
7fad1c63 | 73 | except ExtractorError as err: |
855dc479 | 74 | self._downloader.report_warning('unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err))) |
953e32b2 IM |
75 | return |
76 | if not sub: | |
855dc479 | 77 | self._downloader.report_warning('Did not fetch video subtitles') |
953e32b2 | 78 | return |
5cef4ff0 | 79 | return sub |
953e32b2 | 80 | |
1f343eaa | 81 | def _get_available_subtitles(self, video_id, webpage): |
d82134c3 JMF |
82 | """ |
83 | returns {sub_lang: url} or {} if not available | |
84 | Must be redefined by the subclasses | |
85 | """ | |
b4bcffef PH |
86 | |
87 | # By default, allow implementations to simply pass in the result | |
88 | assert isinstance(webpage, dict), \ | |
89 | '_get_available_subtitles not implemented' | |
90 | return webpage | |
953e32b2 | 91 | |
055e6f36 | 92 | def _get_available_automatic_caption(self, video_id, webpage): |
d82134c3 | 93 | """ |
055e6f36 | 94 | returns {sub_lang: url} or {} if not available |
d82134c3 JMF |
95 | Must be redefined by the subclasses that support automatic captions, |
96 | otherwise it will return {} | |
97 | """ | |
855dc479 | 98 | self._downloader.report_warning('Automatic Captions not supported by this server') |
d82134c3 | 99 | return {} |