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+/(\d+)-\d\.html'
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',
41 def _real_initialize(self
):
44 def _real_extract(self
, url
):
45 mobj
= re
.match(self
._VALID
_URL
, url
)
46 video_id
= mobj
.group(1)
48 page
= self
._download
_webpage
('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id
, video_id
,
49 'Downloading video JSON')
50 video_json
= json
.loads(page
)
52 if 'Status' in video_json
:
53 raise ExtractorError('lynda returned error: %s' % video_json
['Message'], expected
=True)
55 if video_json
['HasAccess'] is False:
57 'Video %s is only available for members. ' % video_id
+ self
.ACCOUNT_CREDENTIALS_HINT
, expected
=True)
59 video_id
= compat_str(video_json
['ID'])
60 duration
= video_json
['DurationInSeconds']
61 title
= video_json
['Title']
65 fmts
= video_json
.get('Formats')
70 'ext': fmt
['Extension'],
71 'width': fmt
['Width'],
72 'height': fmt
['Height'],
73 'filesize': fmt
['FileSize'],
74 'format_id': str(fmt
['Resolution'])
77 prioritized_streams
= video_json
.get('PrioritizedStreams')
78 if prioritized_streams
:
82 'width': int_or_none(format_id
),
83 'format_id': format_id
,
84 } for format_id
, video_url
in prioritized_streams
['0'].items()
87 self
._check
_formats
(formats
, video_id
)
88 self
._sort
_formats
(formats
)
90 subtitles
= self
.extract_subtitles(video_id
, page
)
96 'subtitles': subtitles
,
101 (username
, password
) = self
._get
_login
_info
()
106 'username': username
,
107 'password': password
,
111 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
))
112 login_page
= self
._download
_webpage
(request
, None, 'Logging in as %s' % username
)
114 # Not (yet) logged in
115 m
= re
.search(r
'loginResultJson = \'(?P
<json
>[^
\']+)\';', login_page)
117 response = m.group('json
')
118 response_json = json.loads(response)
119 state = response_json['state
']
121 if state == 'notlogged
':
122 raise ExtractorError('Unable to login
, incorrect username
and/or password
', expected=True)
124 # This is when we get popup:
125 # > You're already logged
in to lynda
.com on two devices
.
126 # > If you log in here, we'll log you out of another device.
127 # So, we need to confirm this.
128 if state
== 'conflicted':
136 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(confirm_form
))
137 login_page
= self
._download
_webpage
(request
, None, 'Confirming log in and log out from another device')
139 if re
.search(self
._SUCCESSFUL
_LOGIN
_REGEX
, login_page
) is None:
140 raise ExtractorError('Unable to log in')
142 def _fix_subtitles(self
, subs
):
144 for pos
in range(0, len(subs
) - 1):
145 seq_current
= subs
[pos
]
146 m_current
= re
.match(self
._TIMECODE
_REGEX
, seq_current
['Timecode'])
147 if m_current
is None:
149 seq_next
= subs
[pos
+ 1]
150 m_next
= re
.match(self
._TIMECODE
_REGEX
, seq_next
['Timecode'])
153 appear_time
= m_current
.group('timecode')
154 disappear_time
= m_next
.group('timecode')
155 text
= seq_current
['Caption'].strip()
156 srt
+= '%s\r\n%s --> %s\r\n%s' % (str(pos
), appear_time
, disappear_time
, text
)
160 def _get_subtitles(self
, video_id
, webpage
):
161 url
= 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
162 subs
= self
._download
_json
(url
, None, False)
164 return {'en': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}
]}
169 class LyndaCourseIE(InfoExtractor
):
170 IE_NAME
= 'lynda:course'
171 IE_DESC
= 'lynda.com online courses'
173 # Course link equals to welcome/introduction video link of same course
174 # We will recognize it as course link
175 _VALID_URL
= r
'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
177 def _real_extract(self
, url
):
178 mobj
= re
.match(self
._VALID
_URL
, url
)
179 course_path
= mobj
.group('coursepath')
180 course_id
= mobj
.group('courseid')
182 page
= self
._download
_webpage
('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id
,
183 course_id
, 'Downloading course JSON')
184 course_json
= json
.loads(page
)
186 if 'Status' in course_json
and course_json
['Status'] == 'NotFound':
187 raise ExtractorError('Course %s does not exist' % course_id
, expected
=True)
189 unaccessible_videos
= 0
191 (username
, _
) = self
._get
_login
_info
()
193 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
194 # by single video API anymore
196 for chapter
in course_json
['Chapters']:
197 for video
in chapter
['Videos']:
198 if username
is None and video
['HasAccess'] is False:
199 unaccessible_videos
+= 1
201 videos
.append(video
['ID'])
203 if unaccessible_videos
> 0:
204 self
._downloader
.report_warning('%s videos are only available for members and will not be downloaded. '
205 % unaccessible_videos
+ LyndaIE
.ACCOUNT_CREDENTIALS_HINT
)
208 self
.url_result('http://www.lynda.com/%s/%s-4.html' %
209 (course_path
, video_id
),
211 for video_id
in videos
]
213 course_title
= course_json
['Title']
215 return self
.playlist_result(entries
, course_id
, course_title
)