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 _SUCCESSFUL_LOGIN_REGEX
= r
'isLoggedIn: true'
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
()
38 request
= compat_urllib_request
.Request(
39 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
))
40 login_page
= self
._download
_webpage
(
41 request
, None, 'Logging in as %s' % username
)
44 m
= re
.search(r
'loginResultJson = \'(?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
= compat_urllib_request
.Request(
68 self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(confirm_form
))
69 login_page
= self
._download
_webpage
(
71 'Confirming log in and log out from another device')
73 if re
.search(self
._SUCCESSFUL
_LOGIN
_REGEX
, login_page
) is None:
74 raise ExtractorError('Unable to log in')
77 class LyndaIE(LyndaBaseIE
):
79 IE_DESC
= 'lynda.com videos'
80 _VALID_URL
= r
'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(?P<id>\d+)'
81 _NETRC_MACHINE
= 'lynda'
83 _TIMECODE_REGEX
= r
'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
86 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
87 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
91 'title': 'Using the exercise files',
95 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
96 'only_matching': True,
99 def _real_extract(self
, url
):
100 video_id
= self
._match
_id
(url
)
102 page
= self
._download
_webpage
(
103 'http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id
,
104 video_id
, 'Downloading video JSON')
105 video_json
= json
.loads(page
)
107 if 'Status' in video_json
:
108 raise ExtractorError(
109 'lynda returned error: %s' % video_json
['Message'], expected
=True)
111 if video_json
['HasAccess'] is False:
112 raise ExtractorError(
113 'Video %s is only available for members. '
114 % video_id
+ self
._ACCOUNT
_CREDENTIALS
_HINT
, expected
=True)
116 video_id
= compat_str(video_json
['ID'])
117 duration
= video_json
['DurationInSeconds']
118 title
= video_json
['Title']
122 fmts
= video_json
.get('Formats')
127 'ext': fmt
['Extension'],
128 'width': fmt
['Width'],
129 'height': fmt
['Height'],
130 'filesize': fmt
['FileSize'],
131 'format_id': str(fmt
['Resolution'])
134 prioritized_streams
= video_json
.get('PrioritizedStreams')
135 if prioritized_streams
:
139 'width': int_or_none(format_id
),
140 'format_id': format_id
,
141 } for format_id
, video_url
in prioritized_streams
['0'].items()
144 self
._check
_formats
(formats
, video_id
)
145 self
._sort
_formats
(formats
)
147 subtitles
= self
.extract_subtitles(video_id
, page
)
152 'duration': duration
,
153 'subtitles': subtitles
,
157 def _fix_subtitles(self
, subs
):
160 for pos
in range(0, len(subs
) - 1):
161 seq_current
= subs
[pos
]
162 m_current
= re
.match(self
._TIMECODE
_REGEX
, seq_current
['Timecode'])
163 if m_current
is None:
165 seq_next
= subs
[pos
+ 1]
166 m_next
= re
.match(self
._TIMECODE
_REGEX
, seq_next
['Timecode'])
169 appear_time
= m_current
.group('timecode')
170 disappear_time
= m_next
.group('timecode')
171 text
= seq_current
['Caption'].strip()
174 srt
+= '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (seq_counter
, appear_time
, disappear_time
, text
)
178 def _get_subtitles(self
, video_id
, webpage
):
179 url
= 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
180 subs
= self
._download
_json
(url
, None, False)
182 return {'en': [{'ext': 'srt', 'data': self
._fix
_subtitles
(subs
)}]}
187 class LyndaCourseIE(LyndaBaseIE
):
188 IE_NAME
= 'lynda:course'
189 IE_DESC
= 'lynda.com online courses'
191 # Course link equals to welcome/introduction video link of same course
192 # We will recognize it as course link
193 _VALID_URL
= r
'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
195 def _real_extract(self
, url
):
196 mobj
= re
.match(self
._VALID
_URL
, url
)
197 course_path
= mobj
.group('coursepath')
198 course_id
= mobj
.group('courseid')
200 page
= self
._download
_webpage
(
201 'http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id
,
202 course_id
, 'Downloading course JSON')
203 course_json
= json
.loads(page
)
205 if 'Status' in course_json
and course_json
['Status'] == 'NotFound':
206 raise ExtractorError(
207 'Course %s does not exist' % course_id
, expected
=True)
209 unaccessible_videos
= 0
212 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
213 # by single video API anymore
215 for chapter
in course_json
['Chapters']:
216 for video
in chapter
['Videos']:
217 if video
['HasAccess'] is False:
218 unaccessible_videos
+= 1
220 videos
.append(video
['ID'])
222 if unaccessible_videos
> 0:
223 self
._downloader
.report_warning(
224 '%s videos are only available for members (or paid members) and will not be downloaded. '
225 % unaccessible_videos
+ self
._ACCOUNT
_CREDENTIALS
_HINT
)
229 'http://www.lynda.com/%s/%s-4.html' % (course_path
, video_id
),
231 for video_id
in videos
]
233 course_title
= course_json
['Title']
235 return self
.playlist_result(entries
, course_id
, course_title
)