2021-01-16 07:40:15 -05:00
|
|
|
import json
|
2014-01-07 03:35:34 -05:00
|
|
|
|
|
|
|
from .common import InfoExtractor
|
|
|
|
from ..utils import (
|
2021-01-16 07:40:15 -05:00
|
|
|
int_or_none,
|
2024-06-15 15:51:27 -04:00
|
|
|
make_archive_id,
|
2021-01-16 07:40:15 -05:00
|
|
|
parse_iso8601,
|
2024-06-15 15:51:27 -04:00
|
|
|
str_or_none,
|
|
|
|
traverse_obj,
|
|
|
|
url_or_none,
|
|
|
|
urljoin,
|
2014-01-07 03:35:34 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-16 07:40:15 -05:00
|
|
|
class KhanAcademyBaseIE(InfoExtractor):
|
|
|
|
_VALID_URL_TEMPL = r'https?://(?:www\.)?khanacademy\.org/(?P<id>(?:[^/]+/){%s}%s[^?#/&]+)'
|
2014-01-07 03:35:34 -05:00
|
|
|
|
2024-09-05 14:47:14 -04:00
|
|
|
_PUBLISHED_CONTENT_VERSION = 'dc34750f0572c80f5effe7134082fe351143c1e4'
|
2024-06-15 15:51:27 -04:00
|
|
|
|
2021-01-16 07:40:15 -05:00
|
|
|
def _parse_video(self, video):
|
|
|
|
return {
|
|
|
|
'_type': 'url_transparent',
|
|
|
|
'url': video['youtubeId'],
|
2024-06-15 15:51:27 -04:00
|
|
|
'id': video['youtubeId'],
|
2021-01-16 07:40:15 -05:00
|
|
|
'ie_key': 'Youtube',
|
2024-06-15 15:51:27 -04:00
|
|
|
**traverse_obj(video, {
|
|
|
|
'display_id': ('id', {str_or_none}),
|
|
|
|
'title': ('translatedTitle', {str}),
|
|
|
|
'thumbnail': ('thumbnailUrls', ..., 'url', {url_or_none}),
|
|
|
|
'duration': ('duration', {int_or_none}),
|
|
|
|
'description': ('description', {str}),
|
|
|
|
}, get_all=False),
|
2021-01-16 07:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
display_id = self._match_id(url)
|
2022-05-04 10:26:45 -04:00
|
|
|
content = self._download_json(
|
2024-06-15 15:51:27 -04:00
|
|
|
'https://www.khanacademy.org/api/internal/graphql/ContentForPath', display_id,
|
|
|
|
query={
|
2022-05-04 10:26:45 -04:00
|
|
|
'fastly_cacheable': 'persist_until_publish',
|
2024-06-15 15:51:27 -04:00
|
|
|
'pcv': self._PUBLISHED_CONTENT_VERSION,
|
2024-09-05 14:47:14 -04:00
|
|
|
'hash': '3712657851',
|
2021-01-16 07:40:15 -05:00
|
|
|
'variables': json.dumps({
|
|
|
|
'path': display_id,
|
2022-05-04 10:26:45 -04:00
|
|
|
'countryCode': 'US',
|
2024-06-15 15:51:27 -04:00
|
|
|
'kaLocale': 'en',
|
|
|
|
'clientPublishedContentVersion': self._PUBLISHED_CONTENT_VERSION,
|
2021-01-16 07:40:15 -05:00
|
|
|
}),
|
2024-06-15 15:51:27 -04:00
|
|
|
'lang': 'en',
|
|
|
|
})['data']['contentRoute']['listedPathData']
|
|
|
|
return self._parse_component_props(content, display_id)
|
2021-01-16 07:40:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
class KhanAcademyIE(KhanAcademyBaseIE):
|
|
|
|
IE_NAME = 'khanacademy'
|
|
|
|
_VALID_URL = KhanAcademyBaseIE._VALID_URL_TEMPL % ('4', 'v/')
|
|
|
|
_TEST = {
|
|
|
|
'url': 'https://www.khanacademy.org/computing/computer-science/cryptography/crypt/v/one-time-pad',
|
2024-06-15 15:51:27 -04:00
|
|
|
'md5': '1d5c2e70fa6aa29c38eca419f12515ce',
|
2014-01-07 03:35:34 -05:00
|
|
|
'info_dict': {
|
2021-01-16 07:40:15 -05:00
|
|
|
'id': 'FlIG3TvQCBQ',
|
|
|
|
'ext': 'mp4',
|
2014-01-07 03:35:34 -05:00
|
|
|
'title': 'The one-time pad',
|
|
|
|
'description': 'The perfect cipher',
|
2024-06-15 15:51:27 -04:00
|
|
|
'display_id': '716378217',
|
2014-01-07 03:35:34 -05:00
|
|
|
'duration': 176,
|
2024-06-15 15:51:27 -04:00
|
|
|
'uploader': 'Khan Academy',
|
|
|
|
'uploader_id': '@khanacademy',
|
|
|
|
'uploader_url': 'https://www.youtube.com/@khanacademy',
|
2014-01-07 03:35:34 -05:00
|
|
|
'upload_date': '20120411',
|
2021-01-16 07:40:15 -05:00
|
|
|
'timestamp': 1334170113,
|
|
|
|
'license': 'cc-by-nc-sa',
|
2024-06-15 15:51:27 -04:00
|
|
|
'live_status': 'not_live',
|
|
|
|
'channel': 'Khan Academy',
|
|
|
|
'channel_id': 'UC4a-Gbdw7vOaccHmFo40b9g',
|
|
|
|
'channel_url': 'https://www.youtube.com/channel/UC4a-Gbdw7vOaccHmFo40b9g',
|
|
|
|
'channel_is_verified': True,
|
|
|
|
'playable_in_embed': True,
|
|
|
|
'categories': ['Education'],
|
|
|
|
'creators': ['Brit Cruise'],
|
|
|
|
'tags': [],
|
|
|
|
'age_limit': 0,
|
|
|
|
'availability': 'public',
|
|
|
|
'comment_count': int,
|
|
|
|
'channel_follower_count': int,
|
|
|
|
'thumbnail': str,
|
|
|
|
'view_count': int,
|
|
|
|
'like_count': int,
|
|
|
|
'heatmap': list,
|
2015-01-05 06:28:35 -05:00
|
|
|
},
|
|
|
|
'add_ie': ['Youtube'],
|
2021-01-16 07:40:15 -05:00
|
|
|
}
|
|
|
|
|
2024-06-15 15:51:27 -04:00
|
|
|
def _parse_component_props(self, component_props, display_id):
|
|
|
|
video = component_props['content']
|
|
|
|
return {
|
|
|
|
**self._parse_video(video),
|
|
|
|
**traverse_obj(video, {
|
|
|
|
'creators': ('authorNames', ..., {str}),
|
|
|
|
'timestamp': ('dateAdded', {parse_iso8601}),
|
|
|
|
'license': ('kaUserLicense', {str}),
|
|
|
|
}),
|
|
|
|
}
|
2021-01-16 07:40:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
class KhanAcademyUnitIE(KhanAcademyBaseIE):
|
|
|
|
IE_NAME = 'khanacademy:unit'
|
2024-06-15 15:51:27 -04:00
|
|
|
_VALID_URL = (KhanAcademyBaseIE._VALID_URL_TEMPL % ('1,2', '')) + '/?(?:[?#&]|$)'
|
|
|
|
_TESTS = [{
|
2021-01-16 07:40:15 -05:00
|
|
|
'url': 'https://www.khanacademy.org/computing/computer-science/cryptography',
|
2014-08-27 18:58:24 -04:00
|
|
|
'info_dict': {
|
2024-06-15 15:51:27 -04:00
|
|
|
'id': 'x48c910b6',
|
2021-01-16 07:40:15 -05:00
|
|
|
'title': 'Cryptography',
|
2014-08-27 18:58:24 -04:00
|
|
|
'description': 'How have humans protected their secret messages through history? What has changed today?',
|
2024-06-15 15:51:27 -04:00
|
|
|
'display_id': 'computing/computer-science/cryptography',
|
|
|
|
'_old_archive_ids': ['khanacademyunit cryptography'],
|
2014-08-27 18:58:24 -04:00
|
|
|
},
|
2021-01-16 07:40:15 -05:00
|
|
|
'playlist_mincount': 31,
|
2024-06-15 15:51:27 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.khanacademy.org/computing/computer-science',
|
|
|
|
'info_dict': {
|
|
|
|
'id': 'x301707a0',
|
|
|
|
'title': 'Computer science theory',
|
|
|
|
'description': 'md5:4b472a4646e6cf6ec4ccb52c4062f8ba',
|
|
|
|
'display_id': 'computing/computer-science',
|
|
|
|
'_old_archive_ids': ['khanacademyunit computer-science'],
|
|
|
|
},
|
|
|
|
'playlist_mincount': 50,
|
|
|
|
}]
|
|
|
|
|
|
|
|
def _parse_component_props(self, component_props, display_id):
|
|
|
|
course = component_props['course']
|
|
|
|
selected_unit = traverse_obj(course, (
|
|
|
|
'unitChildren', lambda _, v: v['relativeUrl'] == f'/{display_id}', any)) or course
|
2014-01-07 03:35:34 -05:00
|
|
|
|
2024-06-15 15:51:27 -04:00
|
|
|
def build_entry(entry):
|
|
|
|
return self.url_result(urljoin(
|
|
|
|
'https://www.khanacademy.org', entry['canonicalUrl']),
|
|
|
|
KhanAcademyIE, title=entry.get('translatedTitle'))
|
2014-01-07 03:35:34 -05:00
|
|
|
|
2024-06-15 15:51:27 -04:00
|
|
|
entries = traverse_obj(selected_unit, (
|
|
|
|
(('unitChildren', ...), None), 'allOrderedChildren', ..., 'curatedChildren',
|
|
|
|
lambda _, v: v['contentKind'] == 'Video' and v['canonicalUrl'], {build_entry}))
|
2014-01-07 03:35:34 -05:00
|
|
|
|
2021-01-16 07:40:15 -05:00
|
|
|
return self.playlist_result(
|
2024-06-15 15:51:27 -04:00
|
|
|
entries,
|
|
|
|
display_id=display_id,
|
|
|
|
**traverse_obj(selected_unit, {
|
|
|
|
'id': ('id', {str}),
|
|
|
|
'title': ('translatedTitle', {str}),
|
|
|
|
'description': ('translatedDescription', {str}),
|
|
|
|
'_old_archive_ids': ('slug', {str}, {lambda x: [make_archive_id(self, x)] if x else None}),
|
|
|
|
}))
|