]>
Commit | Line | Data |
---|---|---|
74dc1052 HADA |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
7 | ||
8 | from ..compat import ( | |
74dc1052 HADA |
9 | compat_urlparse, |
10 | ) | |
11 | ||
12 | from ..utils import ( | |
74dc1052 | 13 | urlencode_postdata, |
4d75c363 HADA |
14 | urljoin, |
15 | int_or_none, | |
16 | clean_html, | |
17 | ExtractorError | |
74dc1052 HADA |
18 | ) |
19 | ||
20 | ||
21 | class AluraIE(InfoExtractor): | |
22 | _VALID_URL = r'https?://(?:cursos\.)?alura\.com\.br/course/(?P<course_name>[^/]+)/task/(?P<id>\d+)' | |
23 | _LOGIN_URL = 'https://cursos.alura.com.br/loginForm?urlAfterLogin=/loginForm' | |
24 | _VIDEO_URL = 'https://cursos.alura.com.br/course/%s/task/%s/video' | |
4d75c363 HADA |
25 | _NETRC_MACHINE = 'alura' |
26 | _TESTS = [{ | |
27 | 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs/task/60095', | |
74dc1052 | 28 | 'info_dict': { |
4d75c363 | 29 | 'id': '60095', |
74dc1052 | 30 | 'ext': 'mp4', |
4d75c363 | 31 | 'title': 'ReferĂȘncias, ref-set e alter' |
4d75c363 | 32 | }, |
39672cdc | 33 | 'skip': 'Requires alura account credentials'}, |
4d75c363 HADA |
34 | { |
35 | # URL without video | |
36 | 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs/task/60098', | |
39672cdc | 37 | 'only_matching': True}, |
4d75c363 HADA |
38 | { |
39 | 'url': 'https://cursos.alura.com.br/course/fundamentos-market-digital/task/55219', | |
39672cdc | 40 | 'only_matching': True} |
4d75c363 | 41 | ] |
74dc1052 HADA |
42 | |
43 | def _real_extract(self, url): | |
44 | ||
5ad28e7f | 45 | course, video_id = self._match_valid_url(url) |
39672cdc | 46 | video_url = self._VIDEO_URL % (course, video_id) |
74dc1052 | 47 | |
c2baf165 | 48 | video_dict = self._download_json(video_url, video_id, 'Searching for videos') |
74dc1052 HADA |
49 | |
50 | if video_dict: | |
51 | webpage = self._download_webpage(url, video_id) | |
4d75c363 | 52 | video_title = clean_html(self._search_regex( |
74dc1052 | 53 | r'<span[^>]+class=(["\'])task-body-header-title-text\1[^>]*>(?P<title>[^<]+)', |
4d75c363 | 54 | webpage, 'title', group='title')) |
74dc1052 HADA |
55 | |
56 | formats = [] | |
57 | for video_obj in video_dict: | |
58 | video_url_m3u8 = video_obj.get('link') | |
59 | video_format = self._extract_m3u8_formats( | |
60 | video_url_m3u8, None, 'mp4', entry_protocol='m3u8_native', | |
61 | m3u8_id='hls', fatal=False) | |
4d75c363 HADA |
62 | for f in video_format: |
63 | m = re.search(r'^[\w \W]*-(?P<res>\w*).mp4[\W \w]*', f['url']) | |
64 | if m: | |
65 | if not f.get('height'): | |
66 | f['height'] = int('720' if m.group('res') == 'hd' else '480') | |
74dc1052 HADA |
67 | formats.extend(video_format) |
68 | ||
54f37eea | 69 | self._sort_formats(formats) |
4d75c363 | 70 | |
74dc1052 HADA |
71 | return { |
72 | 'id': video_id, | |
73 | 'title': video_title, | |
74 | "formats": formats | |
75 | } | |
76 | ||
52efa4b3 | 77 | def _perform_login(self, username, password): |
74dc1052 HADA |
78 | |
79 | login_page = self._download_webpage( | |
80 | self._LOGIN_URL, None, 'Downloading login popup') | |
81 | ||
82 | def is_logged(webpage): | |
83 | return any(re.search(p, webpage) for p in ( | |
84 | r'href=[\"|\']?/signout[\"|\']', | |
85 | r'>Logout<')) | |
86 | ||
87 | # already logged in | |
88 | if is_logged(login_page): | |
89 | return | |
90 | ||
91 | login_form = self._hidden_inputs(login_page) | |
92 | ||
93 | login_form.update({ | |
94 | 'username': username, | |
95 | 'password': password, | |
96 | }) | |
97 | ||
98 | post_url = self._search_regex( | |
99 | r'<form[^>]+class=["|\']signin-form["|\'] action=["|\'](?P<url>.+?)["|\']', login_page, | |
100 | 'post url', default=self._LOGIN_URL, group='url') | |
101 | ||
102 | if not post_url.startswith('http'): | |
103 | post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url) | |
104 | ||
4d75c363 | 105 | response = self._download_webpage( |
74dc1052 HADA |
106 | post_url, None, 'Logging in', |
107 | data=urlencode_postdata(login_form), | |
108 | headers={'Content-Type': 'application/x-www-form-urlencoded'}) | |
109 | ||
4d75c363 HADA |
110 | if not is_logged(response): |
111 | error = self._html_search_regex( | |
112 | r'(?s)<p[^>]+class="alert-message[^"]*">(.+?)</p>', | |
113 | response, 'error message', default=None) | |
114 | if error: | |
115 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
116 | raise ExtractorError('Unable to log in') | |
117 | ||
118 | ||
119 | class AluraCourseIE(AluraIE): | |
120 | ||
121 | _VALID_URL = r'https?://(?:cursos\.)?alura\.com\.br/course/(?P<id>[^/]+)' | |
122 | _LOGIN_URL = 'https://cursos.alura.com.br/loginForm?urlAfterLogin=/loginForm' | |
123 | _NETRC_MACHINE = 'aluracourse' | |
124 | _TESTS = [{ | |
125 | 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs', | |
126 | 'only_matching': True, | |
127 | }] | |
128 | ||
377be417 HADA |
129 | @classmethod |
130 | def suitable(cls, url): | |
131 | return False if AluraIE.suitable(url) else super(AluraCourseIE, cls).suitable(url) | |
132 | ||
4d75c363 | 133 | def _real_extract(self, url): |
74dc1052 | 134 | |
4d75c363 HADA |
135 | course_path = self._match_id(url) |
136 | webpage = self._download_webpage(url, course_path) | |
137 | ||
138 | course_title = self._search_regex( | |
139 | r'<h1.*?>(.*?)<strong>(?P<course_title>.*?)</strong></h[0-9]>', webpage, | |
140 | 'course title', default=course_path, group='course_title') | |
141 | ||
142 | entries = [] | |
143 | if webpage: | |
144 | for path in re.findall(r'<a\b(?=[^>]* class="[^"]*(?<=[" ])courseSectionList-section[" ])(?=[^>]* href="([^"]*))', webpage): | |
145 | page_url = urljoin(url, path) | |
146 | section_path = self._download_webpage(page_url, course_path) | |
147 | for path_video in re.findall(r'<a\b(?=[^>]* class="[^"]*(?<=[" ])task-menu-nav-item-link-VIDEO[" ])(?=[^>]* href="([^"]*))', section_path): | |
39672cdc HADA |
148 | chapter = clean_html( |
149 | self._search_regex( | |
150 | r'<h3[^>]+class=(["\'])task-menu-section-title-text\1[^>]*>(?P<chapter>[^<]+)', | |
151 | section_path, | |
152 | 'chapter', | |
153 | group='chapter')) | |
154 | ||
155 | chapter_number = int_or_none( | |
156 | self._search_regex( | |
157 | r'<span[^>]+class=(["\'])task-menu-section-title-number[^>]*>(.*?)<strong>(?P<chapter_number>[^<]+)</strong>', | |
158 | section_path, | |
159 | 'chapter number', | |
160 | group='chapter_number')) | |
4d75c363 | 161 | video_url = urljoin(url, path_video) |
39672cdc | 162 | |
4d75c363 HADA |
163 | entry = { |
164 | '_type': 'url_transparent', | |
165 | 'id': self._match_id(video_url), | |
166 | 'url': video_url, | |
167 | 'id_key': self.ie_key(), | |
168 | 'chapter': chapter, | |
169 | 'chapter_number': chapter_number | |
170 | } | |
171 | entries.append(entry) | |
39672cdc | 172 | return self.playlist_result(entries, course_path, course_title) |