logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

khanacademy.py (3919B)


  1. from __future__ import unicode_literals
  2. import json
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. parse_iso8601,
  7. try_get,
  8. )
  9. class KhanAcademyBaseIE(InfoExtractor):
  10. _VALID_URL_TEMPL = r'https?://(?:www\.)?khanacademy\.org/(?P<id>(?:[^/]+/){%s}%s[^?#/&]+)'
  11. def _parse_video(self, video):
  12. return {
  13. '_type': 'url_transparent',
  14. 'url': video['youtubeId'],
  15. 'id': video.get('slug'),
  16. 'title': video.get('title'),
  17. 'thumbnail': video.get('imageUrl') or video.get('thumbnailUrl'),
  18. 'duration': int_or_none(video.get('duration')),
  19. 'description': video.get('description'),
  20. 'ie_key': 'Youtube',
  21. }
  22. def _real_extract(self, url):
  23. display_id = self._match_id(url)
  24. component_props = self._parse_json(self._download_json(
  25. 'https://www.khanacademy.org/api/internal/graphql',
  26. display_id, query={
  27. 'hash': 1604303425,
  28. 'variables': json.dumps({
  29. 'path': display_id,
  30. 'queryParams': '',
  31. }),
  32. })['data']['contentJson'], display_id)['componentProps']
  33. return self._parse_component_props(component_props)
  34. class KhanAcademyIE(KhanAcademyBaseIE):
  35. IE_NAME = 'khanacademy'
  36. _VALID_URL = KhanAcademyBaseIE._VALID_URL_TEMPL % ('4', 'v/')
  37. _TEST = {
  38. 'url': 'https://www.khanacademy.org/computing/computer-science/cryptography/crypt/v/one-time-pad',
  39. 'md5': '9c84b7b06f9ebb80d22a5c8dedefb9a0',
  40. 'info_dict': {
  41. 'id': 'FlIG3TvQCBQ',
  42. 'ext': 'mp4',
  43. 'title': 'The one-time pad',
  44. 'description': 'The perfect cipher',
  45. 'duration': 176,
  46. 'uploader': 'Brit Cruise',
  47. 'uploader_id': 'khanacademy',
  48. 'upload_date': '20120411',
  49. 'timestamp': 1334170113,
  50. 'license': 'cc-by-nc-sa',
  51. },
  52. 'add_ie': ['Youtube'],
  53. }
  54. def _parse_component_props(self, component_props):
  55. video = component_props['tutorialPageData']['contentModel']
  56. info = self._parse_video(video)
  57. author_names = video.get('authorNames')
  58. info.update({
  59. 'uploader': ', '.join(author_names) if author_names else None,
  60. 'timestamp': parse_iso8601(video.get('dateAdded')),
  61. 'license': video.get('kaUserLicense'),
  62. })
  63. return info
  64. class KhanAcademyUnitIE(KhanAcademyBaseIE):
  65. IE_NAME = 'khanacademy:unit'
  66. _VALID_URL = (KhanAcademyBaseIE._VALID_URL_TEMPL % ('2', '')) + '/?(?:[?#&]|$)'
  67. _TEST = {
  68. 'url': 'https://www.khanacademy.org/computing/computer-science/cryptography',
  69. 'info_dict': {
  70. 'id': 'cryptography',
  71. 'title': 'Cryptography',
  72. 'description': 'How have humans protected their secret messages through history? What has changed today?',
  73. },
  74. 'playlist_mincount': 31,
  75. }
  76. def _parse_component_props(self, component_props):
  77. curation = component_props['curation']
  78. entries = []
  79. tutorials = try_get(curation, lambda x: x['tabs'][0]['modules'][0]['tutorials'], list) or []
  80. for tutorial_number, tutorial in enumerate(tutorials, 1):
  81. chapter_info = {
  82. 'chapter': tutorial.get('title'),
  83. 'chapter_number': tutorial_number,
  84. 'chapter_id': tutorial.get('id'),
  85. }
  86. for content_item in (tutorial.get('contentItems') or []):
  87. if content_item.get('kind') == 'Video':
  88. info = self._parse_video(content_item)
  89. info.update(chapter_info)
  90. entries.append(info)
  91. return self.playlist_result(
  92. entries, curation.get('unit'), curation.get('title'),
  93. curation.get('description'))