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