1 from __future__ import unicode_literals
6 from .common import InfoExtractor
10 compat_urllib_request,
18 class LyndaIE(InfoExtractor):
20 IE_DESC = 'lynda.com videos'
21 _VALID_URL = r'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(\d+)'
22 _LOGIN_URL = 'https://www.lynda.com/login/login.aspx'
23 _NETRC_MACHINE = 'lynda'
25 _SUCCESSFUL_LOGIN_REGEX = r'isLoggedIn: true'
26 _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
28 ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.'
31 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
32 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
36 'title': 'Using the exercise files',
40 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
41 'only_matching': True,
44 def _real_initialize(self):
47 def _real_extract(self, url):
48 mobj = re.match(self._VALID_URL, url)
49 video_id = mobj.group(1)
51 page = self._download_webpage('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id, video_id,
52 'Downloading video JSON')
53 video_json = json.loads(page)
55 if 'Status' in video_json:
56 raise ExtractorError('lynda returned error: %s' % video_json['Message'], expected=True)
58 if video_json['HasAccess'] is False:
60 'Video %s is only available for members. ' % video_id + self.ACCOUNT_CREDENTIALS_HINT, expected=True)
62 video_id = compat_str(video_json['ID'])
63 duration = video_json['DurationInSeconds']
64 title = video_json['Title']
68 fmts = video_json.get('Formats')
73 'ext': fmt['Extension'],
74 'width': fmt['Width'],
75 'height': fmt['Height'],
76 'filesize': fmt['FileSize'],
77 'format_id': str(fmt['Resolution'])
80 prioritized_streams = video_json.get('PrioritizedStreams')
81 if prioritized_streams:
85 'width': int_or_none(format_id),
86 'format_id': format_id,
87 } for format_id, video_url in prioritized_streams['0'].items()
90 self._check_formats(formats, video_id)
91 self._sort_formats(formats)
93 subtitles = self.extract_subtitles(video_id, page)
99 'subtitles': subtitles,
104 (username, password) = self._get_login_info()
109 'username': username,
110 'password': password,
114 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
115 login_page = self._download_webpage(request, None, 'Logging in as %s' % username)
117 # Not (yet) logged in
118 m = re.search(r'loginResultJson = \'(?P<json>[^\']+)\';', login_page)
120 response = m.group('json')
121 response_json = json.loads(response)
122 state = response_json['state']
124 if state == 'notlogged':
125 raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
127 # This is when we get popup:
128 # > You're already logged in to lynda.com on two devices.
129 # > If you log in here, we'll log you out of another device.
130 # So, we need to confirm this.
131 if state == 'conflicted':
139 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(confirm_form))
140 login_page = self._download_webpage(request, None, 'Confirming log in and log out from another device')
142 if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
143 raise ExtractorError('Unable to log in')
145 def _fix_subtitles(self, subs):
147 for pos in range(0, len(subs) - 1):
148 seq_current = subs[pos]
149 m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
150 if m_current is None:
152 seq_next = subs[pos + 1]
153 m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
156 appear_time = m_current.group('timecode')
157 disappear_time = m_next.group('timecode')
158 text = seq_current['Caption'].lstrip()
159 srt += '%s\r\n%s --> %s\r\n%s' % (str(pos), appear_time, disappear_time, text)
163 def _get_subtitles(self, video_id, webpage):
164 url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
165 subs = self._download_json(url, None, False)
167 return {'en': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]}
172 class LyndaCourseIE(InfoExtractor):
173 IE_NAME = 'lynda:course'
174 IE_DESC = 'lynda.com online courses'
176 # Course link equals to welcome/introduction video link of same course
177 # We will recognize it as course link
178 _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
180 def _real_extract(self, url):
181 mobj = re.match(self._VALID_URL, url)
182 course_path = mobj.group('coursepath')
183 course_id = mobj.group('courseid')
185 page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
186 course_id, 'Downloading course JSON')
187 course_json = json.loads(page)
189 if 'Status' in course_json and course_json['Status'] == 'NotFound':
190 raise ExtractorError('Course %s does not exist' % course_id, expected=True)
192 unaccessible_videos = 0
194 (username, _) = self._get_login_info()
196 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
197 # by single video API anymore
199 for chapter in course_json['Chapters']:
200 for video in chapter['Videos']:
201 if username is None and video['HasAccess'] is False:
202 unaccessible_videos += 1
204 videos.append(video['ID'])
206 if unaccessible_videos > 0:
207 self._downloader.report_warning('%s videos are only available for members and will not be downloaded. '
208 % unaccessible_videos + LyndaIE.ACCOUNT_CREDENTIALS_HINT)
211 self.url_result('http://www.lynda.com/%s/%s-4.html' %
212 (course_path, video_id),
214 for video_id in videos]
216 course_title = course_json['Title']
218 return self.playlist_result(entries, course_id, course_title)