ted.py 5.0 KB

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