You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
7.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .subtitles import SubtitlesInfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. )
  8. class TEDIE(SubtitlesInfoExtractor):
  9. _VALID_URL = r'''(?x)
  10. (?P<proto>https?://)
  11. (?P<type>www|embed)(?P<urlmain>\.ted\.com/
  12. (
  13. (?P<type_playlist>playlists(?:/\d+)?) # We have a playlist
  14. |
  15. ((?P<type_talk>talks)) # We have a simple talk
  16. |
  17. (?P<type_watch>watch)/[^/]+/[^/]+
  18. )
  19. (/lang/(.*?))? # The url may contain the language
  20. /(?P<name>[\w-]+) # Here goes the name and then ".html"
  21. .*)$
  22. '''
  23. _TESTS = [{
  24. 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
  25. 'md5': 'fc94ac279feebbce69f21c0c6ee82810',
  26. 'info_dict': {
  27. 'id': '102',
  28. 'ext': 'mp4',
  29. 'title': 'The illusion of consciousness',
  30. 'description': ('Philosopher Dan Dennett makes a compelling '
  31. 'argument that not only don\'t we understand our own '
  32. 'consciousness, but that half the time our brains are '
  33. 'actively fooling us.'),
  34. 'uploader': 'Dan Dennett',
  35. 'width': 854,
  36. }
  37. }, {
  38. 'url': 'http://www.ted.com/watch/ted-institute/ted-bcg/vishal-sikka-the-beauty-and-power-of-algorithms',
  39. 'md5': '226f4fb9c62380d11b7995efa4c87994',
  40. 'info_dict': {
  41. 'id': 'vishal-sikka-the-beauty-and-power-of-algorithms',
  42. 'ext': 'mp4',
  43. 'title': 'Vishal Sikka: The beauty and power of algorithms',
  44. 'thumbnail': 're:^https?://.+\.jpg',
  45. 'description': 'Adaptive, intelligent, and consistent, algorithms are emerging as the ultimate app for everything from matching consumers to products to assessing medical diagnoses. Vishal Sikka shares his appreciation for the algorithm, charting both its inherent beauty and its growing power.',
  46. }
  47. }, {
  48. 'url': 'http://www.ted.com/talks/gabby_giffords_and_mark_kelly_be_passionate_be_courageous_be_your_best',
  49. 'info_dict': {
  50. 'id': '1972',
  51. 'ext': 'mp4',
  52. 'title': 'Be passionate. Be courageous. Be your best.',
  53. 'uploader': 'Gabby Giffords and Mark Kelly',
  54. 'description': 'md5:5174aed4d0f16021b704120360f72b92',
  55. },
  56. }, {
  57. 'url': 'http://www.ted.com/playlists/who_are_the_hackers',
  58. 'info_dict': {
  59. 'id': '10',
  60. 'title': 'Who are the hackers?',
  61. },
  62. 'playlist_mincount': 6,
  63. }]
  64. _NATIVE_FORMATS = {
  65. 'low': {'preference': 1, 'width': 320, 'height': 180},
  66. 'medium': {'preference': 2, 'width': 512, 'height': 288},
  67. 'high': {'preference': 3, 'width': 854, 'height': 480},
  68. }
  69. def _extract_info(self, webpage):
  70. info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
  71. webpage, 'info json')
  72. return json.loads(info_json)
  73. def _real_extract(self, url):
  74. m = re.match(self._VALID_URL, url, re.VERBOSE)
  75. if m.group('type') == 'embed':
  76. desktop_url = m.group('proto') + 'www' + m.group('urlmain')
  77. return self.url_result(desktop_url, 'TED')
  78. name = m.group('name')
  79. if m.group('type_talk'):
  80. return self._talk_info(url, name)
  81. elif m.group('type_watch'):
  82. return self._watch_info(url, name)
  83. else:
  84. return self._playlist_videos_info(url, name)
  85. def _playlist_videos_info(self, url, name):
  86. '''Returns the videos of the playlist'''
  87. webpage = self._download_webpage(url, name,
  88. 'Downloading playlist webpage')
  89. info = self._extract_info(webpage)
  90. playlist_info = info['playlist']
  91. playlist_entries = [
  92. self.url_result('http://www.ted.com/talks/' + talk['slug'], self.ie_key())
  93. for talk in info['talks']
  94. ]
  95. return self.playlist_result(
  96. playlist_entries,
  97. playlist_id=compat_str(playlist_info['id']),
  98. playlist_title=playlist_info['title'])
  99. def _talk_info(self, url, video_name):
  100. webpage = self._download_webpage(url, video_name)
  101. self.report_extraction(video_name)
  102. talk_info = self._extract_info(webpage)['talks'][0]
  103. formats = [{
  104. 'url': format_url,
  105. 'format_id': format_id,
  106. 'format': format_id,
  107. } for (format_id, format_url) in talk_info['nativeDownloads'].items() if format_url is not None]
  108. if formats:
  109. for f in formats:
  110. finfo = self._NATIVE_FORMATS.get(f['format_id'])
  111. if finfo:
  112. f.update(finfo)
  113. else:
  114. # Use rtmp downloads
  115. formats = [{
  116. 'format_id': f['name'],
  117. 'url': talk_info['streamer'],
  118. 'play_path': f['file'],
  119. 'ext': 'flv',
  120. 'width': f['width'],
  121. 'height': f['height'],
  122. 'tbr': f['bitrate'],
  123. } for f in talk_info['resources']['rtmp']]
  124. self._sort_formats(formats)
  125. video_id = compat_str(talk_info['id'])
  126. # subtitles
  127. video_subtitles = self.extract_subtitles(video_id, talk_info)
  128. if self._downloader.params.get('listsubtitles', False):
  129. self._list_available_subtitles(video_id, talk_info)
  130. return
  131. thumbnail = talk_info['thumb']
  132. if not thumbnail.startswith('http'):
  133. thumbnail = 'http://' + thumbnail
  134. return {
  135. 'id': video_id,
  136. 'title': talk_info['title'].strip(),
  137. 'uploader': talk_info['speaker'],
  138. 'thumbnail': thumbnail,
  139. 'description': self._og_search_description(webpage),
  140. 'subtitles': video_subtitles,
  141. 'formats': formats,
  142. }
  143. def _get_available_subtitles(self, video_id, talk_info):
  144. languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
  145. if languages:
  146. sub_lang_list = {}
  147. for l in languages:
  148. url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
  149. sub_lang_list[l] = url
  150. return sub_lang_list
  151. else:
  152. self._downloader.report_warning('video doesn\'t have subtitles')
  153. return {}
  154. def _watch_info(self, url, name):
  155. webpage = self._download_webpage(url, name)
  156. config_json = self._html_search_regex(
  157. r"data-config='([^']+)", webpage, 'config')
  158. config = json.loads(config_json)
  159. video_url = config['video']['url']
  160. thumbnail = config.get('image', {}).get('url')
  161. title = self._html_search_regex(
  162. r"(?s)<h1(?:\s+class='[^']+')?>(.+?)</h1>", webpage, 'title')
  163. description = self._html_search_regex(
  164. [
  165. r'(?s)<h4 class="[^"]+" id="h3--about-this-talk">.*?</h4>(.*?)</div>',
  166. r'(?s)<p><strong>About this talk:</strong>\s+(.*?)</p>',
  167. ],
  168. webpage, 'description', fatal=False)
  169. return {
  170. 'id': name,
  171. 'url': video_url,
  172. 'title': title,
  173. 'thumbnail': thumbnail,
  174. 'description': description,
  175. }