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