ted.py 4.2 KB

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