]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/lynda.py
[generic] Extract subtitles from video.js (#3156)
[yt-dlp.git] / yt_dlp / extractor / lynda.py
CommitLineData
a7c26e73
PH
1from __future__ import unicode_literals
2
c7f8537d 3import re
c7f8537d 4
5from .common import InfoExtractor
bdf16f81 6from ..compat import (
bdf16f81
S
7 compat_str,
8 compat_urlparse,
9)
1cc79574 10from ..utils import (
16ff7ebc
S
11 ExtractorError,
12 int_or_none,
6e6bc8da 13 urlencode_postdata,
7ee40b5d 14)
c7f8537d 15
16
30cbd4e0 17class LyndaBaseIE(InfoExtractor):
f0128230 18 _SIGNIN_URL = 'https://www.lynda.com/signin/lynda'
bdf16f81
S
19 _PASSWORD_URL = 'https://www.lynda.com/signin/password'
20 _USER_URL = 'https://www.lynda.com/signin/user'
30cbd4e0 21 _ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.'
499bfcbf 22 _NETRC_MACHINE = 'lynda'
30cbd4e0 23
bdf16f81
S
24 @staticmethod
25 def _check_error(json_string, key_or_keys):
26 keys = [key_or_keys] if isinstance(key_or_keys, compat_str) else key_or_keys
27 for key in keys:
28 error = json_string.get(key)
29 if error:
30 raise ExtractorError('Unable to login: %s' % error, expected=True)
31
52efa4b3 32 def _perform_login_step(self, form_html, fallback_action_url, extra_form_data, note, referrer_url):
bdf16f81
S
33 action_url = self._search_regex(
34 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', form_html,
35 'post url', default=fallback_action_url, group='url')
36
37 if not action_url.startswith('http'):
38 action_url = compat_urlparse.urljoin(self._SIGNIN_URL, action_url)
39
40 form_data = self._hidden_inputs(form_html)
41 form_data.update(extra_form_data)
42
5621c322
S
43 response = self._download_json(
44 action_url, None, note,
45 data=urlencode_postdata(form_data),
46 headers={
47 'Referer': referrer_url,
48 'X-Requested-With': 'XMLHttpRequest',
49 }, expected_status=(418, 500, ))
bdf16f81 50
5621c322 51 self._check_error(response, ('email', 'password', 'ErrorMessage'))
bdf16f81
S
52
53 return response, action_url
54
52efa4b3 55 def _perform_login(self, username, password):
bdf16f81
S
56 # Step 1: download signin page
57 signin_page = self._download_webpage(
58 self._SIGNIN_URL, None, 'Downloading signin page')
59
3841256c
S
60 # Already logged in
61 if any(re.search(p, signin_page) for p in (
ec85ded8 62 r'isLoggedIn\s*:\s*true', r'logout\.aspx', r'>Log out<')):
3841256c
S
63 return
64
bdf16f81
S
65 # Step 2: submit email
66 signin_form = self._search_regex(
67 r'(?s)(<form[^>]+data-form-name=["\']signin["\'][^>]*>.+?</form>)',
68 signin_page, 'signin form')
69 signin_page, signin_url = self._login_step(
70 signin_form, self._PASSWORD_URL, {'email': username},
71 'Submitting email', self._SIGNIN_URL)
72
73 # Step 3: submit password
74 password_form = signin_page['body']
75 self._login_step(
76 password_form, self._USER_URL, {'email': username, 'password': password},
77 'Submitting password', signin_url)
30cbd4e0
S
78
79
80class LyndaIE(LyndaBaseIE):
a7c26e73
PH
81 IE_NAME = 'lynda'
82 IE_DESC = 'lynda.com videos'
b7c74c04
S
83 _VALID_URL = r'''(?x)
84 https?://
85 (?:www\.)?(?:lynda\.com|educourse\.ga)/
86 (?:
87 (?:[^/]+/){2,3}(?P<course_id>\d+)|
88 player/embed
89 )/
90 (?P<id>\d+)
91 '''
7ee40b5d 92
7ee40b5d 93 _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
94
a0d64613 95 _TESTS = [{
68d9561c 96 'url': 'https://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
61a98b86 97 # md5 is unstable
ac260dd8 98 'info_dict': {
136db788
S
99 'id': '114408',
100 'ext': 'mp4',
a7c26e73
PH
101 'title': 'Using the exercise files',
102 'duration': 68
c7f8537d 103 }
a0d64613
S
104 }, {
105 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
106 'only_matching': True,
8c6919e4
S
107 }, {
108 'url': 'https://educourse.ga/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
109 'only_matching': True,
b7c74c04
S
110 }, {
111 'url': 'https://www.lynda.com/de/Graphic-Design-tutorials/Willkommen-Grundlagen-guten-Gestaltung/393570/393572-4.html',
112 'only_matching': True,
d89a0a80
S
113 }, {
114 # Status="NotFound", Message="Transcript not found"
115 'url': 'https://www.lynda.com/ASP-NET-tutorials/What-you-should-know/5034180/2811512-4.html',
116 'only_matching': True,
a0d64613 117 }]
7ee40b5d 118
6edfc40a
S
119 def _raise_unavailable(self, video_id):
120 self.raise_login_required(
121 'Video %s is only available for members' % video_id)
122
c7f8537d 123 def _real_extract(self, url):
5ad28e7f 124 mobj = self._match_valid_url(url)
6edfc40a
S
125 video_id = mobj.group('id')
126 course_id = mobj.group('course_id')
127
128 query = {
129 'videoId': video_id,
130 'type': 'video',
131 }
c7f8537d 132
ea8ed40b 133 video = self._download_json(
6edfc40a
S
134 'https://www.lynda.com/ajax/player', video_id,
135 'Downloading video JSON', fatal=False, query=query)
136
137 # Fallback scenario
138 if not video:
139 query['courseId'] = course_id
140
141 play = self._download_json(
142 'https://www.lynda.com/ajax/course/%s/%s/play'
143 % (course_id, video_id), video_id, 'Downloading play JSON')
144
145 if not play:
146 self._raise_unavailable(video_id)
147
148 formats = []
149 for formats_dict in play:
150 urls = formats_dict.get('urls')
151 if not isinstance(urls, dict):
152 continue
153 cdn = formats_dict.get('name')
154 for format_id, format_url in urls.items():
155 if not format_url:
156 continue
157 formats.append({
158 'url': format_url,
159 'format_id': '%s-%s' % (cdn, format_id) if cdn else format_id,
160 'height': int_or_none(format_id),
161 })
162 self._sort_formats(formats)
163
164 conviva = self._download_json(
165 'https://www.lynda.com/ajax/player/conviva', video_id,
166 'Downloading conviva JSON', query=query)
167
168 return {
169 'id': video_id,
170 'title': conviva['VideoTitle'],
171 'description': conviva.get('VideoDescription'),
172 'release_year': int_or_none(conviva.get('ReleaseYear')),
173 'duration': int_or_none(conviva.get('Duration')),
174 'creator': conviva.get('Author'),
175 'formats': formats,
176 }
c7f8537d 177
ea8ed40b 178 if 'Status' in video:
30cbd4e0 179 raise ExtractorError(
ea8ed40b 180 'lynda returned error: %s' % video['Message'], expected=True)
c7f8537d 181
ea8ed40b 182 if video.get('HasAccess') is False:
6edfc40a 183 self._raise_unavailable(video_id)
c7f8537d 184
ea8ed40b
S
185 video_id = compat_str(video.get('ID') or video_id)
186 duration = int_or_none(video.get('DurationInSeconds'))
187 title = video['Title']
c7f8537d 188
16ff7ebc
S
189 formats = []
190
ea8ed40b 191 fmts = video.get('Formats')
16ff7ebc 192 if fmts:
ea8ed40b
S
193 formats.extend([{
194 'url': f['Url'],
195 'ext': f.get('Extension'),
196 'width': int_or_none(f.get('Width')),
197 'height': int_or_none(f.get('Height')),
198 'filesize': int_or_none(f.get('FileSize')),
199 'format_id': compat_str(f.get('Resolution')) if f.get('Resolution') else None,
200 } for f in fmts if f.get('Url')])
201
202 prioritized_streams = video.get('PrioritizedStreams')
16ff7ebc 203 if prioritized_streams:
5a11b793 204 for prioritized_stream_id, prioritized_stream in prioritized_streams.items():
ea8ed40b
S
205 formats.extend([{
206 'url': video_url,
ea8aefd1 207 'height': int_or_none(format_id),
ea8ed40b
S
208 'format_id': '%s-%s' % (prioritized_stream_id, format_id),
209 } for format_id, video_url in prioritized_stream.items()])
c7f8537d 210
a57e8ce6 211 self._check_formats(formats, video_id)
c7f8537d 212 self._sort_formats(formats)
7ee40b5d 213
ea8ed40b 214 subtitles = self.extract_subtitles(video_id)
7ee40b5d 215
c7f8537d 216 return {
217 'id': video_id,
218 'title': title,
219 'duration': duration,
62bcfa8c 220 'subtitles': subtitles,
c7f8537d 221 'formats': formats
222 }
7ee40b5d 223
311c3938
JMF
224 def _fix_subtitles(self, subs):
225 srt = ''
7594be85 226 seq_counter = 0
311c3938
JMF
227 for pos in range(0, len(subs) - 1):
228 seq_current = subs[pos]
229 m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
230 if m_current is None:
231 continue
232 seq_next = subs[pos + 1]
233 m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
234 if m_next is None:
62bcfa8c 235 continue
311c3938
JMF
236 appear_time = m_current.group('timecode')
237 disappear_time = m_next.group('timecode')
7594be85
S
238 text = seq_current['Caption'].strip()
239 if text:
240 seq_counter += 1
241 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (seq_counter, appear_time, disappear_time, text)
311c3938
JMF
242 if srt:
243 return srt
244
ea8ed40b 245 def _get_subtitles(self, video_id):
68d9561c 246 url = 'https://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
d89a0a80
S
247 subs = self._download_webpage(
248 url, video_id, 'Downloading subtitles JSON', fatal=False)
249 if not subs or 'Status="NotFound"' in subs:
250 return {}
251 subs = self._parse_json(subs, video_id, fatal=False)
252 if not subs:
253 return {}
75ba0efb
S
254 fixed_subs = self._fix_subtitles(subs)
255 if fixed_subs:
256 return {'en': [{'ext': 'srt', 'data': fixed_subs}]}
d89a0a80 257 return {}
c7f8537d 258
259
30cbd4e0 260class LyndaCourseIE(LyndaBaseIE):
a7c26e73
PH
261 IE_NAME = 'lynda:course'
262 IE_DESC = 'lynda.com online courses'
c7f8537d 263
264 # Course link equals to welcome/introduction video link of same course
265 # We will recognize it as course link
b7c74c04
S
266 _VALID_URL = r'https?://(?:www|m)\.(?:lynda\.com|educourse\.ga)/(?P<coursepath>(?:[^/]+/){2,3}(?P<courseid>\d+))-2\.html'
267
268 _TESTS = [{
269 'url': 'https://www.lynda.com/Graphic-Design-tutorials/Grundlagen-guten-Gestaltung/393570-2.html',
270 'only_matching': True,
271 }, {
272 'url': 'https://www.lynda.com/de/Graphic-Design-tutorials/Grundlagen-guten-Gestaltung/393570-2.html',
273 'only_matching': True,
274 }]
c7f8537d 275
276 def _real_extract(self, url):
5ad28e7f 277 mobj = self._match_valid_url(url)
c7f8537d 278 course_path = mobj.group('coursepath')
279 course_id = mobj.group('courseid')
5f6a1245 280
f2980fdd
S
281 item_template = 'https://www.lynda.com/%s/%%s-4.html' % course_path
282
71bb0161 283 course = self._download_json(
68d9561c 284 'https://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
f2980fdd
S
285 course_id, 'Downloading course JSON', fatal=False)
286
287 if not course:
288 webpage = self._download_webpage(url, course_id)
289 entries = [
290 self.url_result(
291 item_template % video_id, ie=LyndaIE.ie_key(),
292 video_id=video_id)
293 for video_id in re.findall(
294 r'data-video-id=["\'](\d+)', webpage)]
295 return self.playlist_result(
296 entries, course_id,
297 self._og_search_title(webpage, fatal=False),
298 self._og_search_description(webpage))
c7f8537d 299
71bb0161 300 if course.get('Status') == 'NotFound':
30cbd4e0
S
301 raise ExtractorError(
302 'Course %s does not exist' % course_id, expected=True)
c7f8537d 303
304 unaccessible_videos = 0
00322ad4 305 entries = []
c7f8537d 306
16ff7ebc
S
307 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
308 # by single video API anymore
309
71bb0161
S
310 for chapter in course['Chapters']:
311 for video in chapter.get('Videos', []):
312 if video.get('HasAccess') is False:
c7f8537d 313 unaccessible_videos += 1
314 continue
00322ad4
S
315 video_id = video.get('ID')
316 if video_id:
317 entries.append({
318 '_type': 'url_transparent',
f2980fdd 319 'url': item_template % video_id,
00322ad4
S
320 'ie_key': LyndaIE.ie_key(),
321 'chapter': chapter.get('Title'),
322 'chapter_number': int_or_none(chapter.get('ChapterIndex')),
323 'chapter_id': compat_str(chapter.get('ID')),
324 })
c7f8537d 325
326 if unaccessible_videos > 0:
6a39ee13 327 self.report_warning(
30cbd4e0
S
328 '%s videos are only available for members (or paid members) and will not be downloaded. '
329 % unaccessible_videos + self._ACCOUNT_CREDENTIALS_HINT)
c7f8537d 330
71bb0161 331 course_title = course.get('Title')
04343588 332 course_description = course.get('Description')
c7f8537d 333
04343588 334 return self.playlist_result(entries, course_id, course_title, course_description)