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.

116 lines
3.9 KiB

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)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. return {
  81. 'id': video_id,
  82. 'title': talk_info['title'],
  83. 'uploader': talk_info['speaker'],
  84. 'thumbnail': talk_info['thumb'],
  85. 'description': self._og_search_description(webpage),
  86. 'subtitles': video_subtitles,
  87. 'formats': formats,
  88. }
  89. def _get_available_subtitles(self, video_id, talk_info):
  90. languages = [lang['languageCode'] for lang in talk_info.get('languages', [])]
  91. if languages:
  92. sub_lang_list = {}
  93. for l in languages:
  94. url = 'http://www.ted.com/talks/subtitles/id/%s/lang/%s/format/srt' % (video_id, l)
  95. sub_lang_list[l] = url
  96. return sub_lang_list
  97. else:
  98. self._downloader.report_warning(u'video doesn\'t have subtitles')
  99. return {}