]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udemy.py
e2bab52fef3451596ec1cf0de19e3131e378b5dd
1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
15 class UdemyIE(InfoExtractor
):
17 _VALID_URL
= r
'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
18 _LOGIN_URL
= 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
19 _ORIGIN_URL
= 'https://www.udemy.com'
20 _NETRC_MACHINE
= 'udemy'
23 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
24 'md5': '98eda5b657e752cf945d8445e261b5c5',
28 'title': 'Introduction and Installation',
29 'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
32 'skip': 'Requires udemy account credentials',
35 def _handle_error(self
, response
):
36 if not isinstance(response
, dict):
38 error
= response
.get('error')
40 error_str
= 'Udemy returned error #%s: %s' % (error
.get('code'), error
.get('message'))
41 error_data
= error
.get('data')
43 error_str
+= ' - %s' % error_data
.get('formErrors')
44 raise ExtractorError(error_str
, expected
=True)
46 def _download_json(self
, url_or_request
, video_id
, note
='Downloading JSON metadata'):
48 'X-Udemy-Snail-Case': 'true',
49 'X-Requested-With': 'XMLHttpRequest',
51 for cookie
in self
._downloader
.cookiejar
:
52 if cookie
.name
== 'client_id':
53 headers
['X-Udemy-Client-Id'] = cookie
.value
54 elif cookie
.name
== 'access_token':
55 headers
['X-Udemy-Bearer-Token'] = cookie
.value
57 if isinstance(url_or_request
, compat_urllib_request
.Request
):
58 for header
, value
in headers
.items():
59 url_or_request
.add_header(header
, value
)
61 url_or_request
= compat_urllib_request
.Request(url_or_request
, headers
=headers
)
63 response
= super(UdemyIE
, self
)._download
_json
(url_or_request
, video_id
, note
)
64 self
._handle
_error
(response
)
67 def _real_initialize(self
):
71 (username
, password
) = self
._get
_login
_info
()
74 'Udemy account is required, use --username and --password options to provide account credentials.',
77 login_popup
= self
._download
_webpage
(
78 self
._LOGIN
_URL
, None, 'Downloading login popup')
80 if login_popup
== '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
83 login_form
= self
._form
_hidden
_inputs
('login-form', login_popup
)
86 'email': username
.encode('utf-8'),
87 'password': password
.encode('utf-8'),
90 request
= compat_urllib_request
.Request(
91 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
).encode('utf-8'))
92 request
.add_header('Referer', self
._ORIGIN
_URL
)
93 request
.add_header('Origin', self
._ORIGIN
_URL
)
95 response
= self
._download
_webpage
(
96 request
, None, 'Logging in as %s' % username
)
98 if all(logout_pattern
not in response
99 for logout_pattern
in ['href="https://www.udemy.com/user/logout/', '>Logout<']):
100 error
= self
._html
_search
_regex
(
101 r
'(?s)<div[^>]+class="form-errors[^"]*">(.+?)</div>',
102 response
, 'error message', default
=None)
104 raise ExtractorError('Unable to login: %s' % error
, expected
=True)
105 raise ExtractorError('Unable to log in')
107 def _real_extract(self
, url
):
108 lecture_id
= self
._match
_id
(url
)
110 lecture
= self
._download
_json
(
111 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id
,
112 lecture_id
, 'Downloading lecture JSON')
114 asset_type
= lecture
.get('assetType') or lecture
.get('asset_type')
115 if asset_type
!= 'Video':
116 raise ExtractorError(
117 'Lecture %s is not a video' % lecture_id
, expected
=True)
119 asset
= lecture
['asset']
121 stream_url
= asset
.get('streamUrl') or asset
.get('stream_url')
122 mobj
= re
.search(r
'(https?://www\.youtube\.com/watch\?v=.*)', stream_url
)
124 return self
.url_result(mobj
.group(1), 'Youtube')
126 video_id
= asset
['id']
127 thumbnail
= asset
.get('thumbnailUrl') or asset
.get('thumbnail_url')
128 duration
= asset
['data']['duration']
130 download_url
= asset
.get('downloadUrl') or asset
.get('download_url')
132 video
= download_url
.get('Video') or download_url
.get('video')
133 video_480p
= download_url
.get('Video480p') or download_url
.get('video_480p')
137 'url': video_480p
[0],
146 title
= lecture
['title']
147 description
= lecture
['description']
152 'description': description
,
153 'thumbnail': thumbnail
,
154 'duration': duration
,
159 class UdemyCourseIE(UdemyIE
):
160 IE_NAME
= 'udemy:course'
161 _VALID_URL
= r
'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
162 _SUCCESSFULLY_ENROLLED
= '>You have enrolled in this course!<'
163 _ALREADY_ENROLLED
= '>You are already taking this course.<'
167 def suitable(cls
, url
):
168 return False if UdemyIE
.suitable(url
) else super(UdemyCourseIE
, cls
).suitable(url
)
170 def _real_extract(self
, url
):
171 mobj
= re
.match(self
._VALID
_URL
, url
)
172 course_path
= mobj
.group('coursepath')
174 response
= self
._download
_json
(
175 'https://www.udemy.com/api-1.1/courses/%s' % course_path
,
176 course_path
, 'Downloading course JSON')
178 course_id
= int(response
['id'])
179 course_title
= response
['title']
181 webpage
= self
._download
_webpage
(
182 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id
,
183 course_id
, 'Enrolling in the course')
185 if self
._SUCCESSFULLY
_ENROLLED
in webpage
:
186 self
.to_screen('%s: Successfully enrolled in' % course_id
)
187 elif self
._ALREADY
_ENROLLED
in webpage
:
188 self
.to_screen('%s: Already enrolled in' % course_id
)
190 response
= self
._download
_json
(
191 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id
,
192 course_id
, 'Downloading course curriculum')
196 'https://www.udemy.com/%s/#/lecture/%s' % (course_path
, asset
['id']), 'Udemy')
197 for asset
in response
if asset
.get('assetType') or asset
.get('asset_type') == 'Video'
200 return self
.playlist_result(entries
, course_id
, course_title
)