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.

88 lines
3.9 KiB

  1. import json
  2. import re
  3. from .common import InfoExtractor
  4. class TEDIE(InfoExtractor):
  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. info = {
  71. 'id': info['id'],
  72. 'url': info['htmlStreams'][-1]['file'],
  73. 'ext': 'mp4',
  74. 'title': title,
  75. 'thumbnail': thumbnail,
  76. 'description': desc,
  77. }
  78. return info