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.

162 lines
5.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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': '4ea1dada91e4174b53dac2bb8ace429d',
  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. }
  36. }, {
  37. 'url': 'http://www.ted.com/watch/ted-institute/ted-bcg/vishal-sikka-the-beauty-and-power-of-algorithms',
  38. 'md5': '226f4fb9c62380d11b7995efa4c87994',
  39. 'info_dict': {
  40. 'id': 'vishal-sikka-the-beauty-and-power-of-algorithms',
  41. 'ext': 'mp4',
  42. 'title': 'Vishal Sikka: The beauty and power of algorithms',
  43. 'thumbnail': 're:^https?://.+\.jpg',
  44. '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.',
  45. }
  46. }]
  47. _FORMATS_PREFERENCE = {
  48. 'low': 1,
  49. 'medium': 2,
  50. 'high': 3,
  51. }
  52. def _extract_info(self, webpage):
  53. info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
  54. webpage, 'info json')
  55. return json.loads(info_json)
  56. def _real_extract(self, url):
  57. m = re.match(self._VALID_URL, url, re.VERBOSE)
  58. if m.group('type') == 'embed':
  59. desktop_url = m.group('proto') + 'www' + m.group('urlmain')
  60. return self.url_result(desktop_url, 'TED')
  61. name = m.group('name')
  62. if m.group('type_talk'):
  63. return self._talk_info(url, name)
  64. elif m.group('type_watch'):
  65. return self._watch_info(url, name)
  66. else:
  67. return self._playlist_videos_info(url, name)
  68. def _playlist_videos_info(self, url, name):
  69. '''Returns the videos of the playlist'''
  70. webpage = self._download_webpage(url, name,
  71. 'Downloading playlist webpage')
  72. info = self._extract_info(webpage)
  73. playlist_info = info['playlist']
  74. playlist_entries = [
  75. self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
  76. for talk in info['talks']
  77. ]
  78. return self.playlist_result(
  79. playlist_entries,
  80. playlist_id=compat_str(playlist_info['id']),
  81. playlist_title=playlist_info['title'])
  82. def _talk_info(self, url, video_name):
  83. webpage = self._download_webpage(url, video_name)
  84. self.report_extraction(video_name)
  85. talk_info = self._extract_info(webpage)['talks'][0]
  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. video_id = compat_str(talk_info['id'])
  95. # subtitles
  96. video_subtitles = self.extract_subtitles(video_id, talk_info)
  97. if self._downloader.params.get('listsubtitles', False):
  98. self._list_available_subtitles(video_id, talk_info)
  99. return
  100. thumbnail = talk_info['thumb']
  101. if not thumbnail.startswith('http'):
  102. thumbnail = 'http://' + thumbnail
  103. return {
  104. 'id': video_id,
  105. 'title': talk_info['title'],
  106. 'uploader': talk_info['speaker'],
  107. 'thumbnail': thumbnail,
  108. 'description': self._og_search_description(webpage),
  109. 'subtitles': video_subtitles,
  110. 'formats': formats,
  111. }
  112. def _get_available_subtitles(self, video_id, talk_info):
  113. languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
  114. if languages:
  115. sub_lang_list = {}
  116. for l in languages:
  117. url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
  118. sub_lang_list[l] = url
  119. return sub_lang_list
  120. else:
  121. self._downloader.report_warning(u'video doesn\'t have subtitles')
  122. return {}
  123. def _watch_info(self, url, name):
  124. webpage = self._download_webpage(url, name)
  125. config_json = self._html_search_regex(
  126. r"data-config='([^']+)", webpage, 'config')
  127. config = json.loads(config_json)
  128. video_url = config['video']['url']
  129. thumbnail = config.get('image', {}).get('url')
  130. title = self._html_search_regex(
  131. r"(?s)<h1(?:\s+class='[^']+')?>(.+?)</h1>", webpage, 'title')
  132. description = self._html_search_regex(
  133. r'(?s)<h4 class="[^"]+" id="h3--about-this-talk">.*?</h4>(.*?)</div>',
  134. webpage, 'description', fatal=False)
  135. return {
  136. 'id': name,
  137. 'url': video_url,
  138. 'title': title,
  139. 'thumbnail': thumbnail,
  140. 'description': description,
  141. }