1 from __future__ import unicode_literals
6 from .subtitles import SubtitlesInfoExtractor
14 class TEDIE(SubtitlesInfoExtractor):
15 _VALID_URL=r'''(?x)http://www\.ted\.com/
17 (?P<type_playlist>playlists(?:/\d+)?) # We have a playlist
19 ((?P<type_talk>talks)) # We have a simple talk
21 (/lang/(.*?))? # The url may contain the language
22 /(?P<name>\w+) # Here goes the name and then ".html"
25 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
27 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
29 'title': 'The illusion of consciousness',
30 '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.',
31 'uploader': 'Dan Dennett',
35 _FORMATS_PREFERENCE = {
41 def _extract_info(self, webpage):
42 info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>', webpage, 'info json')
43 return json.loads(info_json)
45 def _real_extract(self, url):
46 m=re.match(self._VALID_URL, url, re.VERBOSE)
47 if m.group('type_talk'):
48 return self._talk_info(url)
51 return self._playlist_videos_info(url, name)
54 def _playlist_videos_info(self, url, name):
55 '''Returns the videos of the playlist'''
57 webpage = self._download_webpage(url, name,
58 'Downloading playlist webpage')
59 info = self._extract_info(webpage)
60 playlist_info = info['playlist']
63 self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
64 for talk in info['talks']
66 return self.playlist_result(
68 playlist_id=compat_str(playlist_info['id']),
69 playlist_title=playlist_info['title'])
71 def _talk_info(self, url, video_id=0):
72 """Return the video for the talk in the url"""
73 m = re.match(self._VALID_URL, url)
74 video_name = m.group('name')
75 webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % 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 = 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')