]>
Commit | Line | Data |
---|---|---|
a7c26e73 PH |
1 | from __future__ import unicode_literals |
2 | ||
c7f8537d | 3 | import re |
4 | import json | |
5 | ||
6 | from .common import InfoExtractor | |
1cc79574 PH |
7 | from ..compat import ( |
8 | compat_str, | |
7ee40b5d | 9 | compat_urllib_parse, |
1cc79574 PH |
10 | ) |
11 | from ..utils import ( | |
16ff7ebc | 12 | ExtractorError, |
41dbc50f | 13 | clean_html, |
16ff7ebc | 14 | int_or_none, |
5c2266df | 15 | sanitized_Request, |
7ee40b5d | 16 | ) |
c7f8537d | 17 | |
18 | ||
30cbd4e0 S |
19 | class LyndaBaseIE(InfoExtractor): |
20 | _LOGIN_URL = 'https://www.lynda.com/login/login.aspx' | |
30cbd4e0 | 21 | _ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.' |
499bfcbf | 22 | _NETRC_MACHINE = 'lynda' |
30cbd4e0 S |
23 | |
24 | def _real_initialize(self): | |
25 | self._login() | |
26 | ||
27 | def _login(self): | |
0d85c3a7 | 28 | username, password = self._get_login_info() |
30cbd4e0 S |
29 | if username is None: |
30 | return | |
31 | ||
32 | login_form = { | |
2988835a S |
33 | 'username': username.encode('utf-8'), |
34 | 'password': password.encode('utf-8'), | |
30cbd4e0 S |
35 | 'remember': 'false', |
36 | 'stayPut': 'false' | |
37 | } | |
5c2266df | 38 | request = sanitized_Request( |
b4dea075 | 39 | self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8')) |
30cbd4e0 S |
40 | login_page = self._download_webpage( |
41 | request, None, 'Logging in as %s' % username) | |
42 | ||
43 | # Not (yet) logged in | |
5b7dab2d | 44 | m = re.search(r'loginResultJson\s*=\s*\'(?P<json>[^\']+)\';', login_page) |
30cbd4e0 S |
45 | if m is not None: |
46 | response = m.group('json') | |
47 | response_json = json.loads(response) | |
48 | state = response_json['state'] | |
49 | ||
50 | if state == 'notlogged': | |
51 | raise ExtractorError( | |
52 | 'Unable to login, incorrect username and/or password', | |
53 | expected=True) | |
54 | ||
55 | # This is when we get popup: | |
56 | # > You're already logged in to lynda.com on two devices. | |
57 | # > If you log in here, we'll log you out of another device. | |
58 | # So, we need to confirm this. | |
59 | if state == 'conflicted': | |
60 | confirm_form = { | |
61 | 'username': '', | |
62 | 'password': '', | |
63 | 'resolve': 'true', | |
64 | 'remember': 'false', | |
65 | 'stayPut': 'false', | |
66 | } | |
5c2266df | 67 | request = sanitized_Request( |
62cca96b | 68 | self._LOGIN_URL, compat_urllib_parse.urlencode(confirm_form).encode('utf-8')) |
30cbd4e0 S |
69 | login_page = self._download_webpage( |
70 | request, None, | |
71 | 'Confirming log in and log out from another device') | |
72 | ||
5b7dab2d | 73 | if all(not re.search(p, login_page) for p in ('isLoggedIn\s*:\s*true', r'logout\.aspx', r'>Log out<')): |
41dbc50f S |
74 | if 'login error' in login_page: |
75 | mobj = re.search( | |
76 | r'(?s)<h1[^>]+class="topmost">(?P<title>[^<]+)</h1>\s*<div>(?P<description>.+?)</div>', | |
77 | login_page) | |
78 | if mobj: | |
79 | raise ExtractorError( | |
80 | 'lynda returned error: %s - %s' | |
81 | % (mobj.group('title'), clean_html(mobj.group('description'))), | |
82 | expected=True) | |
30cbd4e0 S |
83 | raise ExtractorError('Unable to log in') |
84 | ||
179ffab6 | 85 | def _logout(self): |
903d1369 S |
86 | username, _ = self._get_login_info() |
87 | if username is None: | |
88 | return | |
89 | ||
179ffab6 S |
90 | self._download_webpage( |
91 | 'http://www.lynda.com/ajax/logout.aspx', None, | |
92 | 'Logging out', 'Unable to log out', fatal=False) | |
93 | ||
30cbd4e0 S |
94 | |
95 | class LyndaIE(LyndaBaseIE): | |
a7c26e73 PH |
96 | IE_NAME = 'lynda' |
97 | IE_DESC = 'lynda.com videos' | |
30cbd4e0 | 98 | _VALID_URL = r'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(?P<id>\d+)' |
7ee40b5d | 99 | _NETRC_MACHINE = 'lynda' |
100 | ||
7ee40b5d | 101 | _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]' |
102 | ||
a0d64613 | 103 | _TESTS = [{ |
a7c26e73 | 104 | 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html', |
a7c26e73 | 105 | 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1', |
ac260dd8 | 106 | 'info_dict': { |
136db788 S |
107 | 'id': '114408', |
108 | 'ext': 'mp4', | |
a7c26e73 PH |
109 | 'title': 'Using the exercise files', |
110 | 'duration': 68 | |
c7f8537d | 111 | } |
a0d64613 S |
112 | }, { |
113 | 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0', | |
114 | 'only_matching': True, | |
115 | }] | |
7ee40b5d | 116 | |
c7f8537d | 117 | def _real_extract(self, url): |
30cbd4e0 | 118 | video_id = self._match_id(url) |
c7f8537d | 119 | |
ea8ed40b | 120 | video = self._download_json( |
30cbd4e0 S |
121 | 'http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id, |
122 | video_id, 'Downloading video JSON') | |
c7f8537d | 123 | |
ea8ed40b | 124 | if 'Status' in video: |
30cbd4e0 | 125 | raise ExtractorError( |
ea8ed40b | 126 | 'lynda returned error: %s' % video['Message'], expected=True) |
c7f8537d | 127 | |
ea8ed40b | 128 | if video.get('HasAccess') is False: |
62984e45 | 129 | self.raise_login_required('Video %s is only available for members' % video_id) |
c7f8537d | 130 | |
ea8ed40b S |
131 | video_id = compat_str(video.get('ID') or video_id) |
132 | duration = int_or_none(video.get('DurationInSeconds')) | |
133 | title = video['Title'] | |
c7f8537d | 134 | |
16ff7ebc S |
135 | formats = [] |
136 | ||
ea8ed40b | 137 | fmts = video.get('Formats') |
16ff7ebc | 138 | if fmts: |
ea8ed40b S |
139 | formats.extend([{ |
140 | 'url': f['Url'], | |
141 | 'ext': f.get('Extension'), | |
142 | 'width': int_or_none(f.get('Width')), | |
143 | 'height': int_or_none(f.get('Height')), | |
144 | 'filesize': int_or_none(f.get('FileSize')), | |
145 | 'format_id': compat_str(f.get('Resolution')) if f.get('Resolution') else None, | |
146 | } for f in fmts if f.get('Url')]) | |
147 | ||
148 | prioritized_streams = video.get('PrioritizedStreams') | |
16ff7ebc | 149 | if prioritized_streams: |
5a11b793 | 150 | for prioritized_stream_id, prioritized_stream in prioritized_streams.items(): |
ea8ed40b S |
151 | formats.extend([{ |
152 | 'url': video_url, | |
153 | 'width': int_or_none(format_id), | |
154 | 'format_id': '%s-%s' % (prioritized_stream_id, format_id), | |
155 | } for format_id, video_url in prioritized_stream.items()]) | |
c7f8537d | 156 | |
a57e8ce6 | 157 | self._check_formats(formats, video_id) |
c7f8537d | 158 | self._sort_formats(formats) |
7ee40b5d | 159 | |
ea8ed40b | 160 | subtitles = self.extract_subtitles(video_id) |
7ee40b5d | 161 | |
c7f8537d | 162 | return { |
163 | 'id': video_id, | |
164 | 'title': title, | |
165 | 'duration': duration, | |
62bcfa8c | 166 | 'subtitles': subtitles, |
c7f8537d | 167 | 'formats': formats |
168 | } | |
7ee40b5d | 169 | |
311c3938 JMF |
170 | def _fix_subtitles(self, subs): |
171 | srt = '' | |
7594be85 | 172 | seq_counter = 0 |
311c3938 JMF |
173 | for pos in range(0, len(subs) - 1): |
174 | seq_current = subs[pos] | |
175 | m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode']) | |
176 | if m_current is None: | |
177 | continue | |
178 | seq_next = subs[pos + 1] | |
179 | m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode']) | |
180 | if m_next is None: | |
62bcfa8c | 181 | continue |
311c3938 JMF |
182 | appear_time = m_current.group('timecode') |
183 | disappear_time = m_next.group('timecode') | |
7594be85 S |
184 | text = seq_current['Caption'].strip() |
185 | if text: | |
186 | seq_counter += 1 | |
187 | srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (seq_counter, appear_time, disappear_time, text) | |
311c3938 JMF |
188 | if srt: |
189 | return srt | |
190 | ||
ea8ed40b | 191 | def _get_subtitles(self, video_id): |
62bcfa8c | 192 | url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id |
311c3938 JMF |
193 | subs = self._download_json(url, None, False) |
194 | if subs: | |
195 | return {'en': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]} | |
196 | else: | |
197 | return {} | |
c7f8537d | 198 | |
199 | ||
30cbd4e0 | 200 | class LyndaCourseIE(LyndaBaseIE): |
a7c26e73 PH |
201 | IE_NAME = 'lynda:course' |
202 | IE_DESC = 'lynda.com online courses' | |
c7f8537d | 203 | |
204 | # Course link equals to welcome/introduction video link of same course | |
205 | # We will recognize it as course link | |
206 | _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html' | |
207 | ||
208 | def _real_extract(self, url): | |
209 | mobj = re.match(self._VALID_URL, url) | |
210 | course_path = mobj.group('coursepath') | |
211 | course_id = mobj.group('courseid') | |
5f6a1245 | 212 | |
71bb0161 | 213 | course = self._download_json( |
30cbd4e0 S |
214 | 'http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id, |
215 | course_id, 'Downloading course JSON') | |
c7f8537d | 216 | |
179ffab6 S |
217 | self._logout() |
218 | ||
71bb0161 | 219 | if course.get('Status') == 'NotFound': |
30cbd4e0 S |
220 | raise ExtractorError( |
221 | 'Course %s does not exist' % course_id, expected=True) | |
c7f8537d | 222 | |
223 | unaccessible_videos = 0 | |
224 | videos = [] | |
225 | ||
16ff7ebc S |
226 | # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided |
227 | # by single video API anymore | |
228 | ||
71bb0161 S |
229 | for chapter in course['Chapters']: |
230 | for video in chapter.get('Videos', []): | |
231 | if video.get('HasAccess') is False: | |
c7f8537d | 232 | unaccessible_videos += 1 |
233 | continue | |
ae4ddf9e S |
234 | if video.get('ID'): |
235 | videos.append(video['ID']) | |
c7f8537d | 236 | |
237 | if unaccessible_videos > 0: | |
30cbd4e0 S |
238 | self._downloader.report_warning( |
239 | '%s videos are only available for members (or paid members) and will not be downloaded. ' | |
240 | % unaccessible_videos + self._ACCOUNT_CREDENTIALS_HINT) | |
c7f8537d | 241 | |
a7c26e73 | 242 | entries = [ |
30cbd4e0 S |
243 | self.url_result( |
244 | 'http://www.lynda.com/%s/%s-4.html' % (course_path, video_id), | |
245 | 'Lynda') | |
a7c26e73 | 246 | for video_id in videos] |
c7f8537d | 247 | |
71bb0161 | 248 | course_title = course.get('Title') |
c7f8537d | 249 | |
5f6a1245 | 250 | return self.playlist_result(entries, course_id, course_title) |