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