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.

71 lines
2.2 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. unified_strdate,
  6. )
  7. class KhanAcademyIE(InfoExtractor):
  8. _VALID_URL = r'^https?://(?:www\.)?khanacademy\.org/(?P<key>[^/]+)/(?:[^/]+/){,2}(?P<id>[^?#/]+)(?:$|[?#])'
  9. IE_NAME = 'KhanAcademy'
  10. _TEST = {
  11. 'url': 'http://www.khanacademy.org/video/one-time-pad',
  12. 'file': 'one-time-pad.mp4',
  13. 'md5': '7021db7f2d47d4fff89b13177cb1e8f4',
  14. 'info_dict': {
  15. 'title': 'The one-time pad',
  16. 'description': 'The perfect cipher',
  17. 'duration': 176,
  18. 'uploader': 'Brit Cruise',
  19. 'upload_date': '20120411',
  20. }
  21. }
  22. def _real_extract(self, url):
  23. m = re.match(self._VALID_URL, url)
  24. video_id = m.group('id')
  25. if m.group('key') == 'video':
  26. data = self._download_json(
  27. 'http://api.khanacademy.org/api/v1/videos/' + video_id,
  28. video_id, 'Downloading video info')
  29. upload_date = unified_strdate(data['date_added'])
  30. uploader = ', '.join(data['author_names'])
  31. return {
  32. '_type': 'url_transparent',
  33. 'url': data['url'],
  34. 'id': video_id,
  35. 'title': data['title'],
  36. 'thumbnail': data['image_url'],
  37. 'duration': data['duration'],
  38. 'description': data['description'],
  39. 'uploader': uploader,
  40. 'upload_date': upload_date,
  41. }
  42. else:
  43. # topic
  44. data = self._download_json(
  45. 'http://api.khanacademy.org/api/v1/topic/' + video_id,
  46. video_id, 'Downloading topic info')
  47. entries = [
  48. {
  49. '_type': 'url',
  50. 'url': c['url'],
  51. 'id': c['id'],
  52. 'title': c['title'],
  53. }
  54. for c in data['children'] if c['kind'] in ('Video', 'Topic')]
  55. return {
  56. '_type': 'playlist',
  57. 'id': video_id,
  58. 'title': data['title'],
  59. 'description': data['description'],
  60. 'entries': entries,
  61. }