]>
Commit | Line | Data |
---|---|---|
9fd5ce0c PH |
1 | import json |
2 | import re | |
3 | ||
a9a3876d | 4 | from .subtitles import SubtitlesInfoExtractor |
9fd5ce0c | 5 | |
a9a3876d | 6 | class TEDIE(SubtitlesInfoExtractor): |
9fd5ce0c PH |
7 | _VALID_URL=r'''http://www\.ted\.com/ |
8 | ( | |
9 | ((?P<type_playlist>playlists)/(?P<playlist_id>\d+)) # We have a playlist | |
10 | | | |
11 | ((?P<type_talk>talks)) # We have a simple talk | |
12 | ) | |
13 | (/lang/(.*?))? # The url may contain the language | |
14 | /(?P<name>\w+) # Here goes the name and then ".html" | |
15 | ''' | |
6f5ac90c PH |
16 | _TEST = { |
17 | u'url': u'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html', | |
18 | u'file': u'102.mp4', | |
9c5cd094 | 19 | u'md5': u'2d76ee1576672e0bd8f187513267adf6', |
6f5ac90c PH |
20 | u'info_dict': { |
21 | u"description": u"md5:c6fa72e6eedbd938c9caf6b2702f5922", | |
22 | u"title": u"Dan Dennett: The illusion of consciousness" | |
23 | } | |
24 | } | |
9fd5ce0c PH |
25 | |
26 | @classmethod | |
27 | def suitable(cls, url): | |
28 | """Receives a URL and returns True if suitable for this IE.""" | |
29 | return re.match(cls._VALID_URL, url, re.VERBOSE) is not None | |
30 | ||
31 | def _real_extract(self, url): | |
32 | m=re.match(self._VALID_URL, url, re.VERBOSE) | |
33 | if m.group('type_talk'): | |
34 | return [self._talk_info(url)] | |
35 | else : | |
36 | playlist_id=m.group('playlist_id') | |
37 | name=m.group('name') | |
38 | self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name)) | |
39 | return [self._playlist_videos_info(url,name,playlist_id)] | |
40 | ||
41 | def _playlist_videos_info(self,url,name,playlist_id=0): | |
42 | '''Returns the videos of the playlist''' | |
43 | video_RE=r''' | |
44 | <li\ id="talk_(\d+)"([.\s]*?)data-id="(?P<video_id>\d+)" | |
45 | ([.\s]*?)data-playlist_item_id="(\d+)" | |
46 | ([.\s]*?)data-mediaslug="(?P<mediaSlug>.+?)" | |
47 | ''' | |
48 | video_name_RE=r'<p\ class="talk-title"><a href="(?P<talk_url>/talks/(.+).html)">(?P<fullname>.+?)</a></p>' | |
49 | webpage=self._download_webpage(url, playlist_id, 'Downloading playlist webpage') | |
50 | m_videos=re.finditer(video_RE,webpage,re.VERBOSE) | |
51 | m_names=re.finditer(video_name_RE,webpage) | |
52 | ||
53 | playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>', | |
54 | webpage, 'playlist title') | |
55 | ||
56 | playlist_entries = [] | |
57 | for m_video, m_name in zip(m_videos,m_names): | |
58 | talk_url='http://www.ted.com%s' % m_name.group('talk_url') | |
59 | playlist_entries.append(self.url_result(talk_url, 'TED')) | |
60 | return self.playlist_result(playlist_entries, playlist_id = playlist_id, playlist_title = playlist_title) | |
61 | ||
62 | def _talk_info(self, url, video_id=0): | |
63 | """Return the video for the talk in the url""" | |
64 | m = re.match(self._VALID_URL, url,re.VERBOSE) | |
65 | video_name = m.group('name') | |
66 | webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % video_name) | |
67 | self.report_extraction(video_name) | |
68 | # If the url includes the language we get the title translated | |
177ed935 | 69 | title = self._html_search_regex(r'<span .*?id="altHeadline".+?>(?P<title>.*)</span>', |
9fd5ce0c PH |
70 | webpage, 'title') |
71 | json_data = self._search_regex(r'<script.*?>var talkDetails = ({.*?})</script>', | |
72 | webpage, 'json data') | |
73 | info = json.loads(json_data) | |
74 | desc = self._html_search_regex(r'<div class="talk-intro">.*?<p.*?>(.*?)</p>', | |
75 | webpage, 'description', flags = re.DOTALL) | |
76 | ||
77 | thumbnail = self._search_regex(r'</span>[\s.]*</div>[\s.]*<img src="(.*?)"', | |
78 | webpage, 'thumbnail') | |
0d8cb1cc PH |
79 | formats = [{ |
80 | 'ext': 'mp4', | |
81 | 'url': stream['file'], | |
82 | 'format': stream['id'] | |
83 | } for stream in info['htmlStreams']] | |
a9a3876d IM |
84 | |
85 | video_id = info['id'] | |
86 | ||
87 | # subtitles | |
88 | video_subtitles = self.extract_subtitles(video_id, webpage) | |
89 | if self._downloader.params.get('listsubtitles', False): | |
90 | self._list_available_subtitles(video_id, webpage) | |
91 | return | |
92 | ||
9fd5ce0c | 93 | info = { |
a9a3876d | 94 | 'id': video_id, |
0d8cb1cc PH |
95 | 'title': title, |
96 | 'thumbnail': thumbnail, | |
97 | 'description': desc, | |
a9a3876d | 98 | 'subtitles': video_subtitles, |
0d8cb1cc PH |
99 | 'formats': formats, |
100 | } | |
101 | ||
102 | # TODO: Remove when #980 has been merged | |
103 | info.update(info['formats'][-1]) | |
104 | ||
9fd5ce0c | 105 | return info |
a9a3876d IM |
106 | |
107 | 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) | |
110 | if languages: | |
111 | sub_lang_list = {} | |
112 | for l in languages: | |
113 | url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l) | |
114 | sub_lang_list[l] = url | |
115 | return sub_lang_list | |
116 | return {} |