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