]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ted.py
Imported Upstream version 2014.01.17.2
[youtubedl] / 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'''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 "description": "md5:c6fa72e6eedbd938c9caf6b2702f5922",
29 "title": "Dan Dennett: The illusion of consciousness"
30 }
31 }
32
33 @classmethod
34 def suitable(cls, url):
35 """Receives a URL and returns True if suitable for this IE."""
36 return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
37
38 def _real_extract(self, url):
39 m=re.match(self._VALID_URL, url, re.VERBOSE)
40 if m.group('type_talk'):
41 return self._talk_info(url)
42 else :
43 playlist_id=m.group('playlist_id')
44 name=m.group('name')
45 self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
46 return [self._playlist_videos_info(url,name,playlist_id)]
47
48
49 def _playlist_videos_info(self, url, name, playlist_id):
50 '''Returns the videos of the playlist'''
51
52 webpage = self._download_webpage(
53 url, playlist_id, 'Downloading playlist webpage')
54 matches = re.finditer(
55 r'<p\s+class="talk-title[^"]*"><a\s+href="(?P<talk_url>/talks/[^"]+\.html)">[^<]*</a></p>',
56 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 self.url_result(u'http://www.ted.com' + m.group('talk_url'), 'TED')
63 for m in matches
64 ]
65 return self.playlist_result(
66 playlist_entries, playlist_id=playlist_id, playlist_title=playlist_title)
67
68 def _talk_info(self, url, video_id=0):
69 """Return the video for the talk in the url"""
70 m = re.match(self._VALID_URL, url,re.VERBOSE)
71 video_name = m.group('name')
72 webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % video_name)
73 self.report_extraction(video_name)
74 # If the url includes the language we get the title translated
75 title = self._html_search_regex(r'<span .*?id="altHeadline".+?>(?P<title>.*)</span>',
76 webpage, 'title')
77 json_data = self._search_regex(r'<script.*?>var talkDetails = ({.*?})</script>',
78 webpage, 'json data')
79 info = json.loads(json_data)
80 desc = self._html_search_regex(r'<div class="talk-intro">.*?<p.*?>(.*?)</p>',
81 webpage, 'description', flags = re.DOTALL)
82
83 thumbnail = self._search_regex(r'</span>[\s.]*</div>[\s.]*<img src="(.*?)"',
84 webpage, 'thumbnail')
85 formats = [{
86 'ext': 'mp4',
87 'url': stream['file'],
88 'format': stream['id']
89 } for stream in info['htmlStreams']]
90
91 video_id = info['id']
92
93 # subtitles
94 video_subtitles = self.extract_subtitles(video_id, webpage)
95 if self._downloader.params.get('listsubtitles', False):
96 self._list_available_subtitles(video_id, webpage)
97 return
98
99 return {
100 'id': video_id,
101 'title': title,
102 'thumbnail': thumbnail,
103 'description': desc,
104 'subtitles': video_subtitles,
105 'formats': formats,
106 }
107
108 def _get_available_subtitles(self, video_id, webpage):
109 try:
110 options = self._search_regex(r'(?:<select name="subtitles_language_select" id="subtitles_language_select">)(.*?)(?:</select>)', webpage, 'subtitles_language_select', flags=re.DOTALL)
111 languages = re.findall(r'(?:<option value=")(\S+)"', options)
112 if languages:
113 sub_lang_list = {}
114 for l in languages:
115 url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
116 sub_lang_list[l] = url
117 return sub_lang_list
118 except RegexNotFoundError:
119 self._downloader.report_warning(u'video doesn\'t have subtitles')
120 return {}