[udemy] Set session cookies to API requests (Closes #4124, closes #4219, closes #4308)

master
Sergey M․ 10 years ago
parent ceb3367320
commit e293711802

@ -45,6 +45,19 @@ class UdemyIE(InfoExtractor):
self._handle_error(response) self._handle_error(response)
return response return response
def _download_json_cookies(self, url, video_id, note):
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
request = compat_urllib_request.Request(url, headers=headers)
return self._download_json(request, video_id, note)
def _real_initialize(self): def _real_initialize(self):
self._login() self._login()
@ -62,7 +75,9 @@ class UdemyIE(InfoExtractor):
if login_popup == '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>': if login_popup == '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
return return
csrf = self._html_search_regex(r'<input type="hidden" name="csrf" value="(.+?)"', login_popup, 'csrf token') csrf = self._html_search_regex(
r'<input type="hidden" name="csrf" value="(.+?)"',
login_popup, 'csrf token')
login_form = { login_form = {
'email': username, 'email': username,
@ -71,42 +86,52 @@ class UdemyIE(InfoExtractor):
'displayType': 'json', 'displayType': 'json',
'isSubmitted': '1', 'isSubmitted': '1',
} }
request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form)) request = compat_urllib_request.Request(
response = self._download_json(request, None, 'Logging in as %s' % username) self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
response = self._download_json(
request, None, 'Logging in as %s' % username)
if 'returnUrl' not in response: if 'returnUrl' not in response:
raise ExtractorError('Unable to log in') raise ExtractorError('Unable to log in')
def _real_extract(self, url): def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url) mobj = re.match(self._VALID_URL, url)
lecture_id = mobj.group('id') lecture_id = mobj.group('id')
lecture = self._download_json( lecture = self._download_json_cookies(
'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id, lecture_id, 'Downloading lecture JSON') 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id,
lecture_id, 'Downloading lecture JSON')
if lecture['assetType'] != 'Video': asset_type = lecture.get('assetType') or lecture.get('asset_type')
raise ExtractorError('Lecture %s is not a video' % lecture_id, expected=True) if asset_type != 'Video':
raise ExtractorError(
'Lecture %s is not a video' % lecture_id, expected=True)
asset = lecture['asset'] asset = lecture['asset']
stream_url = asset['streamUrl'] stream_url = asset.get('streamUrl') or asset.get('stream_url')
mobj = re.search(r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url) mobj = re.search(r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url)
if mobj: if mobj:
return self.url_result(mobj.group(1), 'Youtube') return self.url_result(mobj.group(1), 'Youtube')
video_id = asset['id'] video_id = asset['id']
thumbnail = asset['thumbnailUrl'] thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
duration = asset['data']['duration'] duration = asset['data']['duration']
download_url = asset['downloadUrl'] 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')
formats = [ formats = [
{ {
'url': download_url['Video480p'][0], 'url': video_480p[0],
'format_id': '360p', 'format_id': '360p',
}, },
{ {
'url': download_url['Video'][0], 'url': video[0],
'format_id': '720p', 'format_id': '720p',
}, },
] ]
@ -139,26 +164,30 @@ class UdemyCourseIE(UdemyIE):
mobj = re.match(self._VALID_URL, url) mobj = re.match(self._VALID_URL, url)
course_path = mobj.group('coursepath') course_path = mobj.group('coursepath')
response = self._download_json( response = self._download_json_cookies(
'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_id = int(response['id'])
course_title = response['title'] course_title = response['title']
webpage = self._download_webpage( 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: if self._SUCCESSFULLY_ENROLLED in webpage:
self.to_screen('%s: Successfully enrolled in' % course_id) self.to_screen('%s: Successfully enrolled in' % course_id)
elif self._ALREADY_ENROLLED in webpage: elif self._ALREADY_ENROLLED in webpage:
self.to_screen('%s: Already enrolled in' % course_id) 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_cookies(
course_id, 'Downloading course curriculum') 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
course_id, 'Downloading course curriculum')
entries = [ entries = [
self.url_result('https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']), 'Udemy') self.url_result(
for asset in response if asset.get('assetType') == 'Video' '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) return self.playlist_result(entries, course_id, course_title)

Loading…
Cancel
Save