ted.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .subtitles import SubtitlesInfoExtractor
  5. from ..utils import (
  6. RegexNotFoundError,
  7. )
  8. class TEDIE(SubtitlesInfoExtractor):
  9. _VALID_URL=r'''(?x)http://www\.ted\.com/
  10. (
  11. ((?P<type_playlist>playlists)/(?P<playlist_id>\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. 'file': '102.mp4',
  21. 'md5': '4ea1dada91e4174b53dac2bb8ace429d',
  22. 'info_dict': {
  23. 'title': 'The illusion of consciousness',
  24. '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.',
  25. 'uploader': 'Dan Dennett',
  26. }
  27. }
  28. _FORMATS_PREFERENCE = {
  29. 'low': 1,
  30. 'medium': 2,
  31. 'high': 3,
  32. }
  33. def _real_extract(self, url):
  34. m=re.match(self._VALID_URL, url, re.VERBOSE)
  35. if m.group('type_talk'):
  36. return self._talk_info(url)
  37. else :
  38. playlist_id=m.group('playlist_id')
  39. name=m.group('name')
  40. self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
  41. return [self._playlist_videos_info(url,name,playlist_id)]
  42. def _playlist_videos_info(self, url, name, playlist_id):
  43. '''Returns the videos of the playlist'''
  44. webpage = self._download_webpage(
  45. url, playlist_id, 'Downloading playlist webpage')
  46. matches = re.finditer(
  47. r'<p\s+class="talk-title[^"]*"><a\s+href="(?P<talk_url>/talks/[^"]+\.html)">[^<]*</a></p>',
  48. webpage)
  49. playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>',
  50. webpage, 'playlist title')
  51. playlist_entries = [
  52. self.url_result(u'http://www.ted.com' + m.group('talk_url'), 'TED')
  53. for m in matches
  54. ]
  55. return self.playlist_result(
  56. playlist_entries, playlist_id=playlist_id, playlist_title=playlist_title)
  57. def _talk_info(self, url, video_id=0):
  58. """Return the video for the talk in the url"""
  59. m = re.match(self._VALID_URL, url)
  60. video_name = m.group('name')
  61. webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % video_name)
  62. self.report_extraction(video_name)
  63. info_json = self._search_regex(r'"talkPage.init",({.+})\)</script>', webpage, 'info json')
  64. info = json.loads(info_json)
  65. talk_info = info['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 = 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 {}