| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2014-12-13 12:24:42 +01:00
										 |  |  | from ..compat import ( | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |     compat_urllib_parse, | 
					
						
							|  |  |  |     compat_urllib_request, | 
					
						
							| 
									
										
										
										
											2014-12-13 12:24:42 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | from ..utils import ( | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |     ExtractorError, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UdemyIE(InfoExtractor): | 
					
						
							|  |  |  |     IE_NAME = 'udemy' | 
					
						
							|  |  |  |     _VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)' | 
					
						
							| 
									
										
										
										
											2015-07-14 22:39:41 +06:00
										 |  |  |     _LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1' | 
					
						
							|  |  |  |     _ORIGIN_URL = 'https://www.udemy.com' | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |     _NETRC_MACHINE = 'udemy' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-16 07:09:10 +01:00
										 |  |  |     _TESTS = [{ | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |         'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757', | 
					
						
							|  |  |  |         'md5': '98eda5b657e752cf945d8445e261b5c5', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '160614', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'Introduction and Installation', | 
					
						
							|  |  |  |             'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876', | 
					
						
							|  |  |  |             'duration': 579.29, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'skip': 'Requires udemy account credentials', | 
					
						
							| 
									
										
										
										
											2014-03-16 07:09:10 +01:00
										 |  |  |     }] | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _handle_error(self, response): | 
					
						
							|  |  |  |         if not isinstance(response, dict): | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         error = response.get('error') | 
					
						
							|  |  |  |         if error: | 
					
						
							|  |  |  |             error_str = 'Udemy returned error #%s: %s' % (error.get('code'), error.get('message')) | 
					
						
							|  |  |  |             error_data = error.get('data') | 
					
						
							|  |  |  |             if error_data: | 
					
						
							|  |  |  |                 error_str += ' - %s' % error_data.get('formErrors') | 
					
						
							|  |  |  |             raise ExtractorError(error_str, expected=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:25:43 +06:00
										 |  |  |     def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'): | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |         headers = { | 
					
						
							|  |  |  |             'X-Udemy-Snail-Case': 'true', | 
					
						
							|  |  |  |             'X-Requested-With': 'XMLHttpRequest', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for cookie in self._downloader.cookiejar: | 
					
						
							|  |  |  |             if cookie.name == 'client_id': | 
					
						
							|  |  |  |                 headers['X-Udemy-Client-Id'] = cookie.value | 
					
						
							|  |  |  |             elif cookie.name == 'access_token': | 
					
						
							|  |  |  |                 headers['X-Udemy-Bearer-Token'] = cookie.value | 
					
						
							| 
									
										
										
										
											2014-11-26 21:25:43 +06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if isinstance(url_or_request, compat_urllib_request.Request): | 
					
						
							|  |  |  |             for header, value in headers.items(): | 
					
						
							|  |  |  |                 url_or_request.add_header(header, value) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             url_or_request = compat_urllib_request.Request(url_or_request, headers=headers) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         response = super(UdemyIE, self)._download_json(url_or_request, video_id, note) | 
					
						
							|  |  |  |         self._handle_error(response) | 
					
						
							|  |  |  |         return response | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |     def _real_initialize(self): | 
					
						
							|  |  |  |         self._login() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _login(self): | 
					
						
							|  |  |  |         (username, password) = self._get_login_info() | 
					
						
							|  |  |  |         if username is None: | 
					
						
							|  |  |  |             raise ExtractorError( | 
					
						
							|  |  |  |                 'Udemy account is required, use --username and --password options to provide account credentials.', | 
					
						
							|  |  |  |                 expected=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         login_popup = self._download_webpage( | 
					
						
							| 
									
										
										
										
											2015-07-14 22:39:41 +06:00
										 |  |  |             self._LOGIN_URL, None, 'Downloading login popup') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if login_popup == '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>': | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 22:39:41 +06:00
										 |  |  |         login_form = self._form_hidden_inputs('login-form', login_popup) | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 22:39:41 +06:00
										 |  |  |         login_form.update({ | 
					
						
							|  |  |  |             'email': username.encode('utf-8'), | 
					
						
							|  |  |  |             'password': password.encode('utf-8'), | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |         request = compat_urllib_request.Request( | 
					
						
							| 
									
										
										
										
											2014-11-26 21:25:43 +06:00
										 |  |  |             self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8')) | 
					
						
							| 
									
										
										
										
											2015-07-14 22:39:41 +06:00
										 |  |  |         request.add_header('Referer', self._ORIGIN_URL) | 
					
						
							|  |  |  |         request.add_header('Origin', self._ORIGIN_URL) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         response = self._download_webpage( | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |             request, None, 'Logging in as %s' % username) | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 22:39:41 +06:00
										 |  |  |         if all(logout_pattern not in response | 
					
						
							|  |  |  |                for logout_pattern in ['href="https://www.udemy.com/user/logout/', '>Logout<']): | 
					
						
							|  |  |  |             error = self._html_search_regex( | 
					
						
							|  |  |  |                 r'(?s)<div[^>]+class="form-errors[^"]*">(.+?)</div>', | 
					
						
							|  |  |  |                 response, 'error message', default=None) | 
					
						
							|  |  |  |             if error: | 
					
						
							|  |  |  |                 raise ExtractorError('Unable to login: %s' % error, expected=True) | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |             raise ExtractorError('Unable to log in') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							| 
									
										
										
										
											2014-12-04 08:27:40 +01:00
										 |  |  |         lecture_id = self._match_id(url) | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:25:43 +06:00
										 |  |  |         lecture = self._download_json( | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |             'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id, | 
					
						
							|  |  |  |             lecture_id, 'Downloading lecture JSON') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |         asset_type = lecture.get('assetType') or lecture.get('asset_type') | 
					
						
							|  |  |  |         if asset_type != 'Video': | 
					
						
							|  |  |  |             raise ExtractorError( | 
					
						
							|  |  |  |                 'Lecture %s is not a video' % lecture_id, expected=True) | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         asset = lecture['asset'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |         stream_url = asset.get('streamUrl') or asset.get('stream_url') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |         mobj = re.search(r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url) | 
					
						
							|  |  |  |         if mobj: | 
					
						
							|  |  |  |             return self.url_result(mobj.group(1), 'Youtube') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         video_id = asset['id'] | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |         thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |         duration = asset['data']['duration'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |         download_url = asset.get('downloadUrl') or asset.get('download_url') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         video = download_url.get('Video') or download_url.get('video') | 
					
						
							|  |  |  |         video_480p = download_url.get('Video480p') or download_url.get('video_480p') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         formats = [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |                 'url': video_480p[0], | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |                 'format_id': '360p', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |                 'url': video[0], | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |                 'format_id': '720p', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title = lecture['title'] | 
					
						
							|  |  |  |         description = lecture['description'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'title': title, | 
					
						
							|  |  |  |             'description': description, | 
					
						
							|  |  |  |             'thumbnail': thumbnail, | 
					
						
							|  |  |  |             'duration': duration, | 
					
						
							|  |  |  |             'formats': formats | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UdemyCourseIE(UdemyIE): | 
					
						
							|  |  |  |     IE_NAME = 'udemy:course' | 
					
						
							|  |  |  |     _VALID_URL = r'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)' | 
					
						
							|  |  |  |     _SUCCESSFULLY_ENROLLED = '>You have enrolled in this course!<' | 
					
						
							|  |  |  |     _ALREADY_ENROLLED = '>You are already taking this course.<' | 
					
						
							| 
									
										
										
										
											2014-03-16 07:09:10 +01:00
										 |  |  |     _TESTS = [] | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def suitable(cls, url): | 
					
						
							|  |  |  |         return False if UdemyIE.suitable(url) else super(UdemyCourseIE, cls).suitable(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |         course_path = mobj.group('coursepath') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:25:43 +06:00
										 |  |  |         response = self._download_json( | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |             'https://www.udemy.com/api-1.1/courses/%s' % course_path, | 
					
						
							|  |  |  |             course_path, 'Downloading course JSON') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         course_id = int(response['id']) | 
					
						
							|  |  |  |         course_title = response['title'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         webpage = self._download_webpage( | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |             'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id, | 
					
						
							|  |  |  |             course_id, 'Enrolling in the course') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if self._SUCCESSFULLY_ENROLLED in webpage: | 
					
						
							|  |  |  |             self.to_screen('%s: Successfully enrolled in' % course_id) | 
					
						
							|  |  |  |         elif self._ALREADY_ENROLLED in webpage: | 
					
						
							|  |  |  |             self.to_screen('%s: Already enrolled in' % course_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:25:43 +06:00
										 |  |  |         response = self._download_json( | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |             'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id, | 
					
						
							|  |  |  |             course_id, 'Downloading course curriculum') | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         entries = [ | 
					
						
							| 
									
										
										
										
											2014-11-26 21:00:18 +06:00
										 |  |  |             self.url_result( | 
					
						
							|  |  |  |                 'https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']), 'Udemy') | 
					
						
							|  |  |  |             for asset in response if asset.get('assetType') or asset.get('asset_type') == 'Video' | 
					
						
							| 
									
										
										
										
											2014-03-14 00:36:14 +07:00
										 |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 20:41:03 +01:00
										 |  |  |         return self.playlist_result(entries, course_id, course_title) |