4 from .subtitles import SubtitlesInfoExtractor
11 class TEDIE(SubtitlesInfoExtractor):
12 _VALID_URL=r'''http://www\.ted\.com/
14 ((?P<type_playlist>playlists)/(?P<playlist_id>\d+)) # We have a playlist
16 ((?P<type_talk>talks)) # We have a simple talk
18 (/lang/(.*?))? # The url may contain the language
19 /(?P<name>\w+) # Here goes the name and then ".html"
22 u'url': u'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
24 u'md5': u'2d76ee1576672e0bd8f187513267adf6',
26 u"description": u"md5:c6fa72e6eedbd938c9caf6b2702f5922",
27 u"title": u"Dan Dennett: The illusion of consciousness"
32 def suitable(cls, url):
33 """Receives a URL and returns True if suitable for this IE."""
34 return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
36 def _real_extract(self, url):
37 m=re.match(self._VALID_URL, url, re.VERBOSE)
38 if m.group('type_talk'):
39 return self._talk_info(url)
41 playlist_id=m.group('playlist_id')
43 self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
44 return [self._playlist_videos_info(url,name,playlist_id)]
47 def _playlist_videos_info(self, url, name, playlist_id):
48 '''Returns the videos of the playlist'''
50 webpage = self._download_webpage(
51 url, playlist_id, u'Downloading playlist webpage')
52 matches = re.finditer(
53 r'<p\s+class="talk-title[^"]*"><a\s+href="(?P<talk_url>/talks/[^"]+\.html)">[^<]*</a></p>',
56 playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>',
57 webpage, 'playlist title')
60 self.url_result(u'http://www.ted.com' + m.group('talk_url'), 'TED')
63 return self.playlist_result(
64 playlist_entries, playlist_id=playlist_id, playlist_title=playlist_title)
66 def _talk_info(self, url, video_id=0):
67 """Return the video for the talk in the url"""
68 m = re.match(self._VALID_URL, url,re.VERBOSE)
69 video_name = m.group('name')
70 webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % video_name)
71 self.report_extraction(video_name)
72 # If the url includes the language we get the title translated
73 title = self._html_search_regex(r'<span .*?id="altHeadline".+?>(?P<title>.*)</span>',
75 json_data = self._search_regex(r'<script.*?>var talkDetails = ({.*?})</script>',
77 info = json.loads(json_data)
78 desc = self._html_search_regex(r'<div class="talk-intro">.*?<p.*?>(.*?)</p>',
79 webpage, 'description', flags = re.DOTALL)
81 thumbnail = self._search_regex(r'</span>[\s.]*</div>[\s.]*<img src="(.*?)"',
85 'url': stream['file'],
86 'format': stream['id']
87 } for stream in info['htmlStreams']]
92 video_subtitles = self.extract_subtitles(video_id, webpage)
93 if self._downloader.params.get('listsubtitles', False):
94 self._list_available_subtitles(video_id, webpage)
100 'thumbnail': thumbnail,
102 'subtitles': video_subtitles,
106 def _get_available_subtitles(self, video_id, webpage):
108 options = self._search_regex(r'(?:<select name="subtitles_language_select" id="subtitles_language_select">)(.*?)(?:</select>)', webpage, 'subtitles_language_select', flags=re.DOTALL)
109 languages = re.findall(r'(?:<option value=")(\S+)"', options)
113 url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
114 sub_lang_list[l] = url
116 except RegexNotFoundError as err:
117 self._downloader.report_warning(u'video doesn\'t have subtitles')