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