X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Fextractor%2Fudemy.py;h=4667ed83b71f4aec5f081741834e2c9cca010e82;hb=05a976cd99ef2a0eb0b301cd4f98e1aec927968c;hp=35df918b879ced4b7f2d7e90f1e21d15479504ca;hpb=e5de3f6c89d2432a3afbc6d0545259ad49ee17a9;p=youtube-dl diff --git a/youtube_dl/extractor/udemy.py b/youtube_dl/extractor/udemy.py index 35df918b8..4667ed83b 100644 --- a/youtube_dl/extractor/udemy.py +++ b/youtube_dl/extractor/udemy.py @@ -3,9 +3,11 @@ 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, ) @@ -16,7 +18,7 @@ class UdemyIE(InfoExtractor): _LOGIN_URL = 'https://www.udemy.com/join/login-submit/' _NETRC_MACHINE = 'udemy' - _TEST = { + _TESTS = [{ 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757', 'md5': '98eda5b657e752cf945d8445e261b5c5', 'info_dict': { @@ -27,7 +29,7 @@ class UdemyIE(InfoExtractor): 'duration': 579.29, }, 'skip': 'Requires udemy account credentials', - } + }] def _handle_error(self, response): if not isinstance(response, dict): @@ -40,8 +42,24 @@ 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) + def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'): + 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 + + 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 @@ -62,7 +80,9 @@ class UdemyIE(InfoExtractor): if login_popup == '
': return - csrf = self._html_search_regex(r'[\da-z-]+)' _SUCCESSFULLY_ENROLLED = '>You have enrolled in this course!<' _ALREADY_ENROLLED = '>You are already taking this course.<' + _TESTS = [] @classmethod def suitable(cls, url): @@ -139,25 +167,29 @@ class UdemyCourseIE(UdemyIE): course_path = mobj.group('coursepath') response = self._download_json( - 'https://www.udemy.com/api-1.1/courses/%s' % course_path, course_path, 'Downloading course JSON') + 'https://www.udemy.com/api-1.1/courses/%s' % course_path, + course_path, 'Downloading course JSON') course_id = int(response['id']) course_title = response['title'] webpage = self._download_webpage( - 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id, course_id, 'Enrolling in the course') + 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id, + course_id, 'Enrolling in the course') 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) - response = self._download_json('https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id, + response = self._download_json( + 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id, course_id, 'Downloading course curriculum') entries = [ - self.url_result('https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']), 'Udemy') - for asset in response if asset.get('assetType') == 'Video' + 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' ] - return self.playlist_result(entries, course_id, course_title) \ No newline at end of file + return self.playlist_result(entries, course_id, course_title)