]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udemy.py
825172806ab62b2edc28c205a299809023297281
[youtubedl] / youtube_dl / extractor / udemy.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7 compat_urllib_parse,
8 compat_urllib_request,
9 )
10 from ..utils import (
11 ExtractorError,
12 sanitized_Request,
13 )
14
15
16 class UdemyIE(InfoExtractor):
17 IE_NAME = 'udemy'
18 _VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
19 _LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
20 _ORIGIN_URL = 'https://www.udemy.com'
21 _NETRC_MACHINE = 'udemy'
22
23 _TESTS = [{
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',
34 }]
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
47 def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
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
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:
62 url_or_request = sanitized_Request(url_or_request, headers=headers)
63
64 response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
65 self._handle_error(response)
66 return response
67
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:
74 self.raise_login_required('Udemy account is required')
75
76 login_popup = self._download_webpage(
77 self._LOGIN_URL, None, 'Downloading login popup')
78
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):
84 return
85
86 login_form = self._form_hidden_inputs('login-form', login_popup)
87
88 login_form.update({
89 'email': username.encode('utf-8'),
90 'password': password.encode('utf-8'),
91 })
92
93 request = sanitized_Request(
94 self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
95 request.add_header('Referer', self._ORIGIN_URL)
96 request.add_header('Origin', self._ORIGIN_URL)
97
98 response = self._download_webpage(
99 request, None, 'Logging in as %s' % username)
100
101 if not is_logged(response):
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)
107 raise ExtractorError('Unable to log in')
108
109 def _real_extract(self, url):
110 lecture_id = self._match_id(url)
111
112 lecture = self._download_json(
113 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id,
114 lecture_id, 'Downloading lecture JSON')
115
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)
120
121 asset = lecture['asset']
122
123 stream_url = asset.get('streamUrl') or asset.get('stream_url')
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']
129 thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
130 duration = asset['data']['duration']
131
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')
136
137 formats = [
138 {
139 'url': video_480p[0],
140 'format_id': '360p',
141 },
142 {
143 'url': video[0],
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
161 class 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.<'
166 _TESTS = []
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
176 response = self._download_json(
177 'https://www.udemy.com/api-1.1/courses/%s' % course_path,
178 course_path, 'Downloading course JSON')
179
180 course_id = int(response['id'])
181 course_title = response['title']
182
183 webpage = self._download_webpage(
184 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id,
185 course_id, 'Enrolling in the course')
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
192 response = self._download_json(
193 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
194 course_id, 'Downloading course curriculum')
195
196 entries = [
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'
200 ]
201
202 return self.playlist_result(entries, course_id, course_title)