2014-03-13 17:36:14 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from .common import InfoExtractor
|
2014-12-13 11:24:42 +00:00
|
|
|
from ..compat import (
|
2015-12-01 18:48:27 +00:00
|
|
|
compat_HTTPError,
|
2014-03-13 17:36:14 +00:00
|
|
|
compat_urllib_parse,
|
|
|
|
compat_urllib_request,
|
2016-03-24 20:26:46 +00:00
|
|
|
compat_urlparse,
|
2014-12-13 11:24:42 +00:00
|
|
|
)
|
|
|
|
from ..utils import (
|
2014-03-13 17:36:14 +00:00
|
|
|
ExtractorError,
|
2015-12-01 18:48:27 +00:00
|
|
|
float_or_none,
|
2015-12-01 14:35:46 +00:00
|
|
|
int_or_none,
|
2015-11-21 16:18:17 +00:00
|
|
|
sanitized_Request,
|
2016-01-05 18:02:21 +00:00
|
|
|
unescapeHTML,
|
2014-03-13 17:36:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class UdemyIE(InfoExtractor):
|
|
|
|
IE_NAME = 'udemy'
|
|
|
|
_VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
|
2015-07-14 16:39:41 +00:00
|
|
|
_LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
|
|
|
|
_ORIGIN_URL = 'https://www.udemy.com'
|
2014-03-13 17:36:14 +00:00
|
|
|
_NETRC_MACHINE = 'udemy'
|
|
|
|
|
2014-03-16 06:09:10 +00:00
|
|
|
_TESTS = [{
|
2014-03-13 17:36:14 +00: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 06:09:10 +00:00
|
|
|
}]
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2016-03-24 20:26:46 +00:00
|
|
|
def _enroll_course(self, base_url, webpage, course_id):
|
2016-01-05 18:02:21 +00:00
|
|
|
checkout_url = unescapeHTML(self._search_regex(
|
|
|
|
r'href=(["\'])(?P<url>https?://(?:www\.)?udemy\.com/payment/checkout/.+?)\1',
|
|
|
|
webpage, 'checkout url', group='url', default=None))
|
|
|
|
if checkout_url:
|
|
|
|
raise ExtractorError(
|
2016-01-05 18:03:39 +00:00
|
|
|
'Course %s is not free. You have to pay for it before you can download. '
|
2016-01-05 18:02:21 +00:00
|
|
|
'Use this URL to confirm purchase: %s' % (course_id, checkout_url), expected=True)
|
|
|
|
|
|
|
|
enroll_url = unescapeHTML(self._search_regex(
|
2016-03-24 20:26:46 +00:00
|
|
|
r'href=(["\'])(?P<url>(?:https?://(?:www\.)?udemy\.com)?/course/subscribe/.+?)\1',
|
2016-01-05 18:02:21 +00:00
|
|
|
webpage, 'enroll url', group='url', default=None))
|
|
|
|
if enroll_url:
|
2016-03-24 20:26:46 +00:00
|
|
|
if not enroll_url.startswith('http'):
|
|
|
|
enroll_url = compat_urlparse.urljoin(base_url, enroll_url)
|
2016-01-05 18:02:21 +00:00
|
|
|
webpage = self._download_webpage(enroll_url, course_id, 'Enrolling in the course')
|
|
|
|
if '>You have enrolled in' in webpage:
|
|
|
|
self.to_screen('%s: Successfully enrolled in the course' % course_id)
|
2015-12-01 18:48:27 +00:00
|
|
|
|
|
|
|
def _download_lecture(self, course_id, lecture_id):
|
|
|
|
return self._download_json(
|
|
|
|
'https://www.udemy.com/api-2.0/users/me/subscribed-courses/%s/lectures/%s?%s' % (
|
|
|
|
course_id, lecture_id, compat_urllib_parse.urlencode({
|
|
|
|
'video_only': '',
|
|
|
|
'auto_play': '',
|
|
|
|
'fields[lecture]': 'title,description,asset',
|
|
|
|
'fields[asset]': 'asset_type,stream_url,thumbnail_url,download_urls,data',
|
|
|
|
'instructorPreviewMode': 'False',
|
|
|
|
})),
|
2015-12-01 18:53:03 +00:00
|
|
|
lecture_id, 'Downloading lecture JSON')
|
2015-12-01 18:48:27 +00:00
|
|
|
|
2014-03-13 17:36:14 +00: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)
|
|
|
|
|
2015-12-01 18:53:03 +00:00
|
|
|
def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
|
2014-11-26 15:00:18 +00: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
|
2015-12-01 18:48:27 +00:00
|
|
|
headers['X-Udemy-Authorization'] = 'Bearer %s' % cookie.value
|
2014-11-26 15:25:43 +00:00
|
|
|
|
|
|
|
if isinstance(url_or_request, compat_urllib_request.Request):
|
|
|
|
for header, value in headers.items():
|
|
|
|
url_or_request.add_header(header, value)
|
|
|
|
else:
|
2015-11-21 16:18:17 +00:00
|
|
|
url_or_request = sanitized_Request(url_or_request, headers=headers)
|
2014-11-26 15:25:43 +00:00
|
|
|
|
2015-12-01 18:53:03 +00:00
|
|
|
response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
|
2014-11-26 15:25:43 +00:00
|
|
|
self._handle_error(response)
|
|
|
|
return response
|
2014-11-26 15:00:18 +00:00
|
|
|
|
2014-03-13 17:36:14 +00:00
|
|
|
def _real_initialize(self):
|
|
|
|
self._login()
|
|
|
|
|
|
|
|
def _login(self):
|
|
|
|
(username, password) = self._get_login_info()
|
|
|
|
if username is None:
|
2015-12-01 16:10:10 +00:00
|
|
|
return
|
2014-03-13 17:36:14 +00:00
|
|
|
|
|
|
|
login_popup = self._download_webpage(
|
2015-07-14 16:39:41 +00:00
|
|
|
self._LOGIN_URL, None, 'Downloading login popup')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-07-22 16:49:00 +00:00
|
|
|
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):
|
2014-03-13 17:36:14 +00:00
|
|
|
return
|
|
|
|
|
2015-07-14 16:39:41 +00:00
|
|
|
login_form = self._form_hidden_inputs('login-form', login_popup)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-07-14 16:39:41 +00:00
|
|
|
login_form.update({
|
|
|
|
'email': username.encode('utf-8'),
|
|
|
|
'password': password.encode('utf-8'),
|
|
|
|
})
|
|
|
|
|
2015-11-21 16:18:17 +00:00
|
|
|
request = sanitized_Request(
|
2014-11-26 15:25:43 +00:00
|
|
|
self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
|
2015-07-14 16:39:41 +00:00
|
|
|
request.add_header('Referer', self._ORIGIN_URL)
|
|
|
|
request.add_header('Origin', self._ORIGIN_URL)
|
|
|
|
|
|
|
|
response = self._download_webpage(
|
2014-11-26 15:00:18 +00:00
|
|
|
request, None, 'Logging in as %s' % username)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-07-22 16:49:00 +00:00
|
|
|
if not is_logged(response):
|
2015-07-14 16:39:41 +00:00
|
|
|
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-13 17:36:14 +00:00
|
|
|
raise ExtractorError('Unable to log in')
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2014-12-04 07:27:40 +00:00
|
|
|
lecture_id = self._match_id(url)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-12-01 18:48:27 +00:00
|
|
|
webpage = self._download_webpage(url, lecture_id)
|
|
|
|
|
|
|
|
course_id = self._search_regex(
|
2016-03-16 15:46:09 +00:00
|
|
|
(r'data-course-id=["\'](\d+)', r'"id"\s*:\s*(\d+)'),
|
2016-03-16 03:15:39 +00:00
|
|
|
webpage, 'course id')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-12-01 18:48:27 +00:00
|
|
|
try:
|
|
|
|
lecture = self._download_lecture(course_id, lecture_id)
|
|
|
|
except ExtractorError as e:
|
|
|
|
# Error could possibly mean we are not enrolled in the course
|
|
|
|
if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
|
2016-03-24 20:26:46 +00:00
|
|
|
self._enroll_course(url, webpage, course_id)
|
2015-12-30 19:09:21 +00:00
|
|
|
lecture = self._download_lecture(course_id, lecture_id)
|
2015-12-01 18:48:27 +00:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
title = lecture['title']
|
|
|
|
description = lecture.get('description')
|
|
|
|
|
|
|
|
asset = lecture['asset']
|
|
|
|
|
|
|
|
asset_type = asset.get('assetType') or asset.get('asset_type')
|
2014-11-26 15:00:18 +00:00
|
|
|
if asset_type != 'Video':
|
|
|
|
raise ExtractorError(
|
|
|
|
'Lecture %s is not a video' % lecture_id, expected=True)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2014-11-26 15:00:18 +00:00
|
|
|
stream_url = asset.get('streamUrl') or asset.get('stream_url')
|
2015-12-01 18:48:27 +00:00
|
|
|
if stream_url:
|
|
|
|
youtube_url = self._search_regex(
|
|
|
|
r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url, 'youtube URL', default=None)
|
|
|
|
if youtube_url:
|
|
|
|
return self.url_result(youtube_url, 'Youtube')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
|
|
|
video_id = asset['id']
|
2014-11-26 15:00:18 +00:00
|
|
|
thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
|
2015-12-01 18:48:27 +00:00
|
|
|
duration = float_or_none(asset.get('data', {}).get('duration'))
|
|
|
|
|
|
|
|
formats = []
|
2016-03-24 20:27:13 +00:00
|
|
|
|
|
|
|
def extract_output_format(src):
|
|
|
|
return {
|
|
|
|
'url': src['url'],
|
|
|
|
'format_id': '%sp' % (src.get('label') or format_id),
|
|
|
|
'width': int_or_none(src.get('width')),
|
|
|
|
'height': int_or_none(src.get('height')),
|
|
|
|
'vbr': int_or_none(src.get('video_bitrate_in_kbps')),
|
|
|
|
'vcodec': src.get('video_codec'),
|
|
|
|
'fps': int_or_none(src.get('frame_rate')),
|
|
|
|
'abr': int_or_none(src.get('audio_bitrate_in_kbps')),
|
|
|
|
'acodec': src.get('audio_codec'),
|
|
|
|
'asr': int_or_none(src.get('audio_sample_rate')),
|
|
|
|
'tbr': int_or_none(src.get('total_bitrate_in_kbps')),
|
|
|
|
'filesize': int_or_none(src.get('file_size_in_bytes')),
|
2015-12-01 18:48:27 +00:00
|
|
|
}
|
2016-03-24 20:27:13 +00:00
|
|
|
|
|
|
|
outputs = asset.get('data', {}).get('outputs')
|
|
|
|
if not isinstance(outputs, dict):
|
|
|
|
outputs = {}
|
|
|
|
|
|
|
|
for format_id, output in outputs.items():
|
|
|
|
if isinstance(output, dict) and output.get('url'):
|
|
|
|
formats.append(extract_output_format(output))
|
|
|
|
|
|
|
|
download_urls = asset.get('download_urls')
|
|
|
|
if isinstance(download_urls, dict):
|
|
|
|
video = download_urls.get('Video')
|
|
|
|
if isinstance(video, list):
|
|
|
|
for format_ in video:
|
|
|
|
video_url = format_.get('file')
|
|
|
|
if not video_url:
|
|
|
|
continue
|
|
|
|
format_id = format_.get('label')
|
|
|
|
f = {
|
|
|
|
'url': format_['file'],
|
|
|
|
'height': int_or_none(format_id),
|
|
|
|
}
|
|
|
|
if format_id:
|
|
|
|
# Some videos contain additional metadata (e.g.
|
|
|
|
# https://www.udemy.com/ios9-swift/learn/#/lecture/3383208)
|
|
|
|
output = outputs.get(format_id)
|
|
|
|
if isinstance(output, dict):
|
|
|
|
output_format = extract_output_format(output)
|
|
|
|
output_format.update(f)
|
|
|
|
f = output_format
|
|
|
|
else:
|
|
|
|
f['format_id'] = '%sp' % format_id
|
|
|
|
formats.append(f)
|
2015-12-01 14:35:46 +00:00
|
|
|
|
|
|
|
self._sort_formats(formats)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'id': video_id,
|
|
|
|
'title': title,
|
|
|
|
'description': description,
|
|
|
|
'thumbnail': thumbnail,
|
|
|
|
'duration': duration,
|
|
|
|
'formats': formats
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class UdemyCourseIE(UdemyIE):
|
|
|
|
IE_NAME = 'udemy:course'
|
2015-12-01 18:48:27 +00:00
|
|
|
_VALID_URL = r'https?://www\.udemy\.com/(?P<id>[\da-z-]+)'
|
2014-03-16 06:09:10 +00:00
|
|
|
_TESTS = []
|
2014-03-13 17:36:14 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def suitable(cls, url):
|
|
|
|
return False if UdemyIE.suitable(url) else super(UdemyCourseIE, cls).suitable(url)
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2015-12-01 18:48:27 +00:00
|
|
|
course_path = self._match_id(url)
|
|
|
|
|
|
|
|
webpage = self._download_webpage(url, course_path)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2014-11-26 15:25:43 +00:00
|
|
|
response = self._download_json(
|
2014-11-26 15:00:18 +00:00
|
|
|
'https://www.udemy.com/api-1.1/courses/%s' % course_path,
|
|
|
|
course_path, 'Downloading course JSON')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-12-01 18:48:27 +00:00
|
|
|
course_id = response['id']
|
|
|
|
course_title = response.get('title')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2016-03-24 20:26:46 +00:00
|
|
|
self._enroll_course(url, webpage, course_id)
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2014-11-26 15:25:43 +00:00
|
|
|
response = self._download_json(
|
2014-11-26 15:00:18 +00:00
|
|
|
'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
|
|
|
|
course_id, 'Downloading course curriculum')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2015-12-30 21:11:21 +00:00
|
|
|
entries = []
|
2016-01-01 14:34:29 +00:00
|
|
|
chapter, chapter_number = None, None
|
2015-12-30 21:11:21 +00:00
|
|
|
for asset in response:
|
|
|
|
asset_type = asset.get('assetType') or asset.get('asset_type')
|
|
|
|
if asset_type == 'Video':
|
|
|
|
asset_id = asset.get('id')
|
|
|
|
if asset_id:
|
|
|
|
entry = {
|
|
|
|
'_type': 'url_transparent',
|
|
|
|
'url': 'https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']),
|
|
|
|
'ie_key': UdemyIE.ie_key(),
|
|
|
|
}
|
2016-01-01 14:34:29 +00:00
|
|
|
if chapter_number:
|
|
|
|
entry['chapter_number'] = chapter_number
|
2015-12-30 21:11:21 +00:00
|
|
|
if chapter:
|
|
|
|
entry['chapter'] = chapter
|
|
|
|
entries.append(entry)
|
|
|
|
elif asset.get('type') == 'chapter':
|
2016-01-01 14:34:29 +00:00
|
|
|
chapter_number = asset.get('index') or asset.get('object_index')
|
2015-12-30 21:11:21 +00:00
|
|
|
chapter = asset.get('title')
|
2014-03-13 17:36:14 +00:00
|
|
|
|
2014-11-23 19:41:03 +00:00
|
|
|
return self.playlist_result(entries, course_id, course_title)
|