]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udemy.py
825172806ab62b2edc28c205a299809023297281
1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
16 class UdemyIE(InfoExtractor
):
18 _VALID_URL
= r
'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
19 _LOGIN_URL
= 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
20 _ORIGIN_URL
= 'https://www.udemy.com'
21 _NETRC_MACHINE
= 'udemy'
24 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
25 'md5': '98eda5b657e752cf945d8445e261b5c5',
29 'title': 'Introduction and Installation',
30 'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
33 'skip': 'Requires udemy account credentials',
36 def _handle_error(self
, response
):
37 if not isinstance(response
, dict):
39 error
= response
.get('error')
41 error_str
= 'Udemy returned error #%s: %s' % (error
.get('code'), error
.get('message'))
42 error_data
= error
.get('data')
44 error_str
+= ' - %s' % error_data
.get('formErrors')
45 raise ExtractorError(error_str
, expected
=True)
47 def _download_json(self
, url_or_request
, video_id
, note
='Downloading JSON metadata'):
49 'X-Udemy-Snail-Case': 'true',
50 'X-Requested-With': 'XMLHttpRequest',
52 for cookie
in self
._downloader
.cookiejar
:
53 if cookie
.name
== 'client_id':
54 headers
['X-Udemy-Client-Id'] = cookie
.value
55 elif cookie
.name
== 'access_token':
56 headers
['X-Udemy-Bearer-Token'] = cookie
.value
58 if isinstance(url_or_request
, compat_urllib_request
.Request
):
59 for header
, value
in headers
.items():
60 url_or_request
.add_header(header
, value
)
62 url_or_request
= sanitized_Request(url_or_request
, headers
=headers
)
64 response
= super(UdemyIE
, self
)._download
_json
(url_or_request
, video_id
, note
)
65 self
._handle
_error
(response
)
68 def _real_initialize(self
):
72 (username
, password
) = self
._get
_login
_info
()
74 self
.raise_login_required('Udemy account is required')
76 login_popup
= self
._download
_webpage
(
77 self
._LOGIN
_URL
, None, 'Downloading login popup')
79 def is_logged(webpage
):
80 return any(p
in webpage
for p
in ['href="https://www.udemy.com/user/logout/', '>Logout<'])
83 if is_logged(login_popup
):
86 login_form
= self
._form
_hidden
_inputs
('login-form', login_popup
)
89 'email': username
.encode('utf-8'),
90 'password': password
.encode('utf-8'),
93 request
= sanitized_Request(
94 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
).encode('utf-8'))
95 request
.add_header('Referer', self
._ORIGIN
_URL
)
96 request
.add_header('Origin', self
._ORIGIN
_URL
)
98 response
= self
._download
_webpage
(
99 request
, None, 'Logging in as %s' % username
)
101 if not is_logged(response
):
102 error
= self
._html
_search
_regex
(
103 r
'(?s)<div[^>]+class="form-errors[^"]*">(.+?)</div>',
104 response
, 'error message', default
=None)
106 raise ExtractorError('Unable to login: %s' % error
, expected
=True)
107 raise ExtractorError('Unable to log in')
109 def _real_extract(self
, url
):
110 lecture_id
= self
._match
_id
(url
)
112 lecture
= self
._download
_json
(
113 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id
,
114 lecture_id
, 'Downloading lecture JSON')
116 asset_type
= lecture
.get('assetType') or lecture
.get('asset_type')
117 if asset_type
!= 'Video':
118 raise ExtractorError(
119 'Lecture %s is not a video' % lecture_id
, expected
=True)
121 asset
= lecture
['asset']
123 stream_url
= asset
.get('streamUrl') or asset
.get('stream_url')
124 mobj
= re
.search(r
'(https?://www\.youtube\.com/watch\?v=.*)', stream_url
)
126 return self
.url_result(mobj
.group(1), 'Youtube')
128 video_id
= asset
['id']
129 thumbnail
= asset
.get('thumbnailUrl') or asset
.get('thumbnail_url')
130 duration
= asset
['data']['duration']
132 download_url
= asset
.get('downloadUrl') or asset
.get('download_url')
134 video
= download_url
.get('Video') or download_url
.get('video')
135 video_480p
= download_url
.get('Video480p') or download_url
.get('video_480p')
139 'url': video_480p
[0],
148 title
= lecture
['title']
149 description
= lecture
['description']
154 'description': description
,
155 'thumbnail': thumbnail
,
156 'duration': duration
,
161 class UdemyCourseIE(UdemyIE
):
162 IE_NAME
= 'udemy:course'
163 _VALID_URL
= r
'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
164 _SUCCESSFULLY_ENROLLED
= '>You have enrolled in this course!<'
165 _ALREADY_ENROLLED
= '>You are already taking this course.<'
169 def suitable(cls
, url
):
170 return False if UdemyIE
.suitable(url
) else super(UdemyCourseIE
, cls
).suitable(url
)
172 def _real_extract(self
, url
):
173 mobj
= re
.match(self
._VALID
_URL
, url
)
174 course_path
= mobj
.group('coursepath')
176 response
= self
._download
_json
(
177 'https://www.udemy.com/api-1.1/courses/%s' % course_path
,
178 course_path
, 'Downloading course JSON')
180 course_id
= int(response
['id'])
181 course_title
= response
['title']
183 webpage
= self
._download
_webpage
(
184 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id
,
185 course_id
, 'Enrolling in the course')
187 if self
._SUCCESSFULLY
_ENROLLED
in webpage
:
188 self
.to_screen('%s: Successfully enrolled in' % course_id
)
189 elif self
._ALREADY
_ENROLLED
in webpage
:
190 self
.to_screen('%s: Already enrolled in' % course_id
)
192 response
= self
._download
_json
(
193 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id
,
194 course_id
, 'Downloading course curriculum')
198 'https://www.udemy.com/%s/#/lecture/%s' % (course_path
, asset
['id']), 'Udemy')
199 for asset
in response
if asset
.get('assetType') or asset
.get('asset_type') == 'Video'
202 return self
.playlist_result(entries
, course_id
, course_title
)