]>
Commit | Line | Data |
---|---|---|
a7c26e73 PH |
1 | from __future__ import unicode_literals |
2 | ||
c7f8537d | 3 | import re |
4 | import json | |
5 | ||
62bcfa8c | 6 | from .subtitles import SubtitlesInfoExtractor |
c7f8537d | 7 | from .common import InfoExtractor |
1cc79574 PH |
8 | from ..compat import ( |
9 | compat_str, | |
7ee40b5d | 10 | compat_urllib_parse, |
11 | compat_urllib_request, | |
1cc79574 PH |
12 | ) |
13 | from ..utils import ( | |
16ff7ebc S |
14 | ExtractorError, |
15 | int_or_none, | |
7ee40b5d | 16 | ) |
c7f8537d | 17 | |
18 | ||
62bcfa8c | 19 | class LyndaIE(SubtitlesInfoExtractor): |
a7c26e73 PH |
20 | IE_NAME = 'lynda' |
21 | IE_DESC = 'lynda.com videos' | |
c7f8537d | 22 | _VALID_URL = r'https?://www\.lynda\.com/[^/]+/[^/]+/\d+/(\d+)-\d\.html' |
7ee40b5d | 23 | _LOGIN_URL = 'https://www.lynda.com/login/login.aspx' |
24 | _NETRC_MACHINE = 'lynda' | |
25 | ||
16ff7ebc | 26 | _SUCCESSFUL_LOGIN_REGEX = r'isLoggedIn: true' |
7ee40b5d | 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.' | |
c7f8537d | 30 | |
31 | _TEST = { | |
a7c26e73 | 32 | 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html', |
a7c26e73 | 33 | 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1', |
ac260dd8 | 34 | 'info_dict': { |
136db788 S |
35 | 'id': '114408', |
36 | 'ext': 'mp4', | |
a7c26e73 PH |
37 | 'title': 'Using the exercise files', |
38 | 'duration': 68 | |
c7f8537d | 39 | } |
40 | } | |
7ee40b5d | 41 | |
42 | def _real_initialize(self): | |
43 | self._login() | |
44 | ||
c7f8537d | 45 | def _real_extract(self, url): |
46 | mobj = re.match(self._VALID_URL, url) | |
47 | video_id = mobj.group(1) | |
48 | ||
136db788 | 49 | page = self._download_webpage('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id, video_id, |
9e1a5b84 | 50 | 'Downloading video JSON') |
c7f8537d | 51 | video_json = json.loads(page) |
52 | ||
7ee40b5d | 53 | if 'Status' in video_json: |
54 | raise ExtractorError('lynda returned error: %s' % video_json['Message'], expected=True) | |
c7f8537d | 55 | |
a7c26e73 | 56 | if video_json['HasAccess'] is False: |
136db788 S |
57 | raise ExtractorError( |
58 | 'Video %s is only available for members. ' % video_id + self.ACCOUNT_CREDENTIALS_HINT, expected=True) | |
c7f8537d | 59 | |
136db788 | 60 | video_id = compat_str(video_json['ID']) |
a7c26e73 PH |
61 | duration = video_json['DurationInSeconds'] |
62 | title = video_json['Title'] | |
c7f8537d | 63 | |
16ff7ebc S |
64 | formats = [] |
65 | ||
66 | fmts = video_json.get('Formats') | |
67 | if fmts: | |
68 | formats.extend([ | |
69 | { | |
70 | 'url': fmt['Url'], | |
a7c26e73 PH |
71 | 'ext': fmt['Extension'], |
72 | 'width': fmt['Width'], | |
73 | 'height': fmt['Height'], | |
74 | 'filesize': fmt['FileSize'], | |
62bcfa8c | 75 | 'format_id': str(fmt['Resolution']) |
16ff7ebc S |
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 | ]) | |
c7f8537d | 87 | |
88 | self._sort_formats(formats) | |
7ee40b5d | 89 | |
62bcfa8c | 90 | if self._downloader.params.get('listsubtitles', False): |
91 | self._list_available_subtitles(video_id, page) | |
92 | return | |
7ee40b5d | 93 | |
62bcfa8c | 94 | subtitles = self._fix_subtitles(self.extract_subtitles(video_id, page)) |
7ee40b5d | 95 | |
c7f8537d | 96 | return { |
97 | 'id': video_id, | |
98 | 'title': title, | |
99 | 'duration': duration, | |
62bcfa8c | 100 | 'subtitles': subtitles, |
c7f8537d | 101 | 'formats': formats |
102 | } | |
7ee40b5d | 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' | |
5f6a1245 | 114 | } |
7ee40b5d | 115 | request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form)) |
136db788 | 116 | login_page = self._download_webpage(request, None, 'Logging in as %s' % username) |
7ee40b5d | 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') | |
5f6a1245 | 122 | response_json = json.loads(response) |
7ee40b5d | 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)) | |
136db788 | 141 | login_page = self._download_webpage(request, None, 'Confirming log in and log out from another device') |
7ee40b5d | 142 | |
143 | if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None: | |
144 | raise ExtractorError('Unable to log in') | |
73a25b30 | 145 | |
62bcfa8c | 146 | def _fix_subtitles(self, subtitles): |
7b09a4d8 PH |
147 | if subtitles is None: |
148 | return subtitles # subtitles not requested | |
149 | ||
62bcfa8c | 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): | |
73a25b30 | 157 | seq_current = subs[pos] |
62bcfa8c | 158 | m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode']) |
159 | if m_current is None: | |
73a25b30 PH |
160 | continue |
161 | seq_next = subs[pos + 1] | |
62bcfa8c | 162 | m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode']) |
163 | if m_next is None: | |
73a25b30 | 164 | continue |
62bcfa8c | 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 | |
7ee40b5d | 172 | |
62bcfa8c | 173 | def _get_available_subtitles(self, video_id, webpage): |
174 | url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id | |
136db788 | 175 | sub = self._download_webpage(url, None, False) |
62bcfa8c | 176 | sub_json = json.loads(sub) |
177 | return {'en': url} if len(sub_json) > 0 else {} | |
c7f8537d | 178 | |
179 | ||
180 | class LyndaCourseIE(InfoExtractor): | |
a7c26e73 PH |
181 | IE_NAME = 'lynda:course' |
182 | IE_DESC = 'lynda.com online courses' | |
c7f8537d | 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') | |
5f6a1245 | 192 | |
c7f8537d | 193 | page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id, |
a7c26e73 | 194 | course_id, 'Downloading course JSON') |
c7f8537d | 195 | course_json = json.loads(page) |
196 | ||
a7c26e73 PH |
197 | if 'Status' in course_json and course_json['Status'] == 'NotFound': |
198 | raise ExtractorError('Course %s does not exist' % course_id, expected=True) | |
c7f8537d | 199 | |
200 | unaccessible_videos = 0 | |
201 | videos = [] | |
7ee40b5d | 202 | (username, _) = self._get_login_info() |
c7f8537d | 203 | |
16ff7ebc S |
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 | ||
a7c26e73 PH |
207 | for chapter in course_json['Chapters']: |
208 | for video in chapter['Videos']: | |
7ee40b5d | 209 | if username is None and video['HasAccess'] is False: |
c7f8537d | 210 | unaccessible_videos += 1 |
211 | continue | |
a7c26e73 | 212 | videos.append(video['ID']) |
c7f8537d | 213 | |
214 | if unaccessible_videos > 0: | |
7ee40b5d | 215 | self._downloader.report_warning('%s videos are only available for members and will not be downloaded. ' |
216 | % unaccessible_videos + LyndaIE.ACCOUNT_CREDENTIALS_HINT) | |
c7f8537d | 217 | |
a7c26e73 PH |
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] | |
c7f8537d | 223 | |
a7c26e73 | 224 | course_title = course_json['Title'] |
c7f8537d | 225 | |
5f6a1245 | 226 | return self.playlist_result(entries, course_id, course_title) |