[shahid] add default fallbacks for extracting api vars
[youtube-dl] / youtube_dl / extractor / udemy.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse,
8     compat_urllib_request,
9 )
10 from ..utils import (
11     ExtractorError,
12 )
13
14
15 class UdemyIE(InfoExtractor):
16     IE_NAME = 'udemy'
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'
21
22     _TESTS = [{
23         'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
24         'md5': '98eda5b657e752cf945d8445e261b5c5',
25         'info_dict': {
26             'id': '160614',
27             'ext': 'mp4',
28             'title': 'Introduction and Installation',
29             'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
30             'duration': 579.29,
31         },
32         'skip': 'Requires udemy account credentials',
33     }]
34
35     def _handle_error(self, response):
36         if not isinstance(response, dict):
37             return
38         error = response.get('error')
39         if error:
40             error_str = 'Udemy returned error #%s: %s' % (error.get('code'), error.get('message'))
41             error_data = error.get('data')
42             if error_data:
43                 error_str += ' - %s' % error_data.get('formErrors')
44             raise ExtractorError(error_str, expected=True)
45
46     def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
47         headers = {
48             'X-Udemy-Snail-Case': 'true',
49             'X-Requested-With': 'XMLHttpRequest',
50         }
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
56
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)
60         else:
61             url_or_request = compat_urllib_request.Request(url_or_request, headers=headers)
62
63         response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
64         self._handle_error(response)
65         return response
66
67     def _real_initialize(self):
68         self._login()
69
70     def _login(self):
71         (username, password) = self._get_login_info()
72         if username is None:
73             raise ExtractorError(
74                 'Udemy account is required, use --username and --password options to provide account credentials.',
75                 expected=True)
76
77         login_popup = self._download_webpage(
78             self._LOGIN_URL, None, 'Downloading login popup')
79
80         if login_popup == '<div class="run-command close-popup redirect" data-url="https://www.udemy.com/"></div>':
81             return
82
83         login_form = self._form_hidden_inputs('login-form', login_popup)
84
85         login_form.update({
86             'email': username.encode('utf-8'),
87             'password': password.encode('utf-8'),
88         })
89
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)
94
95         response = self._download_webpage(
96             request, None, 'Logging in as %s' % username)
97
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)
103             if error:
104                 raise ExtractorError('Unable to login: %s' % error, expected=True)
105             raise ExtractorError('Unable to log in')
106
107     def _real_extract(self, url):
108         lecture_id = self._match_id(url)
109
110         lecture = self._download_json(
111             'https://www.udemy.com/api-1.1/lectures/%s' % lecture_id,
112             lecture_id, 'Downloading lecture JSON')
113
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)
118
119         asset = lecture['asset']
120
121         stream_url = asset.get('streamUrl') or asset.get('stream_url')
122         mobj = re.search(r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url)
123         if mobj:
124             return self.url_result(mobj.group(1), 'Youtube')
125
126         video_id = asset['id']
127         thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
128         duration = asset['data']['duration']
129
130         download_url = asset.get('downloadUrl') or asset.get('download_url')
131
132         video = download_url.get('Video') or download_url.get('video')
133         video_480p = download_url.get('Video480p') or download_url.get('video_480p')
134
135         formats = [
136             {
137                 'url': video_480p[0],
138                 'format_id': '360p',
139             },
140             {
141                 'url': video[0],
142                 'format_id': '720p',
143             },
144         ]
145
146         title = lecture['title']
147         description = lecture['description']
148
149         return {
150             'id': video_id,
151             'title': title,
152             'description': description,
153             'thumbnail': thumbnail,
154             'duration': duration,
155             'formats': formats
156         }
157
158
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.<'
164     _TESTS = []
165
166     @classmethod
167     def suitable(cls, url):
168         return False if UdemyIE.suitable(url) else super(UdemyCourseIE, cls).suitable(url)
169
170     def _real_extract(self, url):
171         mobj = re.match(self._VALID_URL, url)
172         course_path = mobj.group('coursepath')
173
174         response = self._download_json(
175             'https://www.udemy.com/api-1.1/courses/%s' % course_path,
176             course_path, 'Downloading course JSON')
177
178         course_id = int(response['id'])
179         course_title = response['title']
180
181         webpage = self._download_webpage(
182             'https://www.udemy.com/course/subscribe/?courseId=%s' % course_id,
183             course_id, 'Enrolling in the course')
184
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)
189
190         response = self._download_json(
191             'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
192             course_id, 'Downloading course curriculum')
193
194         entries = [
195             self.url_result(
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'
198         ]
199
200         return self.playlist_result(entries, course_id, course_title)