]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/ted.py
Merge pull request #2567 from jaimeMF/sphinx-docs
[yt-dlp.git] / 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)
15 (?P<proto>https?://)
16 (?P<type>www|embed)(?P<urlmain>\.ted\.com/
17 (
18 (?P<type_playlist>playlists(?:/\d+)?) # We have a playlist
19 |
20 ((?P<type_talk>talks)) # We have a simple talk
21 )
22 (/lang/(.*?))? # The url may contain the language
23 /(?P<name>\w+) # Here goes the name and then ".html"
24 .*)$
25 '''
26 _TEST = {
27 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
28 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
29 'info_dict': {
30 'id': '102',
31 'ext': 'mp4',
32 'title': 'The illusion of consciousness',
33 'description': ('Philosopher Dan Dennett makes a compelling '
34 'argument that not only don\'t we understand our own '
35 'consciousness, but that half the time our brains are '
36 'actively fooling us.'),
37 'uploader': 'Dan Dennett',
38 }
39 }
40
41 _FORMATS_PREFERENCE = {
42 'low': 1,
43 'medium': 2,
44 'high': 3,
45 }
46
47 def _extract_info(self, webpage):
48 info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
49 webpage, 'info json')
50 return json.loads(info_json)
51
52 def _real_extract(self, url):
53 m = re.match(self._VALID_URL, url, re.VERBOSE)
54 if m.group('type') == 'embed':
55 desktop_url = m.group('proto') + 'www' + m.group('urlmain')
56 return self.url_result(desktop_url, 'TED')
57 name = m.group('name')
58 if m.group('type_talk'):
59 return self._talk_info(url, name)
60 else:
61 return self._playlist_videos_info(url, name)
62
63 def _playlist_videos_info(self, url, name):
64 '''Returns the videos of the playlist'''
65
66 webpage = self._download_webpage(url, name,
67 'Downloading playlist webpage')
68 info = self._extract_info(webpage)
69 playlist_info = info['playlist']
70
71 playlist_entries = [
72 self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
73 for talk in info['talks']
74 ]
75 return self.playlist_result(
76 playlist_entries,
77 playlist_id=compat_str(playlist_info['id']),
78 playlist_title=playlist_info['title'])
79
80 def _talk_info(self, url, video_name):
81 webpage = self._download_webpage(url, video_name)
82 self.report_extraction(video_name)
83
84 talk_info = self._extract_info(webpage)['talks'][0]
85
86 formats = [{
87 'ext': 'mp4',
88 'url': format_url,
89 'format_id': format_id,
90 'format': format_id,
91 'preference': self._FORMATS_PREFERENCE.get(format_id, -1),
92 } for (format_id, format_url) in talk_info['nativeDownloads'].items()]
93 self._sort_formats(formats)
94
95 video_id = compat_str(talk_info['id'])
96 # subtitles
97 video_subtitles = self.extract_subtitles(video_id, talk_info)
98 if self._downloader.params.get('listsubtitles', False):
99 self._list_available_subtitles(video_id, talk_info)
100 return
101
102 thumbnail = talk_info['thumb']
103 if not thumbnail.startswith('http'):
104 thumbnail = 'http://' + thumbnail
105 return {
106 'id': video_id,
107 'title': talk_info['title'],
108 'uploader': talk_info['speaker'],
109 'thumbnail': thumbnail,
110 'description': self._og_search_description(webpage),
111 'subtitles': video_subtitles,
112 'formats': formats,
113 }
114
115 def _get_available_subtitles(self, video_id, talk_info):
116 languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
117 if languages:
118 sub_lang_list = {}
119 for l in languages:
120 url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
121 sub_lang_list[l] = url
122 return sub_lang_list
123 else:
124 self._downloader.report_warning(u'video doesn\'t have subtitles')
125 return {}