]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ted.py
   1 from __future__ 
import unicode_literals
 
   6 from .subtitles 
import SubtitlesInfoExtractor
 
  13 class TEDIE(SubtitlesInfoExtractor
): 
  14     _VALID_URL
=r
'''http://www\.ted\.com/ 
  16                         ((?P<type_playlist>playlists)/(?P<playlist_id>\d+)) # We have a playlist 
  18                         ((?P<type_talk>talks)) # We have a simple talk 
  20                    (/lang/(.*?))? # The url may contain the language 
  21                    /(?P<name>\w+) # Here goes the name and then ".html" 
  24         'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html', 
  26         'md5': '4ea1dada91e4174b53dac2bb8ace429d', 
  28             "description": "md5:c6fa72e6eedbd938c9caf6b2702f5922", 
  29             "title": "Dan Dennett: The illusion of consciousness" 
  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 
  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
) 
  43             playlist_id
=m
.group('playlist_id') 
  45             self
.to_screen(u
'Getting info of playlist %s: "%s"' % (playlist_id
,name
)) 
  46             return [self
._playlist
_videos
_info
(url
,name
,playlist_id
)] 
  49     def _playlist_videos_info(self
, url
, name
, playlist_id
): 
  50         '''Returns the videos of the playlist''' 
  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>', 
  58         playlist_title 
= self
._html
_search
_regex
(r
'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>', 
  59                                                  webpage
, 'playlist title') 
  62             self
.url_result(u
'http://www.ted.com' + m
.group('talk_url'), 'TED') 
  65         return self
.playlist_result( 
  66             playlist_entries
, playlist_id
=playlist_id
, playlist_title
=playlist_title
) 
  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>', 
  77         json_data 
= self
._search
_regex
(r
'<script.*?>var talkDetails = ({.*?})</script>', 
  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
) 
  83         thumbnail 
= self
._search
_regex
(r
'</span>[\s.]*</div>[\s.]*<img src="(.*?)"', 
  87             'url': stream
['file'], 
  88             'format': stream
['id'] 
  89         } for stream 
in info
['htmlStreams']] 
  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
) 
 102             'thumbnail': thumbnail
, 
 104             'subtitles': video_subtitles
, 
 108     def _get_available_subtitles(self
, video_id
, webpage
): 
 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
) 
 115                     url 
= 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id
, l
) 
 116                     sub_lang_list
[l
] = url
 
 118         except RegexNotFoundError
: 
 119             self
._downloader
.report_warning(u
'video doesn\'t have subtitles')