ted.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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://(?P<type>www|embed)\.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. if m.group('type') == 'embed': # if the _VALID_URL is an embed
  44. desktop_url = re.sub("embed", "www", url)
  45. return self.url_result(desktop_url, 'TED') # pass the desktop version to the extractor
  46. name = m.group('name')
  47. if m.group('type_talk'):
  48. return self._talk_info(url, name)
  49. else:
  50. return self._playlist_videos_info(url, name)
  51. def _playlist_videos_info(self, url, name):
  52. '''Returns the videos of the playlist'''
  53. webpage = self._download_webpage(url, name,
  54. 'Downloading playlist webpage')
  55. info = self._extract_info(webpage)
  56. playlist_info = info['playlist']
  57. playlist_entries = [
  58. self.url_result(u'http://www.ted.com/talks/' + talk['slug'], self.ie_key())
  59. for talk in info['talks']
  60. ]
  61. return self.playlist_result(
  62. playlist_entries,
  63. playlist_id=compat_str(playlist_info['id']),
  64. playlist_title=playlist_info['title'])
  65. def _talk_info(self, url, video_name):
  66. webpage = self._download_webpage(url, video_name)
  67. self.report_extraction(video_name)
  68. talk_info = self._extract_info(webpage)['talks'][0]
  69. formats = [{
  70. 'ext': 'mp4',
  71. 'url': format_url,
  72. 'format_id': format_id,
  73. 'format': format_id,
  74. 'preference': self._FORMATS_PREFERENCE.get(format_id, -1),
  75. } for (format_id, format_url) in talk_info['nativeDownloads'].items()]
  76. self._sort_formats(formats)
  77. video_id = compat_str(talk_info['id'])
  78. # subtitles
  79. video_subtitles = self.extract_subtitles(video_id, talk_info)
  80. if self._downloader.params.get('listsubtitles', False):
  81. self._list_available_subtitles(video_id, talk_info)
  82. return
  83. thumbnail = talk_info['thumb']
  84. if not thumbnail.startswith('http'):
  85. thumbnail = 'http://' + thumbnail
  86. return {
  87. 'id': video_id,
  88. 'title': talk_info['title'],
  89. 'uploader': talk_info['speaker'],
  90. 'thumbnail': thumbnail,
  91. 'description': self._og_search_description(webpage),
  92. 'subtitles': video_subtitles,
  93. 'formats': formats,
  94. }
  95. def _get_available_subtitles(self, video_id, talk_info):
  96. languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
  97. if languages:
  98. sub_lang_list = {}
  99. for l in languages:
  100. url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
  101. sub_lang_list[l] = url
  102. return sub_lang_list
  103. else:
  104. self._downloader.report_warning(u'video doesn\'t have subtitles')
  105. return {}