]>
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): |
953e32b2 | 10 | |
d665f8d3 | 11 | def _list_available_subtitles(self, video_id, webpage=None): |
69df680b IM |
12 | """ outputs the available subtitles for the video """ |
13 | sub_lang_list = self._get_available_subtitles(video_id) | |
d665f8d3 | 14 | auto_captions_list = self._get_available_automatic_caption(video_id, webpage) |
953e32b2 | 15 | sub_lang = ",".join(list(sub_lang_list.keys())) |
8377574c IM |
16 | self.to_screen(u'%s: Available subtitles for video: %s' % |
17 | (video_id, sub_lang)) | |
d665f8d3 JMF |
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)) | |
953e32b2 | 21 | |
055e6f36 | 22 | def extract_subtitles(self, video_id, video_webpage=None): |
69df680b | 23 | """ returns {sub_lang: sub} or {} if subtitles not found """ |
055e6f36 JMF |
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 | ||
d6e203b3 | 31 | if not available_subs_list: # error, it didn't get the available subtitles |
953e32b2 | 32 | return {} |
d6e203b3 IM |
33 | if self._downloader.params.get('allsubtitles', False): |
34 | sub_lang_list = available_subs_list | |
35 | else: | |
055e6f36 JMF |
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]] | |
d6e203b3 | 42 | |
055e6f36 JMF |
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] | |
8377574c | 49 | |
953e32b2 | 50 | subtitles = {} |
d6e203b3 | 51 | for sub_lang, url in sub_lang_list.items(): |
8377574c | 52 | subtitle = self._request_subtitle_url(sub_lang, url) |
953e32b2 IM |
53 | if subtitle: |
54 | subtitles[sub_lang] = subtitle | |
55 | return subtitles | |
56 | ||
8377574c | 57 | def _request_subtitle_url(self, sub_lang, url): |
69df680b | 58 | """ makes the http request for the subtitle """ |
953e32b2 | 59 | try: |
7fad1c63 JMF |
60 | sub = self._download_webpage(url, None, note=False) |
61 | except ExtractorError as err: | |
953e32b2 IM |
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): | |
d82134c3 JMF |
70 | """ |
71 | returns {sub_lang: url} or {} if not available | |
72 | Must be redefined by the subclasses | |
73 | """ | |
953e32b2 IM |
74 | pass |
75 | ||
055e6f36 | 76 | def _get_available_automatic_caption(self, video_id, webpage): |
d82134c3 | 77 | """ |
055e6f36 | 78 | returns {sub_lang: url} or {} if not available |
d82134c3 JMF |
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 {} |