X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Fextractor%2Fudemy.py;h=825172806ab62b2edc28c205a299809023297281;hb=5c2266df4b9aeb7881ed8c026a038e2a25e43734;hp=cf7bd0ae5e8e3b9879066da7c30e07937471b438;hpb=e293711802c010e2720f8df00fde34d0af5f6ac3;p=youtube-dl diff --git a/youtube_dl/extractor/udemy.py b/youtube_dl/extractor/udemy.py index cf7bd0ae5..825172806 100644 --- a/youtube_dl/extractor/udemy.py +++ b/youtube_dl/extractor/udemy.py @@ -3,17 +3,21 @@ from __future__ import unicode_literals import re from .common import InfoExtractor -from ..utils import ( +from ..compat import ( compat_urllib_parse, compat_urllib_request, +) +from ..utils import ( ExtractorError, + sanitized_Request, ) class UdemyIE(InfoExtractor): IE_NAME = 'udemy' _VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P\d+)' - _LOGIN_URL = 'https://www.udemy.com/join/login-submit/' + _LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1' + _ORIGIN_URL = 'https://www.udemy.com' _NETRC_MACHINE = 'udemy' _TESTS = [{ @@ -40,12 +44,7 @@ class UdemyIE(InfoExtractor): error_str += ' - %s' % error_data.get('formErrors') raise ExtractorError(error_str, expected=True) - def _download_json(self, url, video_id, note='Downloading JSON metadata'): - response = super(UdemyIE, self)._download_json(url, video_id, note) - self._handle_error(response) - return response - - def _download_json_cookies(self, url, video_id, note): + def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'): headers = { 'X-Udemy-Snail-Case': 'true', 'X-Requested-With': 'XMLHttpRequest', @@ -55,8 +54,16 @@ class UdemyIE(InfoExtractor): headers['X-Udemy-Client-Id'] = cookie.value elif cookie.name == 'access_token': headers['X-Udemy-Bearer-Token'] = cookie.value - request = compat_urllib_request.Request(url, headers=headers) - return self._download_json(request, video_id, note) + + 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 = sanitized_Request(url_or_request, headers=headers) + + response = super(UdemyIE, self)._download_json(url_or_request, video_id, note) + self._handle_error(response) + return response def _real_initialize(self): self._login() @@ -64,43 +71,45 @@ class UdemyIE(InfoExtractor): 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) + self.raise_login_required('Udemy account is required') login_popup = self._download_webpage( - 'https://www.udemy.com/join/login-popup?displayType=ajax&showSkipButton=1', None, - 'Downloading login popup') + self._LOGIN_URL, None, 'Downloading login popup') - if login_popup == '
': + def is_logged(webpage): + return any(p in webpage for p in ['href="https://www.udemy.com/user/logout/', '>Logout<']) + + # already logged in + if is_logged(login_popup): return - csrf = self._html_search_regex( - r']+class="form-errors[^"]*">(.+?)', + response, 'error message', default=None) + if error: + raise ExtractorError('Unable to login: %s' % error, expected=True) + raise ExtractorError('Unable to log in') def _real_extract(self, url): - mobj = re.match(self._VALID_URL, url) - lecture_id = mobj.group('id') + lecture_id = self._match_id(url) - lecture = self._download_json_cookies( + lecture = self._download_json( 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id, lecture_id, 'Downloading lecture JSON') @@ -164,7 +173,7 @@ class UdemyCourseIE(UdemyIE): mobj = re.match(self._VALID_URL, url) course_path = mobj.group('coursepath') - response = self._download_json_cookies( + response = self._download_json( 'https://www.udemy.com/api-1.1/courses/%s' % course_path, course_path, 'Downloading course JSON') @@ -180,7 +189,7 @@ class UdemyCourseIE(UdemyIE): elif self._ALREADY_ENROLLED in webpage: self.to_screen('%s: Already enrolled in' % course_id) - response = self._download_json_cookies( + response = self._download_json( 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id, course_id, 'Downloading course curriculum')