]>
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 HADA |
31 | 'title': 'ReferĂȘncias, ref-set e alter' |
32 | }, | |
33 | 'skip': 'Requires alura account credentials', | |
34 | }, | |
35 | { | |
36 | # URL without video | |
37 | 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs/task/60098', | |
38 | 'only_matching': True, | |
39 | }, | |
40 | { | |
41 | 'url': 'https://cursos.alura.com.br/course/fundamentos-market-digital/task/55219', | |
42 | 'only_matching': True, | |
74dc1052 | 43 | } |
4d75c363 | 44 | ] |
74dc1052 HADA |
45 | |
46 | def _real_extract(self, url): | |
47 | ||
48 | video_id = self._match_id(url) | |
49 | course = self._search_regex(self._VALID_URL, url, 'post url', group='course_name') | |
50 | video_url = self._VIDEO_URL % (course,video_id) | |
51 | ||
52 | video_dict = self._download_json(video_url, None, 'Searching for videos', expected_status=[404,500]) | |
53 | ||
54 | if video_dict: | |
55 | webpage = self._download_webpage(url, video_id) | |
4d75c363 | 56 | video_title = clean_html(self._search_regex( |
74dc1052 | 57 | r'<span[^>]+class=(["\'])task-body-header-title-text\1[^>]*>(?P<title>[^<]+)', |
4d75c363 | 58 | webpage, 'title', group='title')) |
74dc1052 HADA |
59 | |
60 | formats = [] | |
61 | for video_obj in video_dict: | |
62 | video_url_m3u8 = video_obj.get('link') | |
63 | video_format = self._extract_m3u8_formats( | |
64 | video_url_m3u8, None, 'mp4', entry_protocol='m3u8_native', | |
65 | m3u8_id='hls', fatal=False) | |
4d75c363 HADA |
66 | for f in video_format: |
67 | m = re.search(r'^[\w \W]*-(?P<res>\w*).mp4[\W \w]*', f['url']) | |
68 | if m: | |
69 | if not f.get('height'): | |
70 | f['height'] = int('720' if m.group('res') == 'hd' else '480') | |
74dc1052 HADA |
71 | formats.extend(video_format) |
72 | ||
4d75c363 HADA |
73 | self._sort_formats(formats, field_preference=('height', 'width', 'tbr', 'format_id')) |
74 | ||
74dc1052 HADA |
75 | return { |
76 | 'id': video_id, | |
77 | 'title': video_title, | |
78 | "formats": formats | |
79 | } | |
80 | ||
74dc1052 HADA |
81 | def _real_initialize(self): |
82 | self._login() | |
83 | ||
84 | def _login(self): | |
85 | username, password = self._get_login_info() | |
86 | if username is None: | |
87 | return | |
88 | pass | |
89 | ||
90 | login_page = self._download_webpage( | |
91 | self._LOGIN_URL, None, 'Downloading login popup') | |
92 | ||
93 | def is_logged(webpage): | |
94 | return any(re.search(p, webpage) for p in ( | |
95 | r'href=[\"|\']?/signout[\"|\']', | |
96 | r'>Logout<')) | |
97 | ||
98 | # already logged in | |
99 | if is_logged(login_page): | |
100 | return | |
101 | ||
102 | login_form = self._hidden_inputs(login_page) | |
103 | ||
104 | login_form.update({ | |
105 | 'username': username, | |
106 | 'password': password, | |
107 | }) | |
108 | ||
109 | post_url = self._search_regex( | |
110 | r'<form[^>]+class=["|\']signin-form["|\'] action=["|\'](?P<url>.+?)["|\']', login_page, | |
111 | 'post url', default=self._LOGIN_URL, group='url') | |
112 | ||
113 | if not post_url.startswith('http'): | |
114 | post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url) | |
115 | ||
4d75c363 | 116 | response = self._download_webpage( |
74dc1052 HADA |
117 | post_url, None, 'Logging in', |
118 | data=urlencode_postdata(login_form), | |
119 | headers={'Content-Type': 'application/x-www-form-urlencoded'}) | |
120 | ||
4d75c363 HADA |
121 | if not is_logged(response): |
122 | error = self._html_search_regex( | |
123 | r'(?s)<p[^>]+class="alert-message[^"]*">(.+?)</p>', | |
124 | response, 'error message', default=None) | |
125 | if error: | |
126 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
127 | raise ExtractorError('Unable to log in') | |
128 | ||
129 | ||
130 | class AluraCourseIE(AluraIE): | |
131 | ||
132 | _VALID_URL = r'https?://(?:cursos\.)?alura\.com\.br/course/(?P<id>[^/]+)' | |
133 | _LOGIN_URL = 'https://cursos.alura.com.br/loginForm?urlAfterLogin=/loginForm' | |
134 | _NETRC_MACHINE = 'aluracourse' | |
135 | _TESTS = [{ | |
136 | 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs', | |
137 | 'only_matching': True, | |
138 | }] | |
139 | ||
140 | def _real_extract(self, url): | |
74dc1052 | 141 | |
4d75c363 HADA |
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(self._search_regex(r'<h3[^>]+class=(["\'])task-menu-section-title-text\1[^>]*>(?P<chapter>[^<]+)',section_path, 'chapter', group='chapter')) | |
156 | chapter_number = int_or_none(self._search_regex(r'<span[^>]+class=(["\'])task-menu-section-title-number[^>]*>(.*?)<strong>(?P<chapter_number>[^<]+)</strong>',section_path, 'chapter number', group='chapter_number')) | |
157 | video_url = urljoin(url, path_video) | |
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) | |
167 | return self.playlist_result(entries, course_path, course_title) |