2
0

ted.py 4.0 KB

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