]>
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 |
7ee40b5d | 8 | from ..utils import ( |
9 | compat_urllib_parse, | |
10 | compat_urllib_request, | |
11 | ExtractorError | |
12 | ) | |
c7f8537d | 13 | |
14 | ||
62bcfa8c | 15 | class LyndaIE(SubtitlesInfoExtractor): |
a7c26e73 PH |
16 | IE_NAME = 'lynda' |
17 | IE_DESC = 'lynda.com videos' | |
c7f8537d | 18 | _VALID_URL = r'https?://www\.lynda\.com/[^/]+/[^/]+/\d+/(\d+)-\d\.html' |
7ee40b5d | 19 | _LOGIN_URL = 'https://www.lynda.com/login/login.aspx' |
20 | _NETRC_MACHINE = 'lynda' | |
21 | ||
22 | _SUCCESSFUL_LOGIN_REGEX = r'<a href="https://www.lynda.com/home/userAccount/ChangeContactInfo.aspx" data-qa="eyebrow_account_menu">My account' | |
23 | _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]' | |
24 | ||
25 | ACCOUNT_CREDENTIALS_HINT = 'Use --username and --password options to provide lynda.com account credentials.' | |
c7f8537d | 26 | |
27 | _TEST = { | |
a7c26e73 PH |
28 | 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html', |
29 | 'file': '114408.mp4', | |
30 | 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1', | |
ac260dd8 | 31 | 'info_dict': { |
a7c26e73 PH |
32 | 'title': 'Using the exercise files', |
33 | 'duration': 68 | |
c7f8537d | 34 | } |
35 | } | |
7ee40b5d | 36 | |
37 | def _real_initialize(self): | |
38 | self._login() | |
39 | ||
c7f8537d | 40 | def _real_extract(self, url): |
41 | mobj = re.match(self._VALID_URL, url) | |
42 | video_id = mobj.group(1) | |
43 | ||
44 | page = self._download_webpage('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id, | |
a7c26e73 | 45 | video_id, 'Downloading video JSON') |
c7f8537d | 46 | video_json = json.loads(page) |
47 | ||
7ee40b5d | 48 | if 'Status' in video_json: |
49 | raise ExtractorError('lynda returned error: %s' % video_json['Message'], expected=True) | |
c7f8537d | 50 | |
a7c26e73 | 51 | if video_json['HasAccess'] is False: |
7ee40b5d | 52 | raise ExtractorError('Video %s is only available for members. ' % video_id + self.ACCOUNT_CREDENTIALS_HINT, expected=True) |
c7f8537d | 53 | |
a7c26e73 PH |
54 | video_id = video_json['ID'] |
55 | duration = video_json['DurationInSeconds'] | |
56 | title = video_json['Title'] | |
c7f8537d | 57 | |
a7c26e73 PH |
58 | formats = [{'url': fmt['Url'], |
59 | 'ext': fmt['Extension'], | |
60 | 'width': fmt['Width'], | |
61 | 'height': fmt['Height'], | |
62 | 'filesize': fmt['FileSize'], | |
62bcfa8c | 63 | 'format_id': str(fmt['Resolution']) |
a7c26e73 | 64 | } for fmt in video_json['Formats']] |
c7f8537d | 65 | |
66 | self._sort_formats(formats) | |
7ee40b5d | 67 | |
62bcfa8c | 68 | if self._downloader.params.get('listsubtitles', False): |
69 | self._list_available_subtitles(video_id, page) | |
70 | return | |
7ee40b5d | 71 | |
62bcfa8c | 72 | subtitles = self._fix_subtitles(self.extract_subtitles(video_id, page)) |
7ee40b5d | 73 | |
c7f8537d | 74 | return { |
75 | 'id': video_id, | |
76 | 'title': title, | |
77 | 'duration': duration, | |
62bcfa8c | 78 | 'subtitles': subtitles, |
c7f8537d | 79 | 'formats': formats |
80 | } | |
7ee40b5d | 81 | |
82 | def _login(self): | |
83 | (username, password) = self._get_login_info() | |
84 | if username is None: | |
85 | return | |
86 | ||
87 | login_form = { | |
88 | 'username': username, | |
89 | 'password': password, | |
90 | 'remember': 'false', | |
91 | 'stayPut': 'false' | |
92 | } | |
93 | request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form)) | |
94 | login_page = self._download_webpage(request, None, note='Logging in as %s' % username) | |
95 | ||
96 | # Not (yet) logged in | |
97 | m = re.search(r'loginResultJson = \'(?P<json>[^\']+)\';', login_page) | |
98 | if m is not None: | |
99 | response = m.group('json') | |
100 | response_json = json.loads(response) | |
101 | state = response_json['state'] | |
102 | ||
103 | if state == 'notlogged': | |
104 | raise ExtractorError('Unable to login, incorrect username and/or password', expected=True) | |
105 | ||
106 | # This is when we get popup: | |
107 | # > You're already logged in to lynda.com on two devices. | |
108 | # > If you log in here, we'll log you out of another device. | |
109 | # So, we need to confirm this. | |
110 | if state == 'conflicted': | |
111 | confirm_form = { | |
112 | 'username': '', | |
113 | 'password': '', | |
114 | 'resolve': 'true', | |
115 | 'remember': 'false', | |
116 | 'stayPut': 'false', | |
117 | } | |
118 | request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(confirm_form)) | |
119 | login_page = self._download_webpage(request, None, note='Confirming log in and log out from another device') | |
120 | ||
121 | if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None: | |
122 | raise ExtractorError('Unable to log in') | |
73a25b30 | 123 | |
62bcfa8c | 124 | def _fix_subtitles(self, subtitles): |
7b09a4d8 PH |
125 | if subtitles is None: |
126 | return subtitles # subtitles not requested | |
127 | ||
62bcfa8c | 128 | fixed_subtitles = {} |
129 | for k, v in subtitles.items(): | |
130 | subs = json.loads(v) | |
131 | if len(subs) == 0: | |
132 | continue | |
133 | srt = '' | |
134 | for pos in range(0, len(subs) - 1): | |
73a25b30 | 135 | seq_current = subs[pos] |
62bcfa8c | 136 | m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode']) |
137 | if m_current is None: | |
73a25b30 PH |
138 | continue |
139 | seq_next = subs[pos + 1] | |
62bcfa8c | 140 | m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode']) |
141 | if m_next is None: | |
73a25b30 | 142 | continue |
62bcfa8c | 143 | appear_time = m_current.group('timecode') |
144 | disappear_time = m_next.group('timecode') | |
145 | text = seq_current['Caption'] | |
146 | srt += '%s\r\n%s --> %s\r\n%s' % (str(pos), appear_time, disappear_time, text) | |
147 | if srt: | |
148 | fixed_subtitles[k] = srt | |
149 | return fixed_subtitles | |
7ee40b5d | 150 | |
62bcfa8c | 151 | def _get_available_subtitles(self, video_id, webpage): |
152 | url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id | |
153 | sub = self._download_webpage(url, None, note=False) | |
154 | sub_json = json.loads(sub) | |
155 | return {'en': url} if len(sub_json) > 0 else {} | |
c7f8537d | 156 | |
157 | ||
158 | class LyndaCourseIE(InfoExtractor): | |
a7c26e73 PH |
159 | IE_NAME = 'lynda:course' |
160 | IE_DESC = 'lynda.com online courses' | |
c7f8537d | 161 | |
162 | # Course link equals to welcome/introduction video link of same course | |
163 | # We will recognize it as course link | |
164 | _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html' | |
165 | ||
166 | def _real_extract(self, url): | |
167 | mobj = re.match(self._VALID_URL, url) | |
168 | course_path = mobj.group('coursepath') | |
169 | course_id = mobj.group('courseid') | |
7ee40b5d | 170 | |
c7f8537d | 171 | page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id, |
a7c26e73 | 172 | course_id, 'Downloading course JSON') |
c7f8537d | 173 | course_json = json.loads(page) |
174 | ||
a7c26e73 PH |
175 | if 'Status' in course_json and course_json['Status'] == 'NotFound': |
176 | raise ExtractorError('Course %s does not exist' % course_id, expected=True) | |
c7f8537d | 177 | |
178 | unaccessible_videos = 0 | |
179 | videos = [] | |
7ee40b5d | 180 | (username, _) = self._get_login_info() |
c7f8537d | 181 | |
a7c26e73 PH |
182 | for chapter in course_json['Chapters']: |
183 | for video in chapter['Videos']: | |
7ee40b5d | 184 | if username is None and video['HasAccess'] is False: |
c7f8537d | 185 | unaccessible_videos += 1 |
186 | continue | |
a7c26e73 | 187 | videos.append(video['ID']) |
c7f8537d | 188 | |
189 | if unaccessible_videos > 0: | |
7ee40b5d | 190 | self._downloader.report_warning('%s videos are only available for members and will not be downloaded. ' |
191 | % unaccessible_videos + LyndaIE.ACCOUNT_CREDENTIALS_HINT) | |
c7f8537d | 192 | |
a7c26e73 PH |
193 | entries = [ |
194 | self.url_result('http://www.lynda.com/%s/%s-4.html' % | |
195 | (course_path, video_id), | |
196 | 'Lynda') | |
197 | for video_id in videos] | |
c7f8537d | 198 | |
a7c26e73 | 199 | course_title = course_json['Title'] |
c7f8537d | 200 | |
7ee40b5d | 201 | return self.playlist_result(entries, course_id, course_title) |