]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udemy.py
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-submit/'
19 _NETRC_MACHINE
= 'udemy'
22 'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
23 'md5': '98eda5b657e752cf945d8445e261b5c5',
27 'title': 'Introduction and Installation',
28 'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
31 'skip': 'Requires udemy account credentials',
34 def _handle_error(self
, response
):
35 if not isinstance(response
, dict):
37 error
= response
.get('error')
39 error_str
= 'Udemy returned error #%s: %s' % (error
.get('code'), error
.get('message'))
40 error_data
= error
.get('data')
42 error_str
+= ' - %s' % error_data
.get('formErrors')
43 raise ExtractorError(error_str
, expected
=True)
45 def _download_json(self
, url_or_request
, video_id
, note
='Downloading JSON metadata'):
47 'X-Udemy-Snail-Case': 'true',
48 'X-Requested-With': 'XMLHttpRequest',
50 for cookie
in self
._downloader
.cookiejar
:
51 if cookie
.name
== 'client_id':
52 headers
['X-Udemy-Client-Id'] = cookie
.value
53 elif cookie
.name
== 'access_token':
54 headers
['X-Udemy-Bearer-Token'] = cookie
.value
56 if isinstance(url_or_request
, compat_urllib_request
.Request
):
57 for header
, value
in headers
.items():
58 url_or_request
.add_header(header
, value
)
60 url_or_request
= compat_urllib_request
.Request(url_or_request
, headers
=headers
)
62 response
= super(UdemyIE
, self
)._download
_json
(url_or_request
, video_id
, note
)
63 self
._handle
_error
(response
)
66 def _real_initialize(self
):
70 (username
, password
) = self
._get
_login
_info
()
73 'Udemy account is required, use --username and --password options to provide account credentials.',
76 login_popup
= self
._download
_webpage
(
77 'https://www.udemy.com/join/login-popup?displayType=ajax&showSkipButton=1', None,
78 'Downloading login popup')
80 if login_popup
== '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
83 csrf
= self
._html
_search
_regex
(
84 r
'<input type="hidden" name="csrf" value="(.+?)"',
85 login_popup
, 'csrf token')
91 'displayType': 'json',
94 request
= compat_urllib_request
.Request(
95 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
).encode('utf-8'))
96 response
= self
._download
_json
(
97 request
, None, 'Logging in as %s' % username
)
99 if 'returnUrl' not in response
:
100 raise ExtractorError('Unable to log in')
102 def _real_extract(self
, url
):
103 lecture_id
= self
._match
_id
(url
)
105 lecture
= self
._download
_json
(
106 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id
,
107 lecture_id
, 'Downloading lecture JSON')
109 asset_type
= lecture
.get('assetType') or lecture
.get('asset_type')
110 if asset_type
!= 'Video':
111 raise ExtractorError(
112 'Lecture %s is not a video' % lecture_id
, expected
=True)
114 asset
= lecture
['asset']
116 stream_url
= asset
.get('streamUrl') or asset
.get('stream_url')
117 mobj
= re
.search(r
'(https?://www\.youtube\.com/watch\?v=.*)', stream_url
)
119 return self
.url_result(mobj
.group(1), 'Youtube')
121 video_id
= asset
['id']
122 thumbnail
= asset
.get('thumbnailUrl') or asset
.get('thumbnail_url')
123 duration
= asset
['data']['duration']
125 download_url
= asset
.get('downloadUrl') or asset
.get('download_url')
127 video
= download_url
.get('Video') or download_url
.get('video')
128 video_480p
= download_url
.get('Video480p') or download_url
.get('video_480p')
132 'url': video_480p
[0],
141 title
= lecture
['title']
142 description
= lecture
['description']
147 'description': description
,
148 'thumbnail': thumbnail
,
149 'duration': duration
,
154 class UdemyCourseIE(UdemyIE
):
155 IE_NAME
= 'udemy:course'
156 _VALID_URL
= r
'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
157 _SUCCESSFULLY_ENROLLED
= '>You have enrolled in this course!<'
158 _ALREADY_ENROLLED
= '>You are already taking this course.<'
162 def suitable(cls
, url
):
163 return False if UdemyIE
.suitable(url
) else super(UdemyCourseIE
, cls
).suitable(url
)
165 def _real_extract(self
, url
):
166 mobj
= re
.match(self
._VALID
_URL
, url
)
167 course_path
= mobj
.group('coursepath')
169 response
= self
._download
_json
(
170 'https://www.udemy.com/api-1.1/courses/%s' % course_path
,
171 course_path
, 'Downloading course JSON')
173 course_id
= int(response
['id'])
174 course_title
= response
['title']
176 webpage
= self
._download
_webpage
(
177 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id
,
178 course_id
, 'Enrolling in the course')
180 if self
._SUCCESSFULLY
_ENROLLED
in webpage
:
181 self
.to_screen('%s: Successfully enrolled in' % course_id
)
182 elif self
._ALREADY
_ENROLLED
in webpage
:
183 self
.to_screen('%s: Already enrolled in' % course_id
)
185 response
= self
._download
_json
(
186 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id
,
187 course_id
, 'Downloading course curriculum')
191 'https://www.udemy.com/%s/#/lecture/%s' % (course_path
, asset
['id']), 'Udemy')
192 for asset
in response
if asset
.get('assetType') or asset
.get('asset_type') == 'Video'
195 return self
.playlist_result(entries
, course_id
, course_title
)