]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/alura.py
Merge branch 'alura' of https://github.com/hugohaa/youtube-dl into hugohaa-alura
[yt-dlp.git] / youtube_dlc / extractor / alura.py
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 video_id = self._match_id(url)
46 course = self._search_regex(self._VALID_URL, url, 'post url', group='course_name')
47 video_url = self._VIDEO_URL % (course, video_id)
48
49 video_dict = self._download_json(video_url, video_id, 'Searching for videos')
50
51 if video_dict:
52 webpage = self._download_webpage(url, video_id)
53 video_title = clean_html(self._search_regex(
54 r'<span[^>]+class=(["\'])task-body-header-title-text\1[^>]*>(?P<title>[^<]+)',
55 webpage, 'title', group='title'))
56
57 formats = []
58 for video_obj in video_dict:
59 video_url_m3u8 = video_obj.get('link')
60 video_format = self._extract_m3u8_formats(
61 video_url_m3u8, None, 'mp4', entry_protocol='m3u8_native',
62 m3u8_id='hls', fatal=False)
63 for f in video_format:
64 m = re.search(r'^[\w \W]*-(?P<res>\w*).mp4[\W \w]*', f['url'])
65 if m:
66 if not f.get('height'):
67 f['height'] = int('720' if m.group('res') == 'hd' else '480')
68 formats.extend(video_format)
69
70 self._sort_formats(formats, field_preference=('height', 'width', 'tbr', 'format_id'))
71
72 return {
73 'id': video_id,
74 'title': video_title,
75 "formats": formats
76 }
77
78 def _real_initialize(self):
79 self._login()
80
81 def _login(self):
82 username, password = self._get_login_info()
83 if username is None:
84 return
85 pass
86
87 login_page = self._download_webpage(
88 self._LOGIN_URL, None, 'Downloading login popup')
89
90 def is_logged(webpage):
91 return any(re.search(p, webpage) for p in (
92 r'href=[\"|\']?/signout[\"|\']',
93 r'>Logout<'))
94
95 # already logged in
96 if is_logged(login_page):
97 return
98
99 login_form = self._hidden_inputs(login_page)
100
101 login_form.update({
102 'username': username,
103 'password': password,
104 })
105
106 post_url = self._search_regex(
107 r'<form[^>]+class=["|\']signin-form["|\'] action=["|\'](?P<url>.+?)["|\']', login_page,
108 'post url', default=self._LOGIN_URL, group='url')
109
110 if not post_url.startswith('http'):
111 post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
112
113 response = self._download_webpage(
114 post_url, None, 'Logging in',
115 data=urlencode_postdata(login_form),
116 headers={'Content-Type': 'application/x-www-form-urlencoded'})
117
118 if not is_logged(response):
119 error = self._html_search_regex(
120 r'(?s)<p[^>]+class="alert-message[^"]*">(.+?)</p>',
121 response, 'error message', default=None)
122 if error:
123 raise ExtractorError('Unable to login: %s' % error, expected=True)
124 raise ExtractorError('Unable to log in')
125
126
127 class AluraCourseIE(AluraIE):
128
129 _VALID_URL = r'https?://(?:cursos\.)?alura\.com\.br/course/(?P<id>[^/]+)'
130 _LOGIN_URL = 'https://cursos.alura.com.br/loginForm?urlAfterLogin=/loginForm'
131 _NETRC_MACHINE = 'aluracourse'
132 _TESTS = [{
133 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs',
134 'only_matching': True,
135 }]
136
137 @classmethod
138 def suitable(cls, url):
139 return False if AluraIE.suitable(url) else super(AluraCourseIE, cls).suitable(url)
140
141 def _real_extract(self, url):
142
143 course_path = self._match_id(url)
144 webpage = self._download_webpage(url, course_path)
145
146 course_title = self._search_regex(
147 r'<h1.*?>(.*?)<strong>(?P<course_title>.*?)</strong></h[0-9]>', webpage,
148 'course title', default=course_path, group='course_title')
149
150 entries = []
151 if webpage:
152 for path in re.findall(r'<a\b(?=[^>]* class="[^"]*(?<=[" ])courseSectionList-section[" ])(?=[^>]* href="([^"]*))', webpage):
153 page_url = urljoin(url, path)
154 section_path = self._download_webpage(page_url, course_path)
155 for path_video in re.findall(r'<a\b(?=[^>]* class="[^"]*(?<=[" ])task-menu-nav-item-link-VIDEO[" ])(?=[^>]* href="([^"]*))', section_path):
156 chapter = clean_html(
157 self._search_regex(
158 r'<h3[^>]+class=(["\'])task-menu-section-title-text\1[^>]*>(?P<chapter>[^<]+)',
159 section_path,
160 'chapter',
161 group='chapter'))
162
163 chapter_number = int_or_none(
164 self._search_regex(
165 r'<span[^>]+class=(["\'])task-menu-section-title-number[^>]*>(.*?)<strong>(?P<chapter_number>[^<]+)</strong>',
166 section_path,
167 'chapter number',
168 group='chapter_number'))
169 video_url = urljoin(url, path_video)
170
171 entry = {
172 '_type': 'url_transparent',
173 'id': self._match_id(video_url),
174 'url': video_url,
175 'id_key': self.ie_key(),
176 'chapter': chapter,
177 'chapter_number': chapter_number
178 }
179 entries.append(entry)
180 return self.playlist_result(entries, course_path, course_title)