khanacademy.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import json
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. make_archive_id,
  6. parse_iso8601,
  7. str_or_none,
  8. traverse_obj,
  9. url_or_none,
  10. urljoin,
  11. )
  12. class KhanAcademyBaseIE(InfoExtractor):
  13. _VALID_URL_TEMPL = r'https?://(?:www\.)?khanacademy\.org/(?P<id>(?:[^/]+/){%s}%s[^?#/&]+)'
  14. _PUBLISHED_CONTENT_VERSION = '171419ab20465d931b356f22d20527f13969bb70'
  15. def _parse_video(self, video):
  16. return {
  17. '_type': 'url_transparent',
  18. 'url': video['youtubeId'],
  19. 'id': video['youtubeId'],
  20. 'ie_key': 'Youtube',
  21. **traverse_obj(video, {
  22. 'display_id': ('id', {str_or_none}),
  23. 'title': ('translatedTitle', {str}),
  24. 'thumbnail': ('thumbnailUrls', ..., 'url', {url_or_none}),
  25. 'duration': ('duration', {int_or_none}),
  26. 'description': ('description', {str}),
  27. }, get_all=False),
  28. }
  29. def _real_extract(self, url):
  30. display_id = self._match_id(url)
  31. content = self._download_json(
  32. 'https://www.khanacademy.org/api/internal/graphql/ContentForPath', display_id,
  33. query={
  34. 'fastly_cacheable': 'persist_until_publish',
  35. 'pcv': self._PUBLISHED_CONTENT_VERSION,
  36. 'hash': '1242644265',
  37. 'variables': json.dumps({
  38. 'path': display_id,
  39. 'countryCode': 'US',
  40. 'kaLocale': 'en',
  41. 'clientPublishedContentVersion': self._PUBLISHED_CONTENT_VERSION,
  42. }),
  43. 'lang': 'en',
  44. })['data']['contentRoute']['listedPathData']
  45. return self._parse_component_props(content, display_id)
  46. class KhanAcademyIE(KhanAcademyBaseIE):
  47. IE_NAME = 'khanacademy'
  48. _VALID_URL = KhanAcademyBaseIE._VALID_URL_TEMPL % ('4', 'v/')
  49. _TEST = {
  50. 'url': 'https://www.khanacademy.org/computing/computer-science/cryptography/crypt/v/one-time-pad',
  51. 'md5': '1d5c2e70fa6aa29c38eca419f12515ce',
  52. 'info_dict': {
  53. 'id': 'FlIG3TvQCBQ',
  54. 'ext': 'mp4',
  55. 'title': 'The one-time pad',
  56. 'description': 'The perfect cipher',
  57. 'display_id': '716378217',
  58. 'duration': 176,
  59. 'uploader': 'Khan Academy',
  60. 'uploader_id': '@khanacademy',
  61. 'uploader_url': 'https://www.youtube.com/@khanacademy',
  62. 'upload_date': '20120411',
  63. 'timestamp': 1334170113,
  64. 'license': 'cc-by-nc-sa',
  65. 'live_status': 'not_live',
  66. 'channel': 'Khan Academy',
  67. 'channel_id': 'UC4a-Gbdw7vOaccHmFo40b9g',
  68. 'channel_url': 'https://www.youtube.com/channel/UC4a-Gbdw7vOaccHmFo40b9g',
  69. 'channel_is_verified': True,
  70. 'playable_in_embed': True,
  71. 'categories': ['Education'],
  72. 'creators': ['Brit Cruise'],
  73. 'tags': [],
  74. 'age_limit': 0,
  75. 'availability': 'public',
  76. 'comment_count': int,
  77. 'channel_follower_count': int,
  78. 'thumbnail': str,
  79. 'view_count': int,
  80. 'like_count': int,
  81. 'heatmap': list,
  82. },
  83. 'add_ie': ['Youtube'],
  84. }
  85. def _parse_component_props(self, component_props, display_id):
  86. video = component_props['content']
  87. return {
  88. **self._parse_video(video),
  89. **traverse_obj(video, {
  90. 'creators': ('authorNames', ..., {str}),
  91. 'timestamp': ('dateAdded', {parse_iso8601}),
  92. 'license': ('kaUserLicense', {str}),
  93. }),
  94. }
  95. class KhanAcademyUnitIE(KhanAcademyBaseIE):
  96. IE_NAME = 'khanacademy:unit'
  97. _VALID_URL = (KhanAcademyBaseIE._VALID_URL_TEMPL % ('1,2', '')) + '/?(?:[?#&]|$)'
  98. _TESTS = [{
  99. 'url': 'https://www.khanacademy.org/computing/computer-science/cryptography',
  100. 'info_dict': {
  101. 'id': 'x48c910b6',
  102. 'title': 'Cryptography',
  103. 'description': 'How have humans protected their secret messages through history? What has changed today?',
  104. 'display_id': 'computing/computer-science/cryptography',
  105. '_old_archive_ids': ['khanacademyunit cryptography'],
  106. },
  107. 'playlist_mincount': 31,
  108. }, {
  109. 'url': 'https://www.khanacademy.org/computing/computer-science',
  110. 'info_dict': {
  111. 'id': 'x301707a0',
  112. 'title': 'Computer science theory',
  113. 'description': 'md5:4b472a4646e6cf6ec4ccb52c4062f8ba',
  114. 'display_id': 'computing/computer-science',
  115. '_old_archive_ids': ['khanacademyunit computer-science'],
  116. },
  117. 'playlist_mincount': 50,
  118. }]
  119. def _parse_component_props(self, component_props, display_id):
  120. course = component_props['course']
  121. selected_unit = traverse_obj(course, (
  122. 'unitChildren', lambda _, v: v['relativeUrl'] == f'/{display_id}', any)) or course
  123. def build_entry(entry):
  124. return self.url_result(urljoin(
  125. 'https://www.khanacademy.org', entry['canonicalUrl']),
  126. KhanAcademyIE, title=entry.get('translatedTitle'))
  127. entries = traverse_obj(selected_unit, (
  128. (('unitChildren', ...), None), 'allOrderedChildren', ..., 'curatedChildren',
  129. lambda _, v: v['contentKind'] == 'Video' and v['canonicalUrl'], {build_entry}))
  130. return self.playlist_result(
  131. entries,
  132. display_id=display_id,
  133. **traverse_obj(selected_unit, {
  134. 'id': ('id', {str}),
  135. 'title': ('translatedTitle', {str}),
  136. 'description': ('translatedDescription', {str}),
  137. '_old_archive_ids': ('slug', {str}, {lambda x: [make_archive_id(self, x)] if x else None}),
  138. }))