2 from __future__ import unicode_literals
6 from .common import InfoExtractor
18 class FrontendMastersBaseIE(InfoExtractor):
19 _API_BASE = 'https://api.frontendmasters.com/v1/kabuki'
20 _LOGIN_URL = 'https://frontendmasters.com/login/'
22 _NETRC_MACHINE = 'frontendmasters'
25 'low': {'width': 480, 'height': 360},
26 'mid': {'width': 1280, 'height': 720},
27 'high': {'width': 1920, 'height': 1080}
30 def _real_initialize(self):
34 (username, password) = self._get_login_info()
38 login_page = self._download_webpage(
39 self._LOGIN_URL, None, 'Downloading login page')
41 login_form = self._hidden_inputs(login_page)
48 post_url = self._search_regex(
49 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
50 'post_url', default=self._LOGIN_URL, group='url')
52 if not post_url.startswith('http'):
53 post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
55 response = self._download_webpage(
56 post_url, None, 'Logging in', data=urlencode_postdata(login_form),
57 headers={'Content-Type': 'application/x-www-form-urlencoded'})
60 if any(p in response for p in (
61 'wp-login.php?action=logout', '>Logout')):
64 error = self._html_search_regex(
65 r'class=(["\'])(?:(?!\1).)*\bMessageAlert\b(?:(?!\1).)*\1[^>]*>(?P<error>[^<]+)<',
66 response, 'error message', default=None, group='error')
68 raise ExtractorError('Unable to login: %s' % error, expected=True)
69 raise ExtractorError('Unable to log in')
72 class FrontendMastersPageBaseIE(FrontendMastersBaseIE):
73 def _download_course(self, course_name, url):
74 return self._download_json(
75 '%s/courses/%s' % (self._API_BASE, course_name), course_name,
76 'Downloading course JSON', headers={'Referer': url})
79 def _extract_chapters(course):
81 lesson_elements = course.get('lessonElements')
82 if isinstance(lesson_elements, list):
83 chapters = [e for e in lesson_elements if isinstance(e, compat_str)]
87 def _extract_lesson(chapters, lesson_id, lesson):
88 title = lesson.get('title') or lesson_id
89 display_id = lesson.get('slug')
90 description = lesson.get('description')
91 thumbnail = lesson.get('thumbnail')
94 index = lesson.get('index')
95 element_index = lesson.get('elementIndex')
96 if (isinstance(index, int) and isinstance(element_index, int) and
97 index < element_index):
98 chapter_number = element_index - index
99 chapter = (chapters[chapter_number - 1]
100 if chapter_number - 1 < len(chapters) else None)
103 timestamp = lesson.get('timestamp')
104 if isinstance(timestamp, compat_str):
106 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})',
109 duration = parse_duration(mobj.group('end')) - parse_duration(
113 '_type': 'url_transparent',
114 'url': 'frontendmasters:%s' % lesson_id,
115 'ie_key': FrontendMastersIE.ie_key(),
117 'display_id': display_id,
119 'description': description,
120 'thumbnail': thumbnail,
121 'duration': duration,
123 'chapter_number': chapter_number,
127 class FrontendMastersIE(FrontendMastersBaseIE):
128 _VALID_URL = r'(?:frontendmasters:|https?://api\.frontendmasters\.com/v\d+/kabuki/video/)(?P<id>[^/]+)'
130 'url': 'https://api.frontendmasters.com/v1/kabuki/video/a2qogef6ba',
131 'md5': '7f161159710d6b7016a4f4af6fcb05e2',
135 'title': 'a2qogef6ba',
137 'skip': 'Requires FrontendMasters account credentials',
139 'url': 'frontendmasters:a2qogef6ba',
140 'only_matching': True,
143 def _real_extract(self, url):
144 lesson_id = self._match_id(url)
146 source_url = '%s/video/%s/source' % (self._API_BASE, lesson_id)
149 for ext in ('webm', 'mp4'):
150 for quality in ('low', 'mid', 'high'):
151 resolution = self._QUALITIES[quality].copy()
152 format_id = '%s-%s' % (ext, quality)
153 format_url = self._download_json(
154 source_url, lesson_id,
155 'Downloading %s source JSON' % format_id, query={
157 'r': resolution['height'],
160 }, fatal=False)['url']
165 f = resolution.copy()
169 'format_id': format_id,
172 self._sort_formats(formats)
176 'url': '%s/transcripts/%s.vtt' % (self._API_BASE, lesson_id),
184 'subtitles': subtitles
188 class FrontendMastersLessonIE(FrontendMastersPageBaseIE):
189 _VALID_URL = r'https?://(?:www\.)?frontendmasters\.com/courses/(?P<course_name>[^/]+)/(?P<lesson_name>[^/]+)'
191 'url': 'https://frontendmasters.com/courses/web-development/tools',
194 'display_id': 'tools',
197 'description': 'md5:82c1ea6472e88ed5acd1829fe992e4f7',
198 'thumbnail': r're:^https?://.*\.jpg$',
199 'chapter': 'Introduction',
203 'skip_download': True,
205 'skip': 'Requires FrontendMasters account credentials',
208 def _real_extract(self, url):
209 mobj = re.match(self._VALID_URL, url)
210 course_name, lesson_name = mobj.group('course_name', 'lesson_name')
212 course = self._download_course(course_name, url)
214 lesson_id, lesson = next(
216 for video_id, data in course['lessonData'].items()
217 if data.get('slug') == lesson_name)
219 chapters = self._extract_chapters(course)
220 return self._extract_lesson(chapters, lesson_id, lesson)
223 class FrontendMastersCourseIE(FrontendMastersPageBaseIE):
224 _VALID_URL = r'https?://(?:www\.)?frontendmasters\.com/courses/(?P<id>[^/]+)'
226 'url': 'https://frontendmasters.com/courses/web-development/',
228 'id': 'web-development',
229 'title': 'Introduction to Web Development',
230 'description': 'md5:9317e6e842098bf725d62360e52d49a6',
232 'playlist_count': 81,
233 'skip': 'Requires FrontendMasters account credentials',
237 def suitable(cls, url):
238 return False if FrontendMastersLessonIE.suitable(url) else super(
239 FrontendMastersBaseIE, cls).suitable(url)
241 def _real_extract(self, url):
242 course_name = self._match_id(url)
244 course = self._download_course(course_name, url)
246 chapters = self._extract_chapters(course)
249 course['lessonData'].values(), key=lambda data: data['index'])
252 for lesson in lessons:
253 lesson_name = lesson.get('slug')
256 lesson_id = lesson.get('hash') or lesson.get('statsId')
257 entries.append(self._extract_lesson(chapters, lesson_id, lesson))
259 title = course.get('title')
260 description = course.get('description')
262 return self.playlist_result(entries, course_name, title, description)