]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/egghead.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / egghead.py
CommitLineData
8084951b 1from .common import InfoExtractor
514e8aef 2from ..compat import compat_str
dc6520aa 3from ..utils import (
514e8aef 4 determine_ext,
dc6520aa
S
5 int_or_none,
6 try_get,
7 unified_timestamp,
3052a30d 8 url_or_none,
dc6520aa 9)
8084951b
PH
10
11
2181983a 12class 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
19class EggheadCourseIE(EggheadBaseIE):
8084951b
PH
20 IE_DESC = 'egghead.io course'
21 IE_NAME = 'egghead:course'
a687226b 22 _VALID_URL = r'https?://(?:app\.)?egghead\.io/(?:course|playlist)s/(?P<id>[^/?#&]+)'
ed807c18 23 _TESTS = [{
8084951b
PH
24 'url': 'https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript',
25 'playlist_count': 29,
26 'info_dict': {
ed807c18 27 'id': '432655',
8084951b
PH
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 },
ed807c18 31 }, {
32 'url': 'https://app.egghead.io/playlists/professor-frisby-introduces-composable-functional-javascript',
33 'only_matching': True,
34 }]
8084951b
PH
35
36 def _real_extract(self, url):
37 playlist_id = self._match_id(url)
2181983a 38 series_path = 'series/' + playlist_id
39 lessons = self._call_api(
40 series_path + '/lessons', playlist_id, 'course lessons')
514e8aef
S
41
42 entries = []
43 for lesson in lessons:
3052a30d
S
44 lesson_url = url_or_none(lesson.get('http_url'))
45 if not lesson_url:
514e8aef
S
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
2181983a 53 course = self._call_api(
54 series_path, playlist_id, 'course', False) or {}
8084951b 55
514e8aef
S
56 playlist_id = course.get('id')
57 if playlist_id:
58 playlist_id = compat_str(playlist_id)
485cb375
S
59
60 return self.playlist_result(
61 entries, playlist_id, course.get('title'),
62 course.get('description'))
dc6520aa
S
63
64
2181983a 65class EggheadLessonIE(EggheadBaseIE):
dc6520aa
S
66 IE_DESC = 'egghead.io lesson'
67 IE_NAME = 'egghead:lesson'
a687226b 68 _VALID_URL = r'https?://(?:app\.)?egghead\.io/(?:api/v1/)?lessons/(?P<id>[^/?#&]+)'
514e8aef 69 _TESTS = [{
dc6520aa
S
70 'url': 'https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
71 'info_dict': {
514e8aef
S
72 'id': '1196',
73 'display_id': 'javascript-linear-data-flow-with-container-style-types-box',
dc6520aa
S
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,
2181983a 82 'tags': 'count:2',
dc6520aa
S
83 },
84 'params': {
85 'skip_download': True,
86 },
514e8aef
S
87 }, {
88 'url': 'https://egghead.io/api/v1/lessons/react-add-redux-to-a-react-application',
89 'only_matching': True,
ed807c18 90 }, {
91 'url': 'https://app.egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box',
92 'only_matching': True,
514e8aef 93 }]
dc6520aa
S
94
95 def _real_extract(self, url):
514e8aef 96 display_id = self._match_id(url)
dc6520aa 97
2181983a 98 lesson = self._call_api(
99 'lessons/' + display_id, display_id, 'lesson')
514e8aef
S
100
101 lesson_id = compat_str(lesson['id'])
102 title = lesson['title']
103
104 formats = []
105 for _, format_url in lesson['media_urls'].items():
3052a30d
S
106 format_url = url_or_none(format_url)
107 if not format_url:
514e8aef
S
108 continue
109 ext = determine_ext(format_url)
110 if ext == 'm3u8':
111 formats.extend(self._extract_m3u8_formats(
177877c5 112 format_url, lesson_id, 'mp4', m3u8_id='hls', fatal=False))
514e8aef
S
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 })
dc6520aa
S
120
121 return {
514e8aef
S
122 'id': lesson_id,
123 'display_id': display_id,
124 'title': title,
dc6520aa
S
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),
514e8aef
S
131 'series': try_get(
132 lesson, lambda x: x['series']['title'], compat_str),
133 'formats': formats,
dc6520aa 134 }