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