]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/ted.py
[vevo] Centralize timestamp handling
[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     compat_str,
10 )
11
12
13 class TEDIE(SubtitlesInfoExtractor):
14     _VALID_URL = r'''(?x)http://www\.ted\.com/
15         (
16             (?P<type_playlist>playlists(?:/\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         'md5': '4ea1dada91e4174b53dac2bb8ace429d',
26         'info_dict': {
27             'id': '102',
28             'ext': 'mp4',
29             'title': 'The illusion of consciousness',
30             'description': ('Philosopher Dan Dennett makes a compelling '
31                 'argument that not only don\'t we understand our own '
32                 'consciousness, but that half the time our brains are '
33                 'actively fooling us.'),
34             'uploader': 'Dan Dennett',
35         }
36     }
37
38     _FORMATS_PREFERENCE = {
39         'low': 1,
40         'medium': 2,
41         'high': 3,
42     }
43
44     def _extract_info(self, webpage):
45         info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
46             webpage, 'info json')
47         return json.loads(info_json)
48
49     def _real_extract(self, url):
50         m = re.match(self._VALID_URL, url, re.VERBOSE)
51         name = m.group('name')
52         if m.group('type_talk'):
53             return self._talk_info(url, name)
54         else:
55             return self._playlist_videos_info(url, name)
56
57     def _playlist_videos_info(self, url, name):
58         '''Returns the videos of the playlist'''
59
60         webpage = self._download_webpage(url, name,
61             'Downloading playlist webpage')
62         info = self._extract_info(webpage)
63         playlist_info = info['playlist']
64
65         playlist_entries = [
66             self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
67             for talk in info['talks']
68         ]
69         return self.playlist_result(
70             playlist_entries,
71             playlist_id=compat_str(playlist_info['id']),
72             playlist_title=playlist_info['title'])
73
74     def _talk_info(self, url, video_name):
75         webpage = self._download_webpage(url, video_name)
76         self.report_extraction(video_name)
77
78         talk_info = self._extract_info(webpage)['talks'][0]
79
80         formats = [{
81             'ext': 'mp4',
82             'url': format_url,
83             'format_id': format_id,
84             'format': format_id,
85             'preference': self._FORMATS_PREFERENCE.get(format_id, -1),
86         } for (format_id, format_url) in talk_info['nativeDownloads'].items()]
87         self._sort_formats(formats)
88
89         video_id = compat_str(talk_info['id'])
90         # subtitles
91         video_subtitles = self.extract_subtitles(video_id, talk_info)
92         if self._downloader.params.get('listsubtitles', False):
93             self._list_available_subtitles(video_id, talk_info)
94             return
95
96         return {
97             'id': video_id,
98             'title': talk_info['title'],
99             'uploader': talk_info['speaker'],
100             'thumbnail': talk_info['thumb'],
101             'description': self._og_search_description(webpage),
102             'subtitles': video_subtitles,
103             'formats': formats,
104         }
105
106     def _get_available_subtitles(self, video_id, talk_info):
107         languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
108         if languages:
109             sub_lang_list = {}
110             for l in languages:
111                 url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
112                 sub_lang_list[l] = url
113             return sub_lang_list
114         else:
115             self._downloader.report_warning(u'video doesn\'t have subtitles')
116             return {}