2 from __future__ import unicode_literals
4 from .common import InfoExtractor
21 class PlatziBaseIE(InfoExtractor):
22 _LOGIN_URL = 'https://platzi.com/login/'
23 _NETRC_MACHINE = 'platzi'
25 def _real_initialize(self):
29 username, password = self._get_login_info()
33 login_page = self._download_webpage(
34 self._LOGIN_URL, None, 'Downloading login page')
36 login_form = self._hidden_inputs(login_page)
43 urlh = self._request_webpage(
44 self._LOGIN_URL, None, 'Logging in',
45 data=urlencode_postdata(login_form),
46 headers={'Referer': self._LOGIN_URL})
49 if 'platzi.com/login' not in compat_str(urlh.geturl()):
52 login_error = self._webpage_read_content(
53 urlh, self._LOGIN_URL, None, 'Downloading login error page')
55 login = self._parse_json(
57 r'login\s*=\s*({.+?})(?:\s*;|\s*</script)', login_error, 'login'),
60 for kind in ('error', 'password', 'nonFields'):
61 error = str_or_none(login.get('%sError' % kind))
64 'Unable to login: %s' % error, expected=True)
65 raise ExtractorError('Unable to log in')
68 class PlatziIE(PlatziBaseIE):
72 platzi\.com/clases| # es version
73 courses\.platzi\.com/classes # en version
74 )/[^/]+/(?P<id>\d+)-[^/?\#&]+
78 'url': 'https://platzi.com/clases/1311-next-js/12074-creando-nuestra-primera-pagina/',
79 'md5': '8f56448241005b561c10f11a595b37e3',
83 'title': 'Creando nuestra primera página',
84 'description': 'md5:4c866e45034fc76412fbf6e60ae008bc',
87 'skip': 'Requires platzi account credentials',
89 'url': 'https://courses.platzi.com/classes/1367-communication-codestream/13430-background/',
93 'title': 'Background',
94 'description': 'md5:49c83c09404b15e6e71defaf87f6b305',
97 'skip': 'Requires platzi account credentials',
99 'skip_download': True,
103 def _real_extract(self, url):
104 lecture_id = self._match_id(url)
106 webpage = self._download_webpage(url, lecture_id)
108 data = self._parse_json(
110 r'client_data\s*=\s*({.+?})\s*;', webpage, 'client data'),
113 material = data['initialState']['material']
114 desc = material['description']
115 title = desc['title']
118 for server_id, server in material['videos'].items():
119 if not isinstance(server, dict):
121 for format_id in ('hls', 'dash'):
122 format_url = url_or_none(server.get(format_id))
125 if format_id == 'hls':
126 formats.extend(self._extract_m3u8_formats(
127 format_url, lecture_id, 'mp4',
128 entry_protocol='m3u8_native', m3u8_id=format_id,
129 note='Downloading %s m3u8 information' % server_id,
131 elif format_id == 'dash':
132 formats.extend(self._extract_mpd_formats(
133 format_url, lecture_id, mpd_id=format_id,
134 note='Downloading %s MPD manifest' % server_id,
136 self._sort_formats(formats)
138 content = str_or_none(desc.get('content'))
139 description = (clean_html(compat_b64decode(content).decode('utf-8'))
140 if content else None)
141 duration = int_or_none(material.get('duration'), invscale=60)
146 'description': description,
147 'duration': duration,
152 class PlatziCourseIE(PlatziBaseIE):
153 _VALID_URL = r'''(?x)
156 platzi\.com/clases| # es version
157 courses\.platzi\.com/classes # en version
161 'url': 'https://platzi.com/clases/next-js/',
164 'title': 'Curso de Next.js',
166 'playlist_count': 22,
168 'url': 'https://courses.platzi.com/classes/communication-codestream/',
171 'title': 'Codestream Course',
173 'playlist_count': 14,
177 def suitable(cls, url):
178 return False if PlatziIE.suitable(url) else super(PlatziCourseIE, cls).suitable(url)
180 def _real_extract(self, url):
181 course_name = self._match_id(url)
183 webpage = self._download_webpage(url, course_name)
185 props = self._parse_json(
186 self._search_regex(r'data\s*=\s*({.+?})\s*;', webpage, 'data'),
187 course_name)['initialProps']
190 for chapter_num, chapter in enumerate(props['concepts'], 1):
191 if not isinstance(chapter, dict):
193 materials = chapter.get('materials')
194 if not materials or not isinstance(materials, list):
196 chapter_title = chapter.get('title')
197 chapter_id = str_or_none(chapter.get('id'))
198 for material in materials:
199 if not isinstance(material, dict):
201 if material.get('material_type') != 'video':
203 video_url = urljoin(url, material.get('url'))
207 '_type': 'url_transparent',
209 'title': str_or_none(material.get('name')),
210 'id': str_or_none(material.get('id')),
211 'ie_key': PlatziIE.ie_key(),
212 'chapter': chapter_title,
213 'chapter_number': chapter_num,
214 'chapter_id': chapter_id,
217 course_id = compat_str(try_get(props, lambda x: x['course']['id']))
218 course_title = try_get(props, lambda x: x['course']['name'], compat_str)
220 return self.playlist_result(entries, course_id, course_title)