]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/egghead.py
a4b2a12f684fabc660d4f12010223d7ba0dfb7c2
[yt-dlp.git] / yt_dlp / extractor / egghead.py
1 from .common import InfoExtractor
2 from ..compat import compat_str
3 from ..utils import (
4 determine_ext,
5 int_or_none,
6 try_get,
7 unified_timestamp,
8 url_or_none,
9 )
10
11
12 class EggheadBaseIE(InfoExtractor):
13 def _call_api(self, path, video_id, resource, fatal=True):
14 return self._download_json(
15 'https://app.egghead.io/api/v1/' + path,
16 video_id, 'Downloading %s JSON' % resource, fatal=fatal)
17
18
19 class EggheadCourseIE(EggheadBaseIE):
20 IE_DESC = 'egghead.io course'
21 IE_NAME = 'egghead:course'
22 _VALID_URL = r'https://(?:app\.)?egghead\.io/(?:course|playlist)s/(?P<id>[^/?#&]+)'
23 _TESTS = [{
24 'url': 'https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript',
25 'playlist_count': 29,
26 'info_dict': {
27 'id': '432655',
28 'title': 'Professor Frisby Introduces Composable Functional JavaScript',
29 'description': 're:(?s)^This course teaches the ubiquitous.*You\'ll start composing functionality before you know it.$',
30 },
31 }, {
32 'url': 'https://app.egghead.io/playlists/professor-frisby-introduces-composable-functional-javascript',
33 'only_matching': True,
34 }]
35
36 def _real_extract(self, url):
37 playlist_id = self._match_id(url)
38 series_path = 'series/' + playlist_id
39 lessons = self._call_api(
40 series_path + '/lessons', playlist_id, 'course lessons')
41
42 entries = []
43 for lesson in lessons:
44 lesson_url = url_or_none(lesson.get('http_url'))
45 if not lesson_url:
46 continue
47 lesson_id = lesson.get('id')
48 if lesson_id:
49 lesson_id = compat_str(lesson_id)
50 entries.append(self.url_result(
51 lesson_url, ie=EggheadLessonIE.ie_key(), video_id=lesson_id))
52
53 course = self._call_api(
54 series_path, playlist_id, 'course', False) or {}
55
56 playlist_id = course.get('id')
57 if playlist_id:
58 playlist_id = compat_str(playlist_id)
59
60 return self.playlist_result(
61 entries, playlist_id, course.get('title'),
62 course.get('description'))
63
64
65 class EggheadLessonIE(EggheadBaseIE):
66 IE_DESC = 'egghead.io lesson'
67 IE_NAME = 'egghead:lesson'
68 _VALID_URL = r'https://(?:app\.)?egghead\.io/(?:api/v1/)?lessons/(?P<id>[^/?#&]+)'
69 _TESTS = [{
70 'url': 'https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
71 'info_dict': {
72 'id': '1196',
73 'display_id': 'javascript-linear-data-flow-with-container-style-types-box',
74 'ext': 'mp4',
75 'title': 'Create linear data flow with container style types (Box)',
76 'description': 'md5:9aa2cdb6f9878ed4c39ec09e85a8150e',
77 'thumbnail': r're:^https?:.*\.jpg$',
78 'timestamp': 1481296768,
79 'upload_date': '20161209',
80 'duration': 304,
81 'view_count': 0,
82 'tags': 'count:2',
83 },
84 'params': {
85 'skip_download': True,
86 },
87 }, {
88 'url': 'https://egghead.io/api/v1/lessons/react-add-redux-to-a-react-application',
89 'only_matching': True,
90 }, {
91 'url': 'https://app.egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
92 'only_matching': True,
93 }]
94
95 def _real_extract(self, url):
96 display_id = self._match_id(url)
97
98 lesson = self._call_api(
99 'lessons/' + display_id, display_id, 'lesson')
100
101 lesson_id = compat_str(lesson['id'])
102 title = lesson['title']
103
104 formats = []
105 for _, format_url in lesson['media_urls'].items():
106 format_url = url_or_none(format_url)
107 if not format_url:
108 continue
109 ext = determine_ext(format_url)
110 if ext == 'm3u8':
111 formats.extend(self._extract_m3u8_formats(
112 format_url, lesson_id, 'mp4', m3u8_id='hls', fatal=False))
113 elif ext == 'mpd':
114 formats.extend(self._extract_mpd_formats(
115 format_url, lesson_id, mpd_id='dash', fatal=False))
116 else:
117 formats.append({
118 'url': format_url,
119 })
120
121 return {
122 'id': lesson_id,
123 'display_id': display_id,
124 'title': title,
125 'description': lesson.get('summary'),
126 'thumbnail': lesson.get('thumb_nail'),
127 'timestamp': unified_timestamp(lesson.get('published_at')),
128 'duration': int_or_none(lesson.get('duration')),
129 'view_count': int_or_none(lesson.get('plays_count')),
130 'tags': try_get(lesson, lambda x: x['tag_list'], list),
131 'series': try_get(
132 lesson, lambda x: x['series']['title'], compat_str),
133 'formats': formats,
134 }