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.

79 lines
3.6 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. @classmethod
  15. def suitable(cls, url):
  16. """Receives a URL and returns True if suitable for this IE."""
  17. return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
  18. def _real_extract(self, url):
  19. m=re.match(self._VALID_URL, url, re.VERBOSE)
  20. if m.group('type_talk'):
  21. return [self._talk_info(url)]
  22. else :
  23. playlist_id=m.group('playlist_id')
  24. name=m.group('name')
  25. self.to_screen(u'Getting info of playlist %s: "%s"' % (playlist_id,name))
  26. return [self._playlist_videos_info(url,name,playlist_id)]
  27. def _playlist_videos_info(self,url,name,playlist_id=0):
  28. '''Returns the videos of the playlist'''
  29. video_RE=r'''
  30. <li\ id="talk_(\d+)"([.\s]*?)data-id="(?P<video_id>\d+)"
  31. ([.\s]*?)data-playlist_item_id="(\d+)"
  32. ([.\s]*?)data-mediaslug="(?P<mediaSlug>.+?)"
  33. '''
  34. video_name_RE=r'<p\ class="talk-title"><a href="(?P<talk_url>/talks/(.+).html)">(?P<fullname>.+?)</a></p>'
  35. webpage=self._download_webpage(url, playlist_id, 'Downloading playlist webpage')
  36. m_videos=re.finditer(video_RE,webpage,re.VERBOSE)
  37. m_names=re.finditer(video_name_RE,webpage)
  38. playlist_title = self._html_search_regex(r'div class="headline">\s*?<h1>\s*?<span>(.*?)</span>',
  39. webpage, 'playlist title')
  40. playlist_entries = []
  41. for m_video, m_name in zip(m_videos,m_names):
  42. talk_url='http://www.ted.com%s' % m_name.group('talk_url')
  43. playlist_entries.append(self.url_result(talk_url, 'TED'))
  44. return self.playlist_result(playlist_entries, playlist_id = playlist_id, playlist_title = playlist_title)
  45. def _talk_info(self, url, video_id=0):
  46. """Return the video for the talk in the url"""
  47. m = re.match(self._VALID_URL, url,re.VERBOSE)
  48. video_name = m.group('name')
  49. webpage = self._download_webpage(url, video_id, 'Downloading \"%s\" page' % video_name)
  50. self.report_extraction(video_name)
  51. # If the url includes the language we get the title translated
  52. title = self._html_search_regex(r'<span id="altHeadline" >(?P<title>.*)</span>',
  53. webpage, 'title')
  54. json_data = self._search_regex(r'<script.*?>var talkDetails = ({.*?})</script>',
  55. webpage, 'json data')
  56. info = json.loads(json_data)
  57. desc = self._html_search_regex(r'<div class="talk-intro">.*?<p.*?>(.*?)</p>',
  58. webpage, 'description', flags = re.DOTALL)
  59. thumbnail = self._search_regex(r'</span>[\s.]*</div>[\s.]*<img src="(.*?)"',
  60. webpage, 'thumbnail')
  61. info = {
  62. 'id': info['id'],
  63. 'url': info['htmlStreams'][-1]['file'],
  64. 'ext': 'mp4',
  65. 'title': title,
  66. 'thumbnail': thumbnail,
  67. 'description': desc,
  68. }
  69. return info