4 from .common
import InfoExtractor
13 class FrontendMastersBaseIE(InfoExtractor
):
14 _API_BASE
= 'https://api.frontendmasters.com/v1/kabuki'
15 _LOGIN_URL
= 'https://frontendmasters.com/login/'
17 _NETRC_MACHINE
= 'frontendmasters'
20 'low': {'width': 480, 'height': 360},
21 'mid': {'width': 1280, 'height': 720},
22 'high': {'width': 1920, 'height': 1080},
25 def _perform_login(self
, username
, password
):
26 login_page
= self
._download
_webpage
(
27 self
._LOGIN
_URL
, None, 'Downloading login page')
29 login_form
= self
._hidden
_inputs
(login_page
)
36 post_url
= self
._search
_regex
(
37 r
'<form[^>]+action=(["\'])(?P
<url
>.+?
)\
1', login_page,
38 'post_url
', default=self._LOGIN_URL, group='url
')
40 if not post_url.startswith('http
'):
41 post_url = urllib.parse.urljoin(self._LOGIN_URL, post_url)
43 response = self._download_webpage(
44 post_url, None, 'Logging
in', data=urlencode_postdata(login_form),
45 headers={'Content
-Type
': 'application
/x
-www
-form
-urlencoded
'})
48 if any(p in response for p in (
49 'wp
-login
.php?action
=logout
', '>Logout
')):
52 error = self._html_search_regex(
53 r'class=(["\'])(?:(?!\1).)*\bMessageAlert\b(?:(?!\1).)*\1[^>]*>(?P<error>[^<]+)<',
54 response, 'error message', default=None, group='error')
56 raise ExtractorError(f'Unable to login: {error}', expected=True)
57 raise ExtractorError('Unable to log in')
60 class FrontendMastersPageBaseIE(FrontendMastersBaseIE):
61 def _download_course(self, course_name, url):
62 return self._download_json(
63 f'{self._API_BASE}/courses/{course_name}', course_name,
64 'Downloading course JSON', headers={'Referer': url})
67 def _extract_chapters(course):
69 lesson_elements = course.get('lessonElements')
70 if isinstance(lesson_elements, list):
71 chapters = [url_or_none(e) for e in lesson_elements if url_or_none(e)]
75 def _extract_lesson(chapters, lesson_id, lesson):
76 title = lesson.get('title') or lesson_id
77 display_id = lesson.get('slug')
78 description = lesson.get('description')
79 thumbnail = lesson.get('thumbnail')
82 index = lesson.get('index')
83 element_index = lesson.get('elementIndex')
84 if (isinstance(index, int) and isinstance(element_index, int)
85 and index < element_index):
86 chapter_number = element_index - index
87 chapter = (chapters[chapter_number - 1]
88 if chapter_number - 1 < len(chapters) else None)
91 timestamp = lesson.get('timestamp')
92 if isinstance(timestamp, str):
94 r'(?P<start>\d{1,2}:\d{1,2}:\d{1,2})\s*-(?P<end>\s*\d{1,2}:\d{1,2}:\d{1,2})',
97 duration = parse_duration(mobj.group('end')) - parse_duration(
101 '_type': 'url_transparent',
102 'url': f'frontendmasters:{lesson_id}',
103 'ie_key': FrontendMastersIE.ie_key(),
105 'display_id': display_id,
107 'description': description,
108 'thumbnail': thumbnail,
109 'duration': duration,
111 'chapter_number': chapter_number,
115 class FrontendMastersIE(FrontendMastersBaseIE):
116 _VALID_URL = r'(?:frontendmasters:|https?://api\.frontendmasters\.com/v\d+/kabuki/video/)(?P<id>[^/]+)'
118 'url': 'https://api.frontendmasters.com/v1/kabuki/video/a2qogef6ba',
119 'md5': '7f161159710d6b7016a4f4af6fcb05e2',
123 'title': 'a2qogef6ba',
125 'skip': 'Requires FrontendMasters account credentials',
127 'url': 'frontendmasters:a2qogef6ba',
128 'only_matching': True,
131 def _real_extract(self, url):
132 lesson_id = self._match_id(url)
134 source_url = f'{self._API_BASE}/video/{lesson_id}/source'
137 for ext in ('webm', 'mp4'):
138 for quality in ('low', 'mid', 'high'):
139 resolution = self._QUALITIES[quality].copy()
140 format_id = f'{ext}-{quality}'
141 format_url = self._download_json(
142 source_url, lesson_id,
143 f'Downloading {format_id} source JSON', query={
145 'r': resolution['height'],
148 }, fatal=False)['url']
153 f = resolution.copy()
157 'format_id': format_id,
163 'url': f'{self._API_BASE}/transcripts/{lesson_id}.vtt',
171 'subtitles': subtitles,
175 class FrontendMastersLessonIE(FrontendMastersPageBaseIE):
176 _VALID_URL = r'https?://(?:www\.)?frontendmasters\.com/courses/(?P<course_name>[^/]+)/(?P<lesson_name>[^/]+)'
178 'url': 'https://frontendmasters.com/courses/web-development/tools',
181 'display_id': 'tools',
184 'description': 'md5:82c1ea6472e88ed5acd1829fe992e4f7',
185 'thumbnail': r're:^https?://.*\.jpg$',
186 'chapter': 'Introduction',
190 'skip_download': True,
192 'skip': 'Requires FrontendMasters account credentials',
195 def _real_extract(self, url):
196 mobj = self._match_valid_url(url)
197 course_name, lesson_name = mobj.group('course_name', 'lesson_name')
199 course = self._download_course(course_name, url)
201 lesson_id, lesson = next(
203 for video_id, data in course['lessonData'].items()
204 if data.get('slug') == lesson_name)
206 chapters = self._extract_chapters(course)
207 return self._extract_lesson(chapters, lesson_id, lesson)
210 class FrontendMastersCourseIE(FrontendMastersPageBaseIE):
211 _VALID_URL = r'https?://(?:www\.)?frontendmasters\.com/courses/(?P<id>[^/]+)'
213 'url': 'https://frontendmasters.com/courses/web-development/',
215 'id': 'web-development',
216 'title': 'Introduction to Web Development',
217 'description': 'md5:9317e6e842098bf725d62360e52d49a6',
219 'playlist_count': 81,
220 'skip': 'Requires FrontendMasters account credentials',
224 def suitable(cls, url):
225 return False if FrontendMastersLessonIE.suitable(url) else super(
226 FrontendMastersBaseIE, cls).suitable(url)
228 def _real_extract(self, url):
229 course_name = self._match_id(url)
231 course = self._download_course(course_name, url)
233 chapters = self._extract_chapters(course)
236 course['lessonData'].values(), key=lambda data: data['index'])
239 for lesson in lessons:
240 lesson_name = lesson.get('slug')
241 lesson_id = lesson.get('hash') or lesson.get('statsId')
242 if not lesson_id or not lesson_name:
244 entries.append(self._extract_lesson(chapters, lesson_id, lesson))
246 title = course.get('title')
247 description = course.get('description')
249 return self.playlist_result(entries, course_name, title, description)