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.

124 lines
5.2 KiB

  1. import json
  2. import re
  3. from .subtitles import SubtitlesInfoExtractor
  4. from ..utils import (
  5. compat_str,
  6. RegexNotFoundError,
  7. )
  8. class TEDIE(SubtitlesInfoExtractor):
  9. _VALID_URL=r'''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. u'url': u'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html',
  20. u'file': u'102.mp4',
  21. u'md5': u'2d76ee1576672e0bd8f187513267adf6',
  22. u'info_dict': {
  23. u"description": u"md5:c6fa72e6eedbd938c9caf6b2702f5922",
  24. u"title": u"Dan Dennett: The illusion of consciousness"
  25. }
  26. }
  27. @classmethod
  28. def suitable(cls, url):
  29. """Receives a URL and returns True if suitable for this IE."""
  30. return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
  31. def _real_extract(self, url):
  32. m=re.match(self._VALID_URL, url, re.VERBOSE)
  33. if m.group('type_talk'):
  34. return self._talk_info(url)
  35. else :
  36. playlist_id=m.group('playlist_id')
  37. name=m.group('name')
  38. self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
  39. return [self._playlist_videos_info(url,name,playlist_id)]
  40. def _playlist_videos_info(self,url,name,playlist_id=0):
  41. '''Returns the videos of the playlist'''
  42. video_RE=r'''
  43. <li\ id="talk_(\d+)"([.\s]*?)data-id="(?P<video_id>\d+)"
  44. ([.\s]*?)data-playlist_item_id="(\d+)"
  45. ([.\s]*?)data-mediaslug="(?P<mediaSlug>.+?)"
  46. '''
  47. video_name_RE=r'<p\ class="talk-title"><a href="(?P<talk_url>/talks/(.+).html)">(?P<fullname>.+?)</a></p>'
  48. webpage=self._download_webpage(url, playlist_id, 'Downloading playlist webpage')
  49. m_videos=re.finditer(video_RE,webpage,re.VERBOSE)
  50. m_names=re.finditer(video_name_RE,webpage)
  51. playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>',
  52. webpage, 'playlist title')
  53. playlist_entries = []
  54. for m_video, m_name in zip(m_videos,m_names):
  55. talk_url='http://www.ted.com%s' % m_name.group('talk_url')
  56. playlist_entries.append(self.url_result(talk_url, 'TED'))
  57. return self.playlist_result(playlist_entries, playlist_id = playlist_id, playlist_title = playlist_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,re.VERBOSE)
  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. # If the url includes the language we get the title translated
  65. title = self._html_search_regex(r'<span .*?id="altHeadline".+?>(?P<title>.*)</span>',
  66. webpage, 'title')
  67. json_data = self._search_regex(r'<script.*?>var talkDetails = ({.*?})</script>',
  68. webpage, 'json data')
  69. info = json.loads(json_data)
  70. desc = self._html_search_regex(r'<div class="talk-intro">.*?<p.*?>(.*?)</p>',
  71. webpage, 'description', flags = re.DOTALL)
  72. thumbnail = self._search_regex(r'</span>[\s.]*</div>[\s.]*<img src="(.*?)"',
  73. webpage, 'thumbnail')
  74. formats = [{
  75. 'ext': 'mp4',
  76. 'url': stream['file'],
  77. 'format': stream['id']
  78. } for stream in info['htmlStreams']]
  79. video_id = info['id']
  80. # subtitles
  81. video_subtitles = self.extract_subtitles(video_id, webpage)
  82. if self._downloader.params.get('listsubtitles', False):
  83. self._list_available_subtitles(video_id, webpage)
  84. return
  85. info = {
  86. 'id': video_id,
  87. 'title': title,
  88. 'thumbnail': thumbnail,
  89. 'description': desc,
  90. 'subtitles': video_subtitles,
  91. 'formats': formats,
  92. }
  93. # TODO: Remove when #980 has been merged
  94. info.update(info['formats'][-1])
  95. return info
  96. def _get_available_subtitles(self, video_id, webpage):
  97. try:
  98. options = self._search_regex(r'(?:<select name="subtitles_language_select" id="subtitles_language_select">)(.*?)(?:</select>)', webpage, 'subtitles_language_select', flags=re.DOTALL)
  99. languages = re.findall(r'(?:<option value=")(\S+)"', options)
  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. except RegexNotFoundError as err:
  107. self._downloader.report_warning(u'video doesn\'t have subtitles')
  108. return {}