]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/udemy.py
Merge pull request #6303 from jaimeMF/dash_no_live
[yt-dlp.git] / youtube_dl / extractor / udemy.py
CommitLineData
e5de3f6c
S
1from __future__ import unicode_literals
2
3import re
4
5from .common import InfoExtractor
1cc79574 6from ..compat import (
e5de3f6c
S
7 compat_urllib_parse,
8 compat_urllib_request,
1cc79574
PH
9)
10from ..utils import (
e5de3f6c
S
11 ExtractorError,
12)
13
14
15class UdemyIE(InfoExtractor):
16 IE_NAME = 'udemy'
17 _VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
dcd4d95c
S
18 _LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
19 _ORIGIN_URL = 'https://www.udemy.com'
e5de3f6c
S
20 _NETRC_MACHINE = 'udemy'
21
6563837e 22 _TESTS = [{
e5de3f6c
S
23 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
24 'md5': '98eda5b657e752cf945d8445e261b5c5',
25 'info_dict': {
26 'id': '160614',
27 'ext': 'mp4',
28 'title': 'Introduction and Installation',
29 'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
30 'duration': 579.29,
31 },
32 'skip': 'Requires udemy account credentials',
6563837e 33 }]
e5de3f6c
S
34
35 def _handle_error(self, response):
36 if not isinstance(response, dict):
37 return
38 error = response.get('error')
39 if error:
40 error_str = 'Udemy returned error #%s: %s' % (error.get('code'), error.get('message'))
41 error_data = error.get('data')
42 if error_data:
43 error_str += ' - %s' % error_data.get('formErrors')
44 raise ExtractorError(error_str, expected=True)
45
61c0663c 46 def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
e2937118
S
47 headers = {
48 'X-Udemy-Snail-Case': 'true',
49 'X-Requested-With': 'XMLHttpRequest',
50 }
51 for cookie in self._downloader.cookiejar:
52 if cookie.name == 'client_id':
53 headers['X-Udemy-Client-Id'] = cookie.value
54 elif cookie.name == 'access_token':
55 headers['X-Udemy-Bearer-Token'] = cookie.value
61c0663c
S
56
57 if isinstance(url_or_request, compat_urllib_request.Request):
58 for header, value in headers.items():
59 url_or_request.add_header(header, value)
60 else:
61 url_or_request = compat_urllib_request.Request(url_or_request, headers=headers)
62
63 response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
64 self._handle_error(response)
65 return response
e2937118 66
e5de3f6c
S
67 def _real_initialize(self):
68 self._login()
69
70 def _login(self):
71 (username, password) = self._get_login_info()
72 if username is None:
73 raise ExtractorError(
74 'Udemy account is required, use --username and --password options to provide account credentials.',
75 expected=True)
76
77 login_popup = self._download_webpage(
dcd4d95c 78 self._LOGIN_URL, None, 'Downloading login popup')
e5de3f6c
S
79
80 if login_popup == '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
81 return
82
dcd4d95c 83 login_form = self._form_hidden_inputs('login-form', login_popup)
e5de3f6c 84
dcd4d95c 85 login_form.update({
dcd4d95c
S
86 'email': username.encode('utf-8'),
87 'password': password.encode('utf-8'),
88 })
89
e2937118 90 request = compat_urllib_request.Request(
61c0663c 91 self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
dcd4d95c
S
92 request.add_header('Referer', self._ORIGIN_URL)
93 request.add_header('Origin', self._ORIGIN_URL)
94
95 response = self._download_webpage(
e2937118 96 request, None, 'Logging in as %s' % username)
e5de3f6c 97
dcd4d95c
S
98 if all(logout_pattern not in response
99 for logout_pattern in ['href="https://www.udemy.com/user/logout/', '>Logout<']):
100 error = self._html_search_regex(
101 r'(?s)<div[^>]+class="form-errors[^"]*">(.+?)</div>',
102 response, 'error message', default=None)
103 if error:
104 raise ExtractorError('Unable to login: %s' % error, expected=True)
e5de3f6c
S
105 raise ExtractorError('Unable to log in')
106
107 def _real_extract(self, url):
810fb84d 108 lecture_id = self._match_id(url)
e5de3f6c 109
61c0663c 110 lecture = self._download_json(
e2937118
S
111 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id,
112 lecture_id, 'Downloading lecture JSON')
e5de3f6c 113
e2937118
S
114 asset_type = lecture.get('assetType') or lecture.get('asset_type')
115 if asset_type != 'Video':
116 raise ExtractorError(
117 'Lecture %s is not a video' % lecture_id, expected=True)
e5de3f6c
S
118
119 asset = lecture['asset']
120
e2937118 121 stream_url = asset.get('streamUrl') or asset.get('stream_url')
e5de3f6c
S
122 mobj = re.search(r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url)
123 if mobj:
124 return self.url_result(mobj.group(1), 'Youtube')
125
126 video_id = asset['id']
e2937118 127 thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
e5de3f6c
S
128 duration = asset['data']['duration']
129
e2937118
S
130 download_url = asset.get('downloadUrl') or asset.get('download_url')
131
132 video = download_url.get('Video') or download_url.get('video')
133 video_480p = download_url.get('Video480p') or download_url.get('video_480p')
e5de3f6c
S
134
135 formats = [
136 {
e2937118 137 'url': video_480p[0],
e5de3f6c
S
138 'format_id': '360p',
139 },
140 {
e2937118 141 'url': video[0],
e5de3f6c
S
142 'format_id': '720p',
143 },
144 ]
145
146 title = lecture['title']
147 description = lecture['description']
148
149 return {
150 'id': video_id,
151 'title': title,
152 'description': description,
153 'thumbnail': thumbnail,
154 'duration': duration,
155 'formats': formats
156 }
157
158
159class UdemyCourseIE(UdemyIE):
160 IE_NAME = 'udemy:course'
161 _VALID_URL = r'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
162 _SUCCESSFULLY_ENROLLED = '>You have enrolled in this course!<'
163 _ALREADY_ENROLLED = '>You are already taking this course.<'
6563837e 164 _TESTS = []
e5de3f6c
S
165
166 @classmethod
167 def suitable(cls, url):
168 return False if UdemyIE.suitable(url) else super(UdemyCourseIE, cls).suitable(url)
169
170 def _real_extract(self, url):
171 mobj = re.match(self._VALID_URL, url)
172 course_path = mobj.group('coursepath')
173
61c0663c 174 response = self._download_json(
e2937118
S
175 'https://www.udemy.com/api-1.1/courses/%s' % course_path,
176 course_path, 'Downloading course JSON')
e5de3f6c
S
177
178 course_id = int(response['id'])
179 course_title = response['title']
180
181 webpage = self._download_webpage(
e2937118
S
182 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id,
183 course_id, 'Enrolling in the course')
e5de3f6c
S
184
185 if self._SUCCESSFULLY_ENROLLED in webpage:
186 self.to_screen('%s: Successfully enrolled in' % course_id)
187 elif self._ALREADY_ENROLLED in webpage:
188 self.to_screen('%s: Already enrolled in' % course_id)
189
61c0663c 190 response = self._download_json(
e2937118
S
191 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
192 course_id, 'Downloading course curriculum')
e5de3f6c
S
193
194 entries = [
e2937118
S
195 self.url_result(
196 'https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']), 'Udemy')
197 for asset in response if asset.get('assetType') or asset.get('asset_type') == 'Video'
e5de3f6c
S
198 ]
199
5f6a1245 200 return self.playlist_result(entries, course_id, course_title)