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)/(?P<playlist_id>\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',
26 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
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',
34 _FORMATS_PREFERENCE = {
40 def _real_extract(self, url):
41 m=re.match(self._VALID_URL, url, re.VERBOSE)
42 if m.group('type_talk'):
43 return self._talk_info(url)
45 playlist_id=m.group('playlist_id')
47 self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
48 return [self._playlist_videos_info(url,name,playlist_id)]
51 def _playlist_videos_info(self, url, name, playlist_id):
52 '''Returns the videos of the playlist'''
54 webpage = self._download_webpage(
55 url, playlist_id, 'Downloading playlist webpage')
56 matches = re.finditer(
57 r'<p\s+class="talk-title[^"]*"><a\s+href="(?P<talk_url>/talks/[^"]+\.html)">[^<]*</a></p>',
60 playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>',
61 webpage, 'playlist title')
64 self.url_result(u'http://www.ted.com' + m.group('talk_url'), 'TED')
67 return self.playlist_result(
68 playlist_entries, playlist_id=playlist_id, playlist_title=playlist_title)
70 def _talk_info(self, url, video_id=0):
71 """Return the video for the talk in the url"""
72 m = re.match(self._VALID_URL, url)
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)
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]
84 'format_id': 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)
90 video_id = talk_info['id']
92 video_subtitles = self.extract_subtitles(video_id, talk_info)
93 if self._downloader.params.get('listsubtitles', False):
94 self._list_available_subtitles(video_id, talk_info)
99 'title': talk_info['title'],
100 'uploader': talk_info['speaker'],
101 'thumbnail': talk_info['thumb'],
102 'description': self._og_search_description(webpage),
103 'subtitles': video_subtitles,
107 def _get_available_subtitles(self, video_id, talk_info):
108 languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
112 url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
113 sub_lang_list[l] = url
116 self._downloader.report_warning(u'video doesn\'t have subtitles')