]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/lynda.py
26e84970d49463068f032dcf05afbc03e485e859
[youtubedl] / youtube_dl / extractor / lynda.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .subtitles import SubtitlesInfoExtractor
7 from .common import InfoExtractor
8 from ..compat import (
9 compat_str,
10 compat_urllib_parse,
11 compat_urllib_request,
12 )
13 from ..utils import (
14 ExtractorError,
15 int_or_none,
16 )
17
18
19 class LyndaIE(SubtitlesInfoExtractor):
20 IE_NAME = 'lynda'
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'
25
26 _SUCCESSFUL_LOGIN_REGEX = r'isLoggedIn: true'
27 _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
28
29 ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.'
30
31 _TEST = {
32 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
33 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
34 'info_dict': {
35 'id': '114408',
36 'ext': 'mp4',
37 'title': 'Using the exercise files',
38 'duration': 68
39 }
40 }
41
42 def _real_initialize(self):
43 self._login()
44
45 def _real_extract(self, url):
46 mobj = re.match(self._VALID_URL, url)
47 video_id = mobj.group(1)
48
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)
52
53 if 'Status' in video_json:
54 raise ExtractorError('lynda returned error: %s' % video_json['Message'], expected=True)
55
56 if video_json['HasAccess'] is False:
57 raise ExtractorError(
58 'Video %s is only available for members. ' % video_id + self.ACCOUNT_CREDENTIALS_HINT, expected=True)
59
60 video_id = compat_str(video_json['ID'])
61 duration = video_json['DurationInSeconds']
62 title = video_json['Title']
63
64 formats = []
65
66 fmts = video_json.get('Formats')
67 if fmts:
68 formats.extend([
69 {
70 'url': fmt['Url'],
71 'ext': fmt['Extension'],
72 'width': fmt['Width'],
73 'height': fmt['Height'],
74 'filesize': fmt['FileSize'],
75 'format_id': str(fmt['Resolution'])
76 } for fmt in fmts])
77
78 prioritized_streams = video_json.get('PrioritizedStreams')
79 if prioritized_streams:
80 formats.extend([
81 {
82 'url': video_url,
83 'width': int_or_none(format_id),
84 'format_id': format_id,
85 } for format_id, video_url in prioritized_streams['0'].items()
86 ])
87
88 self._sort_formats(formats)
89
90 if self._downloader.params.get('listsubtitles', False):
91 self._list_available_subtitles(video_id, page)
92 return
93
94 subtitles = self._fix_subtitles(self.extract_subtitles(video_id, page))
95
96 return {
97 'id': video_id,
98 'title': title,
99 'duration': duration,
100 'subtitles': subtitles,
101 'formats': formats
102 }
103
104 def _login(self):
105 (username, password) = self._get_login_info()
106 if username is None:
107 return
108
109 login_form = {
110 'username': username,
111 'password': password,
112 'remember': 'false',
113 'stayPut': 'false'
114 }
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)
117
118 # Not (yet) logged in
119 m = re.search(r'loginResultJson = \'(?P<json>[^\']+)\';', login_page)
120 if m is not None:
121 response = m.group('json')
122 response_json = json.loads(response)
123 state = response_json['state']
124
125 if state == 'notlogged':
126 raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
127
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':
133 confirm_form = {
134 'username': '',
135 'password': '',
136 'resolve': 'true',
137 'remember': 'false',
138 'stayPut': 'false',
139 }
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')
142
143 if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
144 raise ExtractorError('Unable to log in')
145
146 def _fix_subtitles(self, subtitles):
147 if subtitles is None:
148 return subtitles # subtitles not requested
149
150 fixed_subtitles = {}
151 for k, v in subtitles.items():
152 subs = json.loads(v)
153 if len(subs) == 0:
154 continue
155 srt = ''
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:
160 continue
161 seq_next = subs[pos + 1]
162 m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
163 if m_next is None:
164 continue
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)
169 if srt:
170 fixed_subtitles[k] = srt
171 return fixed_subtitles
172
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 {}
178
179
180 class LyndaCourseIE(InfoExtractor):
181 IE_NAME = 'lynda:course'
182 IE_DESC = 'lynda.com online courses'
183
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'
187
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')
192
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)
196
197 if 'Status' in course_json and course_json['Status'] == 'NotFound':
198 raise ExtractorError('Course %s does not exist' % course_id, expected=True)
199
200 unaccessible_videos = 0
201 videos = []
202 (username, _) = self._get_login_info()
203
204 # Might want to extract videos right here from video['Formats'] as it seems 'Formats' is not provided
205 # by single video API anymore
206
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
211 continue
212 videos.append(video['ID'])
213
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)
217
218 entries = [
219 self.url_result('http://www.lynda.com/%s/%s-4.html' %
220 (course_path, video_id),
221 'Lynda')
222 for video_id in videos]
223
224 course_title = course_json['Title']
225
226 return self.playlist_result(entries, course_id, course_title)