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