]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ted.py
[comedycentral] Add a testcase for extended-interviews URLs (#2636)
[yt-dlp.git] / youtube_dl / extractor / ted.py
CommitLineData
f853f859
PH
1from __future__ import unicode_literals
2
9fd5ce0c
PH
3import json
4import re
5
a9a3876d 6from .subtitles import SubtitlesInfoExtractor
9fd5ce0c 7
4ed3e510 8from ..utils import (
ca1fee34 9 compat_str,
4ed3e510
IM
10)
11
f853f859 12
a9a3876d 13class TEDIE(SubtitlesInfoExtractor):
aab74fa1
PH
14 _VALID_URL = r'''(?x)
15 (?P<proto>https?://)
16 (?P<type>www|embed)(?P<urlmain>\.ted\.com/
bacac173
JMF
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"
aab74fa1 24 .*)$
bacac173 25 '''
6f5ac90c 26 _TEST = {
f853f859 27 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
26dca166 28 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
f853f859 29 'info_dict': {
7b9965ea
JMF
30 'id': '102',
31 'ext': 'mp4',
652bee05 32 'title': 'The illusion of consciousness',
bacac173
JMF
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.'),
652bee05 37 'uploader': 'Dan Dennett',
6f5ac90c
PH
38 }
39 }
9fd5ce0c 40
652bee05
JMF
41 _FORMATS_PREFERENCE = {
42 'low': 1,
43 'medium': 2,
44 'high': 3,
45 }
9fd5ce0c 46
ca1fee34 47 def _extract_info(self, webpage):
bacac173
JMF
48 info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
49 webpage, 'info json')
ca1fee34
JMF
50 return json.loads(info_json)
51
9fd5ce0c 52 def _real_extract(self, url):
bacac173 53 m = re.match(self._VALID_URL, url, re.VERBOSE)
aab74fa1
PH
54 if m.group('type') == 'embed':
55 desktop_url = m.group('proto') + 'www' + m.group('urlmain')
56 return self.url_result(desktop_url, 'TED')
bacac173 57 name = m.group('name')
9fd5ce0c 58 if m.group('type_talk'):
bacac173
JMF
59 return self._talk_info(url, name)
60 else:
ca1fee34 61 return self._playlist_videos_info(url, name)
9fd5ce0c 62
ca1fee34 63 def _playlist_videos_info(self, url, name):
9fd5ce0c 64 '''Returns the videos of the playlist'''
fc2ef392 65
ca1fee34
JMF
66 webpage = self._download_webpage(url, name,
67 'Downloading playlist webpage')
68 info = self._extract_info(webpage)
69 playlist_info = info['playlist']
9fd5ce0c 70
fc2ef392 71 playlist_entries = [
ca1fee34
JMF
72 self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
73 for talk in info['talks']
fc2ef392
PH
74 ]
75 return self.playlist_result(
ca1fee34
JMF
76 playlist_entries,
77 playlist_id=compat_str(playlist_info['id']),
78 playlist_title=playlist_info['title'])
9fd5ce0c 79
bacac173
JMF
80 def _talk_info(self, url, video_name):
81 webpage = self._download_webpage(url, video_name)
9fd5ce0c 82 self.report_extraction(video_name)
a9a3876d 83
ca1fee34 84 talk_info = self._extract_info(webpage)['talks'][0]
a9a3876d 85
652bee05
JMF
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
7b9965ea 95 video_id = compat_str(talk_info['id'])
a9a3876d 96 # subtitles
652bee05 97 video_subtitles = self.extract_subtitles(video_id, talk_info)
a9a3876d 98 if self._downloader.params.get('listsubtitles', False):
652bee05 99 self._list_available_subtitles(video_id, talk_info)
a9a3876d
IM
100 return
101
b6c1cecc
JMF
102 thumbnail = talk_info['thumb']
103 if not thumbnail.startswith('http'):
104 thumbnail = 'http://' + thumbnail
463a9087 105 return {
a9a3876d 106 'id': video_id,
652bee05
JMF
107 'title': talk_info['title'],
108 'uploader': talk_info['speaker'],
b6c1cecc 109 'thumbnail': thumbnail,
652bee05 110 'description': self._og_search_description(webpage),
a9a3876d 111 'subtitles': video_subtitles,
0d8cb1cc
PH
112 'formats': formats,
113 }
114
652bee05
JMF
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:
4ed3e510 124 self._downloader.report_warning(u'video doesn\'t have subtitles')
652bee05 125 return {}