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.

82 lines
2.7 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|api)\.)?khanacademy\.org/(?P<key>[^/]+)/(?:[^/]+/){,2}(?P<id>[^?#/]+)(?:$|[?#])'
  9. IE_NAME = 'KhanAcademy'
  10. _TESTS = [{
  11. 'url': 'http://www.khanacademy.org/video/one-time-pad',
  12. 'md5': '7021db7f2d47d4fff89b13177cb1e8f4',
  13. 'info_dict': {
  14. 'id': 'one-time-pad',
  15. 'ext': 'mp4',
  16. 'title': 'The one-time pad',
  17. 'description': 'The perfect cipher',
  18. 'duration': 176,
  19. 'uploader': 'Brit Cruise',
  20. 'uploader_id': 'khanacademy',
  21. 'upload_date': '20120411',
  22. },
  23. 'add_ie': ['Youtube'],
  24. }, {
  25. 'url': 'https://www.khanacademy.org/math/applied-math/cryptography',
  26. 'info_dict': {
  27. 'id': 'cryptography',
  28. 'title': 'Journey into cryptography',
  29. 'description': 'How have humans protected their secret messages through history? What has changed today?',
  30. },
  31. 'playlist_mincount': 3,
  32. }]
  33. def _real_extract(self, url):
  34. m = re.match(self._VALID_URL, url)
  35. video_id = m.group('id')
  36. if m.group('key') == 'video':
  37. data = self._download_json(
  38. 'http://api.khanacademy.org/api/v1/videos/' + video_id,
  39. video_id, 'Downloading video info')
  40. upload_date = unified_strdate(data['date_added'])
  41. uploader = ', '.join(data['author_names'])
  42. return {
  43. '_type': 'url_transparent',
  44. 'url': data['url'],
  45. 'id': video_id,
  46. 'title': data['title'],
  47. 'thumbnail': data['image_url'],
  48. 'duration': data['duration'],
  49. 'description': data['description'],
  50. 'uploader': uploader,
  51. 'upload_date': upload_date,
  52. }
  53. else:
  54. # topic
  55. data = self._download_json(
  56. 'http://api.khanacademy.org/api/v1/topic/' + video_id,
  57. video_id, 'Downloading topic info')
  58. entries = [
  59. {
  60. '_type': 'url',
  61. 'url': c['url'],
  62. 'id': c['id'],
  63. 'title': c['title'],
  64. }
  65. for c in data['children'] if c['kind'] in ('Video', 'Topic')]
  66. return {
  67. '_type': 'playlist',
  68. 'id': video_id,
  69. 'title': data['title'],
  70. 'description': data['description'],
  71. 'entries': entries,
  72. }