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