]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udemy.py
054f427252341306edd7698e6d150bcb619b64fc
1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
13 class UdemyIE(InfoExtractor
):
15 _VALID_URL
= r
'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
16 _LOGIN_URL
= 'https://www.udemy.com/join/login-submit/'
17 _NETRC_MACHINE
= 'udemy'
20 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
21 'md5': '98eda5b657e752cf945d8445e261b5c5',
25 'title': 'Introduction and Installation',
26 'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
29 'skip': 'Requires udemy account credentials',
32 def _handle_error(self
, response
):
33 if not isinstance(response
, dict):
35 error
= response
.get('error')
37 error_str
= 'Udemy returned error #%s: %s' % (error
.get('code'), error
.get('message'))
38 error_data
= error
.get('data')
40 error_str
+= ' - %s' % error_data
.get('formErrors')
41 raise ExtractorError(error_str
, expected
=True)
43 def _download_json(self
, url
, video_id
, note
='Downloading JSON metadata'):
44 response
= super(UdemyIE
, self
)._download
_json
(url
, video_id
, note
)
45 self
._handle
_error
(response
)
48 def _real_initialize(self
):
52 (username
, password
) = self
._get
_login
_info
()
55 'Udemy account is required, use --username and --password options to provide account credentials.',
58 login_popup
= self
._download
_webpage
(
59 'https://www.udemy.com/join/login-popup?displayType=ajax&showSkipButton=1', None,
60 'Downloading login popup')
62 if login_popup
== '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
65 csrf
= self
._html
_search
_regex
(r
'<input type="hidden" name="csrf" value="(.+?)"', login_popup
, 'csrf token')
71 'displayType': 'json',
74 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
))
75 response
= self
._download
_json
(request
, None, 'Logging in as %s' % username
)
77 if 'returnUrl' not in response
:
78 raise ExtractorError('Unable to log in')
80 def _real_extract(self
, url
):
81 mobj
= re
.match(self
._VALID
_URL
, url
)
82 lecture_id
= mobj
.group('id')
84 lecture
= self
._download
_json
(
85 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id
, lecture_id
, 'Downloading lecture JSON')
87 if lecture
['assetType'] != 'Video':
88 raise ExtractorError('Lecture %s is not a video' % lecture_id
, expected
=True)
90 asset
= lecture
['asset']
92 stream_url
= asset
['streamUrl']
93 mobj
= re
.search(r
'(https?://www\.youtube\.com/watch\?v=.*)', stream_url
)
95 return self
.url_result(mobj
.group(1), 'Youtube')
97 video_id
= asset
['id']
98 thumbnail
= asset
['thumbnailUrl']
99 duration
= asset
['data']['duration']
101 download_url
= asset
['downloadUrl']
105 'url': download_url
['Video480p'][0],
109 'url': download_url
['Video'][0],
114 title
= lecture
['title']
115 description
= lecture
['description']
120 'description': description
,
121 'thumbnail': thumbnail
,
122 'duration': duration
,
127 class UdemyCourseIE(UdemyIE
):
128 IE_NAME
= 'udemy:course'
129 _VALID_URL
= r
'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
130 _SUCCESSFULLY_ENROLLED
= '>You have enrolled in this course!<'
131 _ALREADY_ENROLLED
= '>You are already taking this course.<'
135 def suitable(cls
, url
):
136 return False if UdemyIE
.suitable(url
) else super(UdemyCourseIE
, cls
).suitable(url
)
138 def _real_extract(self
, url
):
139 mobj
= re
.match(self
._VALID
_URL
, url
)
140 course_path
= mobj
.group('coursepath')
142 response
= self
._download
_json
(
143 'https://www.udemy.com/api-1.1/courses/%s' % course_path
, course_path
, 'Downloading course JSON')
145 course_id
= int(response
['id'])
146 course_title
= response
['title']
148 webpage
= self
._download
_webpage
(
149 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id
, course_id
, 'Enrolling in the course')
151 if self
._SUCCESSFULLY
_ENROLLED
in webpage
:
152 self
.to_screen('%s: Successfully enrolled in' % course_id
)
153 elif self
._ALREADY
_ENROLLED
in webpage
:
154 self
.to_screen('%s: Already enrolled in' % course_id
)
156 response
= self
._download
_json
('https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id
,
157 course_id
, 'Downloading course curriculum')
160 self
.url_result('https://www.udemy.com/%s/#/lecture/%s' % (course_path
, asset
['id']), 'Udemy')
161 for asset
in response
if asset
.get('assetType') == 'Video'
164 return self
.playlist_result(entries
, course_id
, course_title
)