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.

119 lines
4.0 KiB

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)http://www\.ted\.com/
  10. (
  11. (?P<type_playlist>playlists(?:/\d+)?) # We have a playlist
  12. |
  13. ((?P<type_talk>talks)) # We have a simple talk
  14. )
  15. (/lang/(.*?))? # The url may contain the language
  16. /(?P<name>\w+) # Here goes the name and then ".html"
  17. '''
  18. _TEST = {
  19. 'url': 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
  20. 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
  21. 'info_dict': {
  22. 'id': '102',
  23. 'ext': 'mp4',
  24. 'title': 'The illusion of consciousness',
  25. 'description': ('Philosopher Dan Dennett makes a compelling '
  26. 'argument that not only don\'t we understand our own '
  27. 'consciousness, but that half the time our brains are '
  28. 'actively fooling us.'),
  29. 'uploader': 'Dan Dennett',
  30. }
  31. }
  32. _FORMATS_PREFERENCE = {
  33. 'low': 1,
  34. 'medium': 2,
  35. 'high': 3,
  36. }
  37. def _extract_info(self, webpage):
  38. info_json = self._search_regex(r'q\("\w+.init",({.+})\)</script>',
  39. webpage, 'info json')
  40. return json.loads(info_json)
  41. def _real_extract(self, url):
  42. m = re.match(self._VALID_URL, url, re.VERBOSE)
  43. name = m.group('name')
  44. if m.group('type_talk'):
  45. return self._talk_info(url, name)
  46. else:
  47. return self._playlist_videos_info(url, name)
  48. def _playlist_videos_info(self, url, name):
  49. '''Returns the videos of the playlist'''
  50. webpage = self._download_webpage(url, name,
  51. 'Downloading playlist webpage')
  52. info = self._extract_info(webpage)
  53. playlist_info = info['playlist']
  54. playlist_entries = [
  55. self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
  56. for talk in info['talks']
  57. ]
  58. return self.playlist_result(
  59. playlist_entries,
  60. playlist_id=compat_str(playlist_info['id']),
  61. playlist_title=playlist_info['title'])
  62. def _talk_info(self, url, video_name):
  63. webpage = self._download_webpage(url, video_name)
  64. self.report_extraction(video_name)
  65. talk_info = self._extract_info(webpage)['talks'][0]
  66. formats = [{
  67. 'ext': 'mp4',
  68. 'url': format_url,
  69. 'format_id': format_id,
  70. 'format': format_id,
  71. 'preference': self._FORMATS_PREFERENCE.get(format_id, -1),
  72. } for (format_id, format_url) in talk_info['nativeDownloads'].items()]
  73. self._sort_formats(formats)
  74. video_id = compat_str(talk_info['id'])
  75. # subtitles
  76. video_subtitles = self.extract_subtitles(video_id, talk_info)
  77. if self._downloader.params.get('listsubtitles', False):
  78. self._list_available_subtitles(video_id, talk_info)
  79. return
  80. thumbnail = talk_info['thumb']
  81. if not thumbnail.startswith('http'):
  82. thumbnail = 'http://' + thumbnail
  83. return {
  84. 'id': video_id,
  85. 'title': talk_info['title'],
  86. 'uploader': talk_info['speaker'],
  87. 'thumbnail': thumbnail,
  88. 'description': self._og_search_description(webpage),
  89. 'subtitles': video_subtitles,
  90. 'formats': formats,
  91. }
  92. def _get_available_subtitles(self, video_id, talk_info):
  93. languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
  94. if languages:
  95. sub_lang_list = {}
  96. for l in languages:
  97. url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
  98. sub_lang_list[l] = url
  99. return sub_lang_list
  100. else:
  101. self._downloader.report_warning(u'video doesn\'t have subtitles')
  102. return {}