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