]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udemy.py
0e4d386a8ba32387f6f9025e633efb4c1ee59700
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_or_request
, video_id
, note
='Downloading JSON metadata'):
45 'X-Udemy-Snail-Case': 'true',
46 'X-Requested-With': 'XMLHttpRequest',
48 for cookie
in self
._downloader
.cookiejar
:
49 if cookie
.name
== 'client_id':
50 headers
['X-Udemy-Client-Id'] = cookie
.value
51 elif cookie
.name
== 'access_token':
52 headers
['X-Udemy-Bearer-Token'] = cookie
.value
54 if isinstance(url_or_request
, compat_urllib_request
.Request
):
55 for header
, value
in headers
.items():
56 url_or_request
.add_header(header
, value
)
58 url_or_request
= compat_urllib_request
.Request(url_or_request
, headers
=headers
)
60 response
= super(UdemyIE
, self
)._download
_json
(url_or_request
, video_id
, note
)
61 self
._handle
_error
(response
)
64 def _real_initialize(self
):
68 (username
, password
) = self
._get
_login
_info
()
71 'Udemy account is required, use --username and --password options to provide account credentials.',
74 login_popup
= self
._download
_webpage
(
75 'https://www.udemy.com/join/login-popup?displayType=ajax&showSkipButton=1', None,
76 'Downloading login popup')
78 if login_popup
== '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
81 csrf
= self
._html
_search
_regex
(
82 r
'<input type="hidden" name="csrf" value="(.+?)"',
83 login_popup
, 'csrf token')
89 'displayType': 'json',
92 request
= compat_urllib_request
.Request(
93 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
).encode('utf-8'))
94 response
= self
._download
_json
(
95 request
, None, 'Logging in as %s' % username
)
97 if 'returnUrl' not in response
:
98 raise ExtractorError('Unable to log in')
102 def _real_extract(self
, url
):
103 mobj
= re
.match(self
._VALID
_URL
, url
)
104 lecture_id
= mobj
.group('id')
106 lecture
= self
._download
_json
(
107 'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id
,
108 lecture_id
, 'Downloading lecture JSON')
110 asset_type
= lecture
.get('assetType') or lecture
.get('asset_type')
111 if asset_type
!= 'Video':
112 raise ExtractorError(
113 'Lecture %s is not a video' % lecture_id
, expected
=True)
115 asset
= lecture
['asset']
117 stream_url
= asset
.get('streamUrl') or asset
.get('stream_url')
118 mobj
= re
.search(r
'(https?://www\.youtube\.com/watch\?v=.*)', stream_url
)
120 return self
.url_result(mobj
.group(1), 'Youtube')
122 video_id
= asset
['id']
123 thumbnail
= asset
.get('thumbnailUrl') or asset
.get('thumbnail_url')
124 duration
= asset
['data']['duration']
126 download_url
= asset
.get('downloadUrl') or asset
.get('download_url')
128 video
= download_url
.get('Video') or download_url
.get('video')
129 video_480p
= download_url
.get('Video480p') or download_url
.get('video_480p')
133 'url': video_480p
[0],
142 title
= lecture
['title']
143 description
= lecture
['description']
148 'description': description
,
149 'thumbnail': thumbnail
,
150 'duration': duration
,
155 class UdemyCourseIE(UdemyIE
):
156 IE_NAME
= 'udemy:course'
157 _VALID_URL
= r
'https?://www\.udemy\.com/(?P<coursepath>[\da-z-]+)'
158 _SUCCESSFULLY_ENROLLED
= '>You have enrolled in this course!<'
159 _ALREADY_ENROLLED
= '>You are already taking this course.<'
163 def suitable(cls
, url
):
164 return False if UdemyIE
.suitable(url
) else super(UdemyCourseIE
, cls
).suitable(url
)
166 def _real_extract(self
, url
):
167 mobj
= re
.match(self
._VALID
_URL
, url
)
168 course_path
= mobj
.group('coursepath')
170 response
= self
._download
_json
(
171 'https://www.udemy.com/api-1.1/courses/%s' % course_path
,
172 course_path
, 'Downloading course JSON')
174 course_id
= int(response
['id'])
175 course_title
= response
['title']
177 webpage
= self
._download
_webpage
(
178 'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id
,
179 course_id
, 'Enrolling in the course')
181 if self
._SUCCESSFULLY
_ENROLLED
in webpage
:
182 self
.to_screen('%s: Successfully enrolled in' % course_id
)
183 elif self
._ALREADY
_ENROLLED
in webpage
:
184 self
.to_screen('%s: Already enrolled in' % course_id
)
186 response
= self
._download
_json
(
187 'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id
,
188 course_id
, 'Downloading course curriculum')
192 'https://www.udemy.com/%s/#/lecture/%s' % (course_path
, asset
['id']), 'Udemy')
193 for asset
in response
if asset
.get('assetType') or asset
.get('asset_type') == 'Video'
196 return self
.playlist_result(entries
, course_id
, course_title
)