aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/udemy.py
blob: 59832b1ece75d480afdfa16c3c398a701f532a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from __future__ import unicode_literals

from .common import InfoExtractor
from ..compat import (
    compat_HTTPError,
    compat_urllib_parse,
    compat_urllib_request,
)
from ..utils import (
    ExtractorError,
    float_or_none,
    int_or_none,
    sanitized_Request,
)


class UdemyIE(InfoExtractor):
    IE_NAME = 'udemy'
    _VALID_URL = r'https?://www\.udemy\.com/(?:[^#]+#/lecture/|lecture/view/?\?lectureId=)(?P<id>\d+)'
    _LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&showSkipButton=1'
    _ORIGIN_URL = 'https://www.udemy.com'
    _SUCCESSFULLY_ENROLLED = '>You have enrolled in this course!<'
    _ALREADY_ENROLLED = '>You are already taking this course.<'
    _NETRC_MACHINE = 'udemy'

    _TESTS = [{
        'url': 'https://www.udemy.com/java-tutorial/#/lecture/172757',
        'md5': '98eda5b657e752cf945d8445e261b5c5',
        'info_dict': {
            'id': '160614',
            'ext': 'mp4',
            'title': 'Introduction and Installation',
            'description': 'md5:c0d51f6f21ef4ec65f091055a5eef876',
            'duration': 579.29,
        },
        'skip': 'Requires udemy account credentials',
    }]

    def _enroll_course(self, webpage, course_id):
        enroll_url = self._search_regex(
            r'href=(["\'])(?P<url>https?://(?:www\.)?udemy\.com/course/subscribe/.+?)\1',
            webpage, 'enroll url', group='url',
            default='https://www.udemy.com/course/subscribe/?courseId=%s' % course_id)
        webpage = self._download_webpage(enroll_url, course_id, 'Enrolling in the course')
        if self._SUCCESSFULLY_ENROLLED in webpage:
            self.to_screen('%s: Successfully enrolled in' % course_id)
        elif self._ALREADY_ENROLLED in webpage:
            self.to_screen('%s: Already enrolled in' % course_id)

    def _download_lecture(self, course_id, lecture_id):
        return self._download_json(
            'https://www.udemy.com/api-2.0/users/me/subscribed-courses/%s/lectures/%s?%s' % (
                course_id, lecture_id, compat_urllib_parse.urlencode({
                    'video_only': '',
                    'auto_play': '',
                    'fields[lecture]': 'title,description,asset',
                    'fields[asset]': 'asset_type,stream_url,thumbnail_url,download_urls,data',
                    'instructorPreviewMode': 'False',
                })),
            lecture_id, 'Downloading lecture JSON')

    def _handle_error(self, response):
        if not isinstance(response, dict):
            return
        error = response.get('error')
        if error:
            error_str = 'Udemy returned error #%s: %s' % (error.get('code'), error.get('message'))
            error_data = error.get('data')
            if error_data:
                error_str += ' - %s' % error_data.get('formErrors')
            raise ExtractorError(error_str, expected=True)

    def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata'):
        headers = {
            'X-Udemy-Snail-Case': 'true',
            'X-Requested-With': 'XMLHttpRequest',
        }
        for cookie in self._downloader.cookiejar:
            if cookie.name == 'client_id':
                headers['X-Udemy-Client-Id'] = cookie.value
            elif cookie.name == 'access_token':
                headers['X-Udemy-Bearer-Token'] = cookie.value
                headers['X-Udemy-Authorization'] = 'Bearer %s' % cookie.value

        if isinstance(url_or_request, compat_urllib_request.Request):
            for header, value in headers.items():
                url_or_request.add_header(header, value)
        else:
            url_or_request = sanitized_Request(url_or_request, headers=headers)

        response = super(UdemyIE, self)._download_json(url_or_request, video_id, note)
        self._handle_error(response)
        return response

    def _real_initialize(self):
        self._login()

    def _login(self):
        (username, password) = self._get_login_info()
        if username is None:
            return

        login_popup = self._download_webpage(
            self._LOGIN_URL, None, 'Downloading login popup')

        def is_logged(webpage):
            return any(p in webpage for p in ['href="https://www.udemy.com/user/logout/', '>Logout<'])

        # already logged in
        if is_logged(login_popup):
            return

        login_form = self._form_hidden_inputs('login-form', login_popup)

        login_form.update({
            'email': username.encode('utf-8'),
            'password': password.encode('utf-8'),
        })

        request = sanitized_Request(
            self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
        request.add_header('Referer', self._ORIGIN_URL)
        request.add_header('Origin', self._ORIGIN_URL)

        response = self._download_webpage(
            request, None, 'Logging in as %s' % username)

        if not is_logged(response):
            error = self._html_search_regex(
                r'(?s)<div[^>]+class="form-errors[^"]*">(.+?)</div>',
                response, 'error message', default=None)
            if error:
                raise ExtractorError('Unable to login: %s' % error, expected=True)
            raise ExtractorError('Unable to log in')

    def _real_extract(self, url):
        lecture_id = self._match_id(url)

        webpage = self._download_webpage(url, lecture_id)

        course_id = self._search_regex(
            r'data-course-id=["\'](\d+)', webpage, 'course id')

        try:
            lecture = self._download_lecture(course_id, lecture_id)
        except ExtractorError as e:
            # Error could possibly mean we are not enrolled in the course
            if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
                self._enroll_course(webpage, course_id)
                lecture_id = self._download_lecture(course_id, lecture_id)
            else:
                raise

        title = lecture['title']
        description = lecture.get('description')

        asset = lecture['asset']

        asset_type = asset.get('assetType') or asset.get('asset_type')
        if asset_type != 'Video':
            raise ExtractorError(
                'Lecture %s is not a video' % lecture_id, expected=True)

        stream_url = asset.get('streamUrl') or asset.get('stream_url')
        if stream_url:
            youtube_url = self._search_regex(
                r'(https?://www\.youtube\.com/watch\?v=.*)', stream_url, 'youtube URL', default=None)
            if youtube_url:
                return self.url_result(youtube_url, 'Youtube')

        video_id = asset['id']
        thumbnail = asset.get('thumbnailUrl') or asset.get('thumbnail_url')
        duration = float_or_none(asset.get('data', {}).get('duration'))
        outputs = asset.get('data', {}).get('outputs', {})

        formats = []
        for format_ in asset.get('download_urls', {}).get('Video', []):
            video_url = format_.get('file')
            if not video_url:
                continue
            format_id = format_.get('label')
            f = {
                'url': format_['file'],
                'height': int_or_none(format_id),
            }
            if format_id:
                # Some videos contain additional metadata (e.g.
                # https://www.udemy.com/ios9-swift/learn/#/lecture/3383208)
                output = outputs.get(format_id)
                if isinstance(output, dict):
                    f.update({
                        'format_id': '%sp' % (output.get('label') or format_id),
                        'width': int_or_none(output.get('width')),
                        'height': int_or_none(output.get('height')),
                        'vbr': int_or_none(output.get('video_bitrate_in_kbps')),
                        'vcodec': output.get('video_codec'),
                        'fps': int_or_none(output.get('frame_rate')),
                        'abr': int_or_none(output.get('audio_bitrate_in_kbps')),
                        'acodec': output.get('audio_codec'),
                        'asr': int_or_none(output.get('audio_sample_rate')),
                        'tbr': int_or_none(output.get('total_bitrate_in_kbps')),
                        'filesize': int_or_none(output.get('file_size_in_bytes')),
                    })
                else:
                    f['format_id'] = '%sp' % format_id
            formats.append(f)

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': title,
            'description': description,
            'thumbnail': thumbnail,
            'duration': duration,
            'formats': formats
        }


class UdemyCourseIE(UdemyIE):
    IE_NAME = 'udemy:course'
    _VALID_URL = r'https?://www\.udemy\.com/(?P<id>[\da-z-]+)'
    _TESTS = []

    @classmethod
    def suitable(cls, url):
        return False if UdemyIE.suitable(url) else super(UdemyCourseIE, cls).suitable(url)

    def _real_extract(self, url):
        course_path = self._match_id(url)

        webpage = self._download_webpage(url, course_path)

        response = self._download_json(
            'https://www.udemy.com/api-1.1/courses/%s' % course_path,
            course_path, 'Downloading course JSON')

        course_id = response['id']
        course_title = response.get('title')

        self._enroll_course(webpage, course_id)

        response = self._download_json(
            'https://www.udemy.com/api-1.1/courses/%s/curriculum' % course_id,
            course_id, 'Downloading course curriculum')

        entries = [
            self.url_result(
                'https://www.udemy.com/%s/#/lecture/%s' % (course_path, asset['id']), 'Udemy')
            for asset in response if asset.get('assetType') or asset.get('asset_type') == 'Video'
        ]

        return self.playlist_result(entries, course_id, course_title)