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