1 from __future__ import unicode_literals
6 from .subtitles import SubtitlesInfoExtractor
13 class TEDIE(SubtitlesInfoExtractor):
14 _VALID_URL = r'''(?x)http://www\.ted\.com/
16 (?P<type_playlist>playlists(?:/\d+)?) # We have a playlist
18 ((?P<type_talk>talks)) # We have a simple talk
20 (/lang/(.*?))? # The url may contain the language
21 /(?P<name>\w+) # Here goes the name and then ".html"
24 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
25 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
29 'title': 'The illusion of consciousness',
30 'description': ('Philosopher Dan Dennett makes a compelling '
31 'argument that not only don\'t we understand our own '
32 'consciousness, but that half the time our brains are '
33 'actively fooling us.'),
34 'uploader': 'Dan Dennett',
38 _FORMATS_PREFERENCE = {
44 def _extract_info(self, webpage):
45 info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
47 return json.loads(info_json)
49 def _real_extract(self, url):
50 m = re.match(self._VALID_URL, url, re.VERBOSE)
51 name = m.group('name')
52 if m.group('type_talk'):
53 return self._talk_info(url, name)
55 return self._playlist_videos_info(url, name)
57 def _playlist_videos_info(self, url, name):
58 '''Returns the videos of the playlist'''
60 webpage = self._download_webpage(url, name,
61 'Downloading playlist webpage')
62 info = self._extract_info(webpage)
63 playlist_info = info['playlist']
66 self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
67 for talk in info['talks']
69 return self.playlist_result(
71 playlist_id=compat_str(playlist_info['id']),
72 playlist_title=playlist_info['title'])
74 def _talk_info(self, url, video_name):
75 webpage = self._download_webpage(url, video_name)
76 self.report_extraction(video_name)
78 talk_info = self._extract_info(webpage)['talks'][0]
83 'format_id': format_id,
85 'preference': self._FORMATS_PREFERENCE.get(format_id, -1),
86 } for (format_id, format_url) in talk_info['nativeDownloads'].items()]
87 self._sort_formats(formats)
89 video_id = compat_str(talk_info['id'])
91 video_subtitles = self.extract_subtitles(video_id, talk_info)
92 if self._downloader.params.get('listsubtitles', False):
93 self._list_available_subtitles(video_id, talk_info)
98 'title': talk_info['title'],
99 'uploader': talk_info['speaker'],
100 'thumbnail': talk_info['thumb'],
101 'description': self._og_search_description(webpage),
102 'subtitles': video_subtitles,
106 def _get_available_subtitles(self, video_id, talk_info):
107 languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
111 url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
112 sub_lang_list[l] = url
115 self._downloader.report_warning(u'video doesn\'t have subtitles')