]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/platzi.py
[extractor] Add `_perform_login` function (#2943)
[yt-dlp.git] / yt_dlp / extractor / platzi.py
CommitLineData
c701472f
S
1# coding: utf-8
2from __future__ import unicode_literals
3
4from .common import InfoExtractor
5from ..compat import (
6 compat_b64decode,
7 compat_str,
8)
9from ..utils import (
10 clean_html,
11 ExtractorError,
12 int_or_none,
13 str_or_none,
14 try_get,
15 url_or_none,
16 urlencode_postdata,
17 urljoin,
18)
19
20
66d04c74 21class PlatziBaseIE(InfoExtractor):
c701472f
S
22 _LOGIN_URL = 'https://platzi.com/login/'
23 _NETRC_MACHINE = 'platzi'
24
52efa4b3 25 def _perform_login(self, username, password):
c701472f
S
26 login_page = self._download_webpage(
27 self._LOGIN_URL, None, 'Downloading login page')
28
29 login_form = self._hidden_inputs(login_page)
30
31 login_form.update({
32 'email': username,
33 'password': password,
34 })
35
36 urlh = self._request_webpage(
37 self._LOGIN_URL, None, 'Logging in',
38 data=urlencode_postdata(login_form),
39 headers={'Referer': self._LOGIN_URL})
40
41 # login succeeded
7947a1f7 42 if 'platzi.com/login' not in urlh.geturl():
c701472f
S
43 return
44
45 login_error = self._webpage_read_content(
46 urlh, self._LOGIN_URL, None, 'Downloading login error page')
47
48 login = self._parse_json(
49 self._search_regex(
50 r'login\s*=\s*({.+?})(?:\s*;|\s*</script)', login_error, 'login'),
51 None)
52
53 for kind in ('error', 'password', 'nonFields'):
54 error = str_or_none(login.get('%sError' % kind))
55 if error:
56 raise ExtractorError(
57 'Unable to login: %s' % error, expected=True)
58 raise ExtractorError('Unable to log in')
59
66d04c74
S
60
61class PlatziIE(PlatziBaseIE):
62 _VALID_URL = r'''(?x)
63 https?://
64 (?:
65 platzi\.com/clases| # es version
66 courses\.platzi\.com/classes # en version
67 )/[^/]+/(?P<id>\d+)-[^/?\#&]+
68 '''
69
70 _TESTS = [{
71 'url': 'https://platzi.com/clases/1311-next-js/12074-creando-nuestra-primera-pagina/',
72 'md5': '8f56448241005b561c10f11a595b37e3',
73 'info_dict': {
74 'id': '12074',
75 'ext': 'mp4',
76 'title': 'Creando nuestra primera página',
77 'description': 'md5:4c866e45034fc76412fbf6e60ae008bc',
78 'duration': 420,
79 },
80 'skip': 'Requires platzi account credentials',
81 }, {
82 'url': 'https://courses.platzi.com/classes/1367-communication-codestream/13430-background/',
83 'info_dict': {
84 'id': '13430',
85 'ext': 'mp4',
86 'title': 'Background',
87 'description': 'md5:49c83c09404b15e6e71defaf87f6b305',
88 'duration': 360,
89 },
90 'skip': 'Requires platzi account credentials',
91 'params': {
92 'skip_download': True,
93 },
94 }]
95
c701472f
S
96 def _real_extract(self, url):
97 lecture_id = self._match_id(url)
98
99 webpage = self._download_webpage(url, lecture_id)
100
101 data = self._parse_json(
102 self._search_regex(
31dbd054
S
103 # client_data may contain "};" so that we have to try more
104 # strict regex first
105 (r'client_data\s*=\s*({.+?})\s*;\s*\n',
106 r'client_data\s*=\s*({.+?})\s*;'),
107 webpage, 'client data'),
c701472f
S
108 lecture_id)
109
110 material = data['initialState']['material']
111 desc = material['description']
112 title = desc['title']
113
114 formats = []
115 for server_id, server in material['videos'].items():
116 if not isinstance(server, dict):
117 continue
118 for format_id in ('hls', 'dash'):
119 format_url = url_or_none(server.get(format_id))
120 if not format_url:
121 continue
122 if format_id == 'hls':
123 formats.extend(self._extract_m3u8_formats(
124 format_url, lecture_id, 'mp4',
125 entry_protocol='m3u8_native', m3u8_id=format_id,
126 note='Downloading %s m3u8 information' % server_id,
127 fatal=False))
128 elif format_id == 'dash':
129 formats.extend(self._extract_mpd_formats(
130 format_url, lecture_id, mpd_id=format_id,
131 note='Downloading %s MPD manifest' % server_id,
132 fatal=False))
133 self._sort_formats(formats)
134
135 content = str_or_none(desc.get('content'))
136 description = (clean_html(compat_b64decode(content).decode('utf-8'))
137 if content else None)
138 duration = int_or_none(material.get('duration'), invscale=60)
139
140 return {
141 'id': lecture_id,
142 'title': title,
143 'description': description,
144 'duration': duration,
145 'formats': formats,
146 }
147
148
66d04c74 149class PlatziCourseIE(PlatziBaseIE):
c701472f
S
150 _VALID_URL = r'''(?x)
151 https?://
152 (?:
153 platzi\.com/clases| # es version
154 courses\.platzi\.com/classes # en version
155 )/(?P<id>[^/?\#&]+)
156 '''
157 _TESTS = [{
158 'url': 'https://platzi.com/clases/next-js/',
159 'info_dict': {
160 'id': '1311',
161 'title': 'Curso de Next.js',
162 },
163 'playlist_count': 22,
164 }, {
165 'url': 'https://courses.platzi.com/classes/communication-codestream/',
166 'info_dict': {
167 'id': '1367',
168 'title': 'Codestream Course',
169 },
170 'playlist_count': 14,
171 }]
172
173 @classmethod
174 def suitable(cls, url):
175 return False if PlatziIE.suitable(url) else super(PlatziCourseIE, cls).suitable(url)
176
177 def _real_extract(self, url):
178 course_name = self._match_id(url)
179
180 webpage = self._download_webpage(url, course_name)
181
182 props = self._parse_json(
183 self._search_regex(r'data\s*=\s*({.+?})\s*;', webpage, 'data'),
184 course_name)['initialProps']
185
186 entries = []
187 for chapter_num, chapter in enumerate(props['concepts'], 1):
188 if not isinstance(chapter, dict):
189 continue
190 materials = chapter.get('materials')
191 if not materials or not isinstance(materials, list):
192 continue
193 chapter_title = chapter.get('title')
194 chapter_id = str_or_none(chapter.get('id'))
195 for material in materials:
196 if not isinstance(material, dict):
197 continue
198 if material.get('material_type') != 'video':
199 continue
200 video_url = urljoin(url, material.get('url'))
201 if not video_url:
202 continue
203 entries.append({
204 '_type': 'url_transparent',
205 'url': video_url,
206 'title': str_or_none(material.get('name')),
207 'id': str_or_none(material.get('id')),
208 'ie_key': PlatziIE.ie_key(),
209 'chapter': chapter_title,
210 'chapter_number': chapter_num,
211 'chapter_id': chapter_id,
212 })
213
214 course_id = compat_str(try_get(props, lambda x: x['course']['id']))
215 course_title = try_get(props, lambda x: x['course']['name'], compat_str)
216
217 return self.playlist_result(entries, course_id, course_title)