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