1 from __future__
import unicode_literals
6 from .common
import InfoExtractor
19 class LyndaBaseIE(InfoExtractor
):
20 _LOGIN_URL
= 'https://www.lynda.com/login/login.aspx'
21 _ACCOUNT_CREDENTIALS_HINT
= 'Use --username and --password options to provide lynda.com account credentials.'
22 _NETRC_MACHINE
= 'lynda'
24 def _real_initialize(self
):
28 username
, password
= self
._get
_login
_info
()
33 'username': username
.encode('utf-8'),
34 'password': password
.encode('utf-8'),
38 request
= sanitized_Request(
39 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
).encode('utf-8'))
40 login_page
= self
._download
_webpage
(
41 request
, None, 'Logging in as %s' % username
)
44 m
= re
.search(r
'loginResultJson\s*=\s*\'(?P
<json
>[^
\']+)\';', login_page)
46 response = m.group('json
')
47 response_json = json.loads(response)
48 state = response_json['state
']
50 if state == 'notlogged
':
52 'Unable to login
, incorrect username
and/or password
',
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':
67 request
= sanitized_Request(
68 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(confirm_form
).encode('utf-8'))
69 login_page
= self
._download
_webpage
(
71 'Confirming log in and log out from another device')
73 if all(not re
.search(p
, login_page
) for p
in ('isLoggedIn\s*:\s*true', r
'logout\.aspx', r
'>Log out<')):
74 if 'login error' in login_page
:
76 r
'(?s)<h1[^>]+class="topmost">(?P<title>[^<]+)</h1>\s*<div>(?P<description>.+?)</div>',
80 'lynda returned error: %s - %s'
81 % (mobj
.group('title'), clean_html(mobj
.group('description'))),
83 raise ExtractorError('Unable to log in')
86 username
, _
= self
._get
_login
_info
()
90 self
._download
_webpage
(
91 'http://www.lynda.com/ajax/logout.aspx', None,
92 'Logging out', 'Unable to log out', fatal
=False)
95 class LyndaIE(LyndaBaseIE
):
97 IE_DESC
= 'lynda.com videos'
98 _VALID_URL
= r
'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(?P<id>\d+)'
99 _NETRC_MACHINE
= 'lynda'
101 _TIMECODE_REGEX
= r
'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
104 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
105 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
109 'title': 'Using the exercise files',
113 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
114 'only_matching': True,
117 def _real_extract(self
, url
):
118 video_id
= self
._match
_id
(url
)
120 video
= self
._download
_json
(
121 'http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id
,
122 video_id
, 'Downloading video JSON')
124 if 'Status' in video
:
125 raise ExtractorError(
126 'lynda returned error: %s' % video
['Message'], expected
=True)
128 if video
.get('HasAccess') is False:
129 self
.raise_login_required('Video %s is only available for members' % video_id
)
131 video_id
= compat_str(video
.get('ID') or video_id
)
132 duration
= int_or_none(video
.get('DurationInSeconds'))
133 title
= video
['Title']
137 fmts
= video
.get('Formats')
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')])
148 prioritized_streams
= video
.get('PrioritizedStreams')
149 if prioritized_streams
:
150 for prioritized_stream_id
, prioritized_stream
in prioritized_streams
.items():
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()])
157 self
._check
_formats
(formats
, video_id
)
158 self
._sort
_formats
(formats
)
160 subtitles
= self
.extract_subtitles(video_id
)
165 'duration': duration
,
166 'subtitles': subtitles
,
170 def _fix_subtitles(self
, subs
):
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:
178 seq_next
= subs
[pos
+ 1]
179 m_next
= re
.match(self
._TIMECODE
_REGEX
, seq_next
['Timecode'])
182 appear_time
= m_current
.group('timecode')
183 disappear_time
= m_next
.group('timecode')
184 text
= seq_current
['Caption'].strip()
187 srt
+= '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (seq_counter
, appear_time
, disappear_time
, text
)
191 def _get_subtitles(self
, video_id
):
192 url
= 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
193 subs
= self
._download
_json
(url
, None, False)
195 return {'en': [{'ext': 'srt', 'data': self
._fix
_subtitles
(subs
)}]}
200 class LyndaCourseIE(LyndaBaseIE
):
201 IE_NAME
= 'lynda:course'
202 IE_DESC
= 'lynda.com online courses'
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'
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')
213 course
= self
._download
_json
(
214 'http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id
,
215 course_id
, 'Downloading course JSON')
219 if course
.get('Status') == 'NotFound':
220 raise ExtractorError(
221 'Course %s does not exist' % course_id
, expected
=True)
223 unaccessible_videos
= 0
226 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
227 # by single video API anymore
229 for chapter
in course
['Chapters']:
230 for video
in chapter
.get('Videos', []):
231 if video
.get('HasAccess') is False:
232 unaccessible_videos
+= 1
235 videos
.append(video
['ID'])
237 if unaccessible_videos
> 0:
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
)
244 'http://www.lynda.com/%s/%s-4.html' % (course_path
, video_id
),
246 for video_id
in videos
]
248 course_title
= course
.get('Title')
250 return self
.playlist_result(entries
, course_id
, course_title
)