]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/lynda.py
26e84970d49463068f032dcf05afbc03e485e859
1 from __future__
import unicode_literals
6 from .subtitles
import SubtitlesInfoExtractor
7 from .common
import InfoExtractor
11 compat_urllib_request
,
19 class LyndaIE(SubtitlesInfoExtractor
):
21 IE_DESC
= 'lynda.com videos'
22 _VALID_URL
= r
'https?://www\.lynda\.com/[^/]+/[^/]+/\d+/(\d+)-\d\.html'
23 _LOGIN_URL
= 'https://www.lynda.com/login/login.aspx'
24 _NETRC_MACHINE
= 'lynda'
26 _SUCCESSFUL_LOGIN_REGEX
= r
'isLoggedIn: true'
27 _TIMECODE_REGEX
= r
'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
29 ACCOUNT_CREDENTIALS_HINT
= 'Use --username and --password options to provide lynda.com account credentials.'
32 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
33 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
37 'title': 'Using the exercise files',
42 def _real_initialize(self
):
45 def _real_extract(self
, url
):
46 mobj
= re
.match(self
._VALID
_URL
, url
)
47 video_id
= mobj
.group(1)
49 page
= self
._download
_webpage
('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id
, video_id
,
50 'Downloading video JSON')
51 video_json
= json
.loads(page
)
53 if 'Status' in video_json
:
54 raise ExtractorError('lynda returned error: %s' % video_json
['Message'], expected
=True)
56 if video_json
['HasAccess'] is False:
58 'Video %s is only available for members. ' % video_id
+ self
.ACCOUNT_CREDENTIALS_HINT
, expected
=True)
60 video_id
= compat_str(video_json
['ID'])
61 duration
= video_json
['DurationInSeconds']
62 title
= video_json
['Title']
66 fmts
= video_json
.get('Formats')
71 'ext': fmt
['Extension'],
72 'width': fmt
['Width'],
73 'height': fmt
['Height'],
74 'filesize': fmt
['FileSize'],
75 'format_id': str(fmt
['Resolution'])
78 prioritized_streams
= video_json
.get('PrioritizedStreams')
79 if prioritized_streams
:
83 'width': int_or_none(format_id
),
84 'format_id': format_id
,
85 } for format_id
, video_url
in prioritized_streams
['0'].items()
88 self
._sort
_formats
(formats
)
90 if self
._downloader
.params
.get('listsubtitles', False):
91 self
._list
_available
_subtitles
(video_id
, page
)
94 subtitles
= self
._fix
_subtitles
(self
.extract_subtitles(video_id
, page
))
100 'subtitles': subtitles
,
105 (username
, password
) = self
._get
_login
_info
()
110 'username': username
,
111 'password': password
,
115 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(login_form
))
116 login_page
= self
._download
_webpage
(request
, None, 'Logging in as %s' % username
)
118 # Not (yet) logged in
119 m
= re
.search(r
'loginResultJson = \'(?P
<json
>[^
\']+)\';', login_page)
121 response = m.group('json
')
122 response_json = json.loads(response)
123 state = response_json['state
']
125 if state == 'notlogged
':
126 raise ExtractorError('Unable to login
, incorrect username
and/or password
', expected=True)
128 # This is when we get popup:
129 # > You're already logged
in to lynda
.com on two devices
.
130 # > If you log in here, we'll log you out of another device.
131 # So, we need to confirm this.
132 if state
== 'conflicted':
140 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, compat_urllib_parse
.urlencode(confirm_form
))
141 login_page
= self
._download
_webpage
(request
, None, 'Confirming log in and log out from another device')
143 if re
.search(self
._SUCCESSFUL
_LOGIN
_REGEX
, login_page
) is None:
144 raise ExtractorError('Unable to log in')
146 def _fix_subtitles(self
, subtitles
):
147 if subtitles
is None:
148 return subtitles
# subtitles not requested
151 for k
, v
in subtitles
.items():
156 for pos
in range(0, len(subs
) - 1):
157 seq_current
= subs
[pos
]
158 m_current
= re
.match(self
._TIMECODE
_REGEX
, seq_current
['Timecode'])
159 if m_current
is None:
161 seq_next
= subs
[pos
+ 1]
162 m_next
= re
.match(self
._TIMECODE
_REGEX
, seq_next
['Timecode'])
165 appear_time
= m_current
.group('timecode')
166 disappear_time
= m_next
.group('timecode')
167 text
= seq_current
['Caption']
168 srt
+= '%s\r\n%s --> %s\r\n%s' % (str(pos
), appear_time
, disappear_time
, text
)
170 fixed_subtitles
[k
] = srt
171 return fixed_subtitles
173 def _get_available_subtitles(self
, video_id
, webpage
):
174 url
= 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
175 sub
= self
._download
_webpage
(url
, None, False)
176 sub_json
= json
.loads(sub
)
177 return {'en': url
} if len(sub_json
) > 0 else {}
180 class LyndaCourseIE(InfoExtractor
):
181 IE_NAME
= 'lynda:course'
182 IE_DESC
= 'lynda.com online courses'
184 # Course link equals to welcome/introduction video link of same course
185 # We will recognize it as course link
186 _VALID_URL
= r
'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
188 def _real_extract(self
, url
):
189 mobj
= re
.match(self
._VALID
_URL
, url
)
190 course_path
= mobj
.group('coursepath')
191 course_id
= mobj
.group('courseid')
193 page
= self
._download
_webpage
('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id
,
194 course_id
, 'Downloading course JSON')
195 course_json
= json
.loads(page
)
197 if 'Status' in course_json
and course_json
['Status'] == 'NotFound':
198 raise ExtractorError('Course %s does not exist' % course_id
, expected
=True)
200 unaccessible_videos
= 0
202 (username
, _
) = self
._get
_login
_info
()
204 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
205 # by single video API anymore
207 for chapter
in course_json
['Chapters']:
208 for video
in chapter
['Videos']:
209 if username
is None and video
['HasAccess'] is False:
210 unaccessible_videos
+= 1
212 videos
.append(video
['ID'])
214 if unaccessible_videos
> 0:
215 self
._downloader
.report_warning('%s videos are only available for members and will not be downloaded. '
216 % unaccessible_videos
+ LyndaIE
.ACCOUNT_CREDENTIALS_HINT
)
219 self
.url_result('http://www.lynda.com/%s/%s-4.html' %
220 (course_path
, video_id
),
222 for video_id
in videos
]
224 course_title
= course_json
['Title']
226 return self
.playlist_result(entries
, course_id
, course_title
)