1 from __future__ import unicode_literals
6 from .common import InfoExtractor
10 compat_urllib_request,
18 class LyndaBaseIE(InfoExtractor):
19 _LOGIN_URL = 'https://www.lynda.com/login/login.aspx'
20 _ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.'
21 _NETRC_MACHINE = 'lynda'
23 def _real_initialize(self):
27 (username, password) = self._get_login_info()
32 'username': username.encode('utf-8'),
33 'password': password.encode('utf-8'),
37 request = compat_urllib_request.Request(
38 self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
39 login_page = self._download_webpage(
40 request, None, 'Logging in as %s' % username)
43 m = re.search(r'loginResultJson\s*=\s*\'(?P<json>[^\']+)\';', login_page)
45 response = m.group('json')
46 response_json = json.loads(response)
47 state = response_json['state']
49 if state == 'notlogged':
51 'Unable to login, incorrect username and/or password',
54 # This is when we get popup:
55 # > You're already logged in to lynda.com on two devices.
56 # > If you log in here, we'll log you out of another device.
57 # So, we need to confirm this.
58 if state == 'conflicted':
66 request = compat_urllib_request.Request(
67 self._LOGIN_URL, compat_urllib_parse.urlencode(confirm_form).encode('utf-8'))
68 login_page = self._download_webpage(
70 'Confirming log in and log out from another device')
72 if all(not re.search(p, login_page) for p in ('isLoggedIn\s*:\s*true', r'logout\.aspx', r'>Log out<')):
73 raise ExtractorError('Unable to log in')
76 class LyndaIE(LyndaBaseIE):
78 IE_DESC = 'lynda.com videos'
79 _VALID_URL = r'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(?P<id>\d+)'
80 _NETRC_MACHINE = 'lynda'
82 _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
85 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
86 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
90 'title': 'Using the exercise files',
94 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
95 'only_matching': True,
98 def _real_extract(self, url):
99 video_id = self._match_id(url)
101 page = self._download_webpage(
102 'http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id,
103 video_id, 'Downloading video JSON')
104 video_json = json.loads(page)
106 if 'Status' in video_json:
107 raise ExtractorError(
108 'lynda returned error: %s' % video_json['Message'], expected=True)
110 if video_json['HasAccess'] is False:
111 raise ExtractorError(
112 'Video %s is only available for members. '
113 % video_id + self._ACCOUNT_CREDENTIALS_HINT, expected=True)
115 video_id = compat_str(video_json['ID'])
116 duration = video_json['DurationInSeconds']
117 title = video_json['Title']
121 fmts = video_json.get('Formats')
126 'ext': fmt['Extension'],
127 'width': fmt['Width'],
128 'height': fmt['Height'],
129 'filesize': fmt['FileSize'],
130 'format_id': str(fmt['Resolution'])
133 prioritized_streams = video_json.get('PrioritizedStreams')
134 if prioritized_streams:
138 'width': int_or_none(format_id),
139 'format_id': format_id,
140 } for format_id, video_url in prioritized_streams['0'].items()
143 self._check_formats(formats, video_id)
144 self._sort_formats(formats)
146 subtitles = self.extract_subtitles(video_id, page)
151 'duration': duration,
152 'subtitles': subtitles,
156 def _fix_subtitles(self, subs):
159 for pos in range(0, len(subs) - 1):
160 seq_current = subs[pos]
161 m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
162 if m_current is None:
164 seq_next = subs[pos + 1]
165 m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
168 appear_time = m_current.group('timecode')
169 disappear_time = m_next.group('timecode')
170 text = seq_current['Caption'].strip()
173 srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (seq_counter, appear_time, disappear_time, text)
177 def _get_subtitles(self, video_id, webpage):
178 url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
179 subs = self._download_json(url, None, False)
181 return {'en': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]}
186 class LyndaCourseIE(LyndaBaseIE):
187 IE_NAME = 'lynda:course'
188 IE_DESC = 'lynda.com online courses'
190 # Course link equals to welcome/introduction video link of same course
191 # We will recognize it as course link
192 _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
194 def _real_extract(self, url):
195 mobj = re.match(self._VALID_URL, url)
196 course_path = mobj.group('coursepath')
197 course_id = mobj.group('courseid')
199 page = self._download_webpage(
200 'http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
201 course_id, 'Downloading course JSON')
202 course_json = json.loads(page)
204 if 'Status' in course_json and course_json['Status'] == 'NotFound':
205 raise ExtractorError(
206 'Course %s does not exist' % course_id, expected=True)
208 unaccessible_videos = 0
211 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
212 # by single video API anymore
214 for chapter in course_json['Chapters']:
215 for video in chapter['Videos']:
216 if video['HasAccess'] is False:
217 unaccessible_videos += 1
219 videos.append(video['ID'])
221 if unaccessible_videos > 0:
222 self._downloader.report_warning(
223 '%s videos are only available for members (or paid members) and will not be downloaded. '
224 % unaccessible_videos + self._ACCOUNT_CREDENTIALS_HINT)
228 'http://www.lynda.com/%s/%s-4.html' % (course_path, video_id),
230 for video_id in videos]
232 course_title = course_json['Title']
234 return self.playlist_result(entries, course_id, course_title)