[ted] Fix video extraction
[youtube-dl] / youtube_dl / extractor / ted.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .subtitles import SubtitlesInfoExtractor
7
8 from ..utils import (
9     RegexNotFoundError,
10 )
11
12
13 class TEDIE(SubtitlesInfoExtractor):
14     _VALID_URL=r'''(?x)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                    '''
23     _TEST = {
24         'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
25         'file': '102.mp4',
26         'md5': '4ea1dada91e4174b53dac2bb8ace429d',
27         'info_dict': {
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',
31         }
32     }
33
34     _FORMATS_PREFERENCE = {
35         'low': 1,
36         'medium': 2,
37         'high': 3,
38     }
39
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)
44         else :
45             playlist_id=m.group('playlist_id')
46             name=m.group('name')
47             self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
48             return [self._playlist_videos_info(url,name,playlist_id)]
49
50
51     def _playlist_videos_info(self, url, name, playlist_id):
52         '''Returns the videos of the playlist'''
53
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>',
58             webpage)
59
60         playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>',
61                                                  webpage, 'playlist title')
62
63         playlist_entries = [
64             self.url_result(u'http://www.ted.com' + m.group('talk_url'), 'TED')
65             for m in matches
66         ]
67         return self.playlist_result(
68             playlist_entries, playlist_id=playlist_id, playlist_title=playlist_title)
69
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)
76
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]
80
81         formats = [{
82             'ext': 'mp4',
83             'url': format_url,
84             'format_id': format_id,
85             'format': 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)
89
90         video_id = talk_info['id']
91         # subtitles
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)
95             return
96
97         return {
98             'id': video_id,
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,
104             'formats': formats,
105         }
106
107     def _get_available_subtitles(self, video_id, talk_info):
108         languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
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
115         else:
116             self._downloader.report_warning(u'video doesn\'t have subtitles')
117             return {}