]>
Commit | Line | Data |
---|---|---|
f853f859 PH |
1 | from __future__ import unicode_literals |
2 | ||
9fd5ce0c PH |
3 | import json |
4 | import re | |
5 | ||
a9a3876d | 6 | from .subtitles import SubtitlesInfoExtractor |
9fd5ce0c | 7 | |
4ed3e510 | 8 | from ..utils import ( |
4ed3e510 IM |
9 | RegexNotFoundError, |
10 | ) | |
11 | ||
f853f859 | 12 | |
a9a3876d | 13 | class TEDIE(SubtitlesInfoExtractor): |
652bee05 | 14 | _VALID_URL=r'''(?x)http://www\.ted\.com/ |
9fd5ce0c PH |
15 | ( |
16 | ((?P<type_playlist>playlists)/(?P<playlist_id>\d+)) # We have a playlist | |
17 | | | |
18 | ((?P<type_talk>talks)) # We have a simple talk | |
19 | ) | |
20 | (/lang/(.*?))? # The url may contain the language | |
21 | /(?P<name>\w+) # Here goes the name and then ".html" | |
22 | ''' | |
6f5ac90c | 23 | _TEST = { |
f853f859 PH |
24 | 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html', |
25 | 'file': '102.mp4', | |
26dca166 | 26 | 'md5': '4ea1dada91e4174b53dac2bb8ace429d', |
f853f859 | 27 | 'info_dict': { |
652bee05 JMF |
28 | 'title': 'The illusion of consciousness', |
29 | 'description': 'Philosopher Dan Dennett makes a compelling argument that not only don\'t we understand our own consciousness, but that half the time our brains are actively fooling us.', | |
30 | 'uploader': 'Dan Dennett', | |
6f5ac90c PH |
31 | } |
32 | } | |
9fd5ce0c | 33 | |
652bee05 JMF |
34 | _FORMATS_PREFERENCE = { |
35 | 'low': 1, | |
36 | 'medium': 2, | |
37 | 'high': 3, | |
38 | } | |
9fd5ce0c PH |
39 | |
40 | def _real_extract(self, url): | |
41 | m=re.match(self._VALID_URL, url, re.VERBOSE) | |
42 | if m.group('type_talk'): | |
be6dfd1b | 43 | return self._talk_info(url) |
9fd5ce0c PH |
44 | else : |
45 | playlist_id=m.group('playlist_id') | |
46 | name=m.group('name') | |
47 | self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name)) | |
48 | return [self._playlist_videos_info(url,name,playlist_id)] | |
49 | ||
fc2ef392 PH |
50 | |
51 | def _playlist_videos_info(self, url, name, playlist_id): | |
9fd5ce0c | 52 | '''Returns the videos of the playlist''' |
fc2ef392 PH |
53 | |
54 | webpage = self._download_webpage( | |
f853f859 | 55 | url, playlist_id, 'Downloading playlist webpage') |
fc2ef392 PH |
56 | matches = re.finditer( |
57 | r'<p\s+class="talk-title[^"]*"><a\s+href="(?P<talk_url>/talks/[^"]+\.html)">[^<]*</a></p>', | |
58 | webpage) | |
9fd5ce0c PH |
59 | |
60 | playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>', | |
61 | webpage, 'playlist title') | |
62 | ||
fc2ef392 PH |
63 | playlist_entries = [ |
64 | self.url_result(u'http://www.ted.com' + m.group('talk_url'), 'TED') | |
65 | for m in matches | |
66 | ] | |
67 | return self.playlist_result( | |
68 | playlist_entries, playlist_id=playlist_id, playlist_title=playlist_title) | |
9fd5ce0c PH |
69 | |
70 | def _talk_info(self, url, video_id=0): | |
71 | """Return the video for the talk in the url""" | |
652bee05 | 72 | m = re.match(self._VALID_URL, url) |
9fd5ce0c PH |
73 | video_name = m.group('name') |
74 | webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % video_name) | |
75 | self.report_extraction(video_name) | |
a9a3876d | 76 | |
652bee05 JMF |
77 | info_json = self._search_regex(r'"talkPage.init",({.+})\)</script>', webpage, 'info json') |
78 | info = json.loads(info_json) | |
79 | talk_info = info['talks'][0] | |
a9a3876d | 80 | |
652bee05 JMF |
81 | formats = [{ |
82 | 'ext': 'mp4', | |
83 | 'url': format_url, | |
84 | 'format_id': format_id, | |
85 | 'format': format_id, | |
86 | 'preference': self._FORMATS_PREFERENCE.get(format_id, -1), | |
87 | } for (format_id, format_url) in talk_info['nativeDownloads'].items()] | |
88 | self._sort_formats(formats) | |
89 | ||
90 | video_id = talk_info['id'] | |
a9a3876d | 91 | # subtitles |
652bee05 | 92 | video_subtitles = self.extract_subtitles(video_id, talk_info) |
a9a3876d | 93 | if self._downloader.params.get('listsubtitles', False): |
652bee05 | 94 | self._list_available_subtitles(video_id, talk_info) |
a9a3876d IM |
95 | return |
96 | ||
463a9087 | 97 | return { |
a9a3876d | 98 | 'id': video_id, |
652bee05 JMF |
99 | 'title': talk_info['title'], |
100 | 'uploader': talk_info['speaker'], | |
101 | 'thumbnail': talk_info['thumb'], | |
102 | 'description': self._og_search_description(webpage), | |
a9a3876d | 103 | 'subtitles': video_subtitles, |
0d8cb1cc PH |
104 | 'formats': formats, |
105 | } | |
106 | ||
652bee05 JMF |
107 | def _get_available_subtitles(self, video_id, talk_info): |
108 | languages = [lang['languageCode'] for lang in talk_info.get('languages', [])] | |
109 | if languages: | |
110 | sub_lang_list = {} | |
111 | for l in languages: | |
112 | url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l) | |
113 | sub_lang_list[l] = url | |
114 | return sub_lang_list | |
115 | else: | |
4ed3e510 | 116 | self._downloader.report_warning(u'video doesn\'t have subtitles') |
652bee05 | 117 | return {} |