]>
Commit | Line | Data |
---|---|---|
483fc223 S |
1 | from __future__ import unicode_literals |
2 | ||
8c3e35dd | 3 | import collections |
483fc223 | 4 | import json |
8c3e35dd | 5 | import os |
38eb2968 | 6 | import random |
483fc223 S |
7 | |
8 | from .common import InfoExtractor | |
9 | from ..compat import ( | |
10 | compat_str, | |
483fc223 S |
11 | compat_urlparse, |
12 | ) | |
13 | from ..utils import ( | |
425f3fdf | 14 | dict_get, |
483fc223 | 15 | ExtractorError, |
8c3e35dd | 16 | float_or_none, |
483fc223 S |
17 | int_or_none, |
18 | parse_duration, | |
756926ff | 19 | qualities, |
8c3e35dd | 20 | srt_subtitles_timecode, |
3d7e3aaa | 21 | update_url_query, |
6e6bc8da | 22 | urlencode_postdata, |
483fc223 S |
23 | ) |
24 | ||
25 | ||
563772ed | 26 | class PluralsightBaseIE(InfoExtractor): |
9df6b03c | 27 | _API_BASE = 'https://app.pluralsight.com' |
563772ed S |
28 | |
29 | ||
30 | class PluralsightIE(PluralsightBaseIE): | |
483fc223 | 31 | IE_NAME = 'pluralsight' |
b0dfcab6 | 32 | _VALID_URL = r'https?://(?:(?:www|app)\.)?pluralsight\.com/(?:training/)?player\?' |
c3a227d1 | 33 | _LOGIN_URL = 'https://app.pluralsight.com/id/' |
563772ed | 34 | |
483fc223 S |
35 | _NETRC_MACHINE = 'pluralsight' |
36 | ||
71bd93b8 | 37 | _TESTS = [{ |
483fc223 S |
38 | 'url': 'http://www.pluralsight.com/training/player?author=mike-mckeown&name=hosting-sql-server-windows-azure-iaas-m7-mgmt&mode=live&clip=3&course=hosting-sql-server-windows-azure-iaas', |
39 | 'md5': '4d458cf5cf4c593788672419a8dd4cf8', | |
40 | 'info_dict': { | |
41 | 'id': 'hosting-sql-server-windows-azure-iaas-m7-mgmt-04', | |
42 | 'ext': 'mp4', | |
8e5a7c5e | 43 | 'title': 'Demo Monitoring', |
483fc223 S |
44 | 'duration': 338, |
45 | }, | |
46 | 'skip': 'Requires pluralsight account credentials', | |
71bd93b8 S |
47 | }, { |
48 | 'url': 'https://app.pluralsight.com/training/player?course=angularjs-get-started&author=scott-allen&name=angularjs-get-started-m1-introduction&clip=0&mode=live', | |
49 | 'only_matching': True, | |
c23e2664 S |
50 | }, { |
51 | # available without pluralsight account | |
52 | 'url': 'http://app.pluralsight.com/training/player?author=scott-allen&name=angularjs-get-started-m1-introduction&mode=live&clip=0&course=angularjs-get-started', | |
53 | 'only_matching': True, | |
b0dfcab6 S |
54 | }, { |
55 | 'url': 'https://app.pluralsight.com/player?course=ccna-intro-networking&author=ross-bagurdes&name=ccna-intro-networking-m06&clip=0', | |
56 | 'only_matching': True, | |
71bd93b8 | 57 | }] |
483fc223 S |
58 | |
59 | def _real_initialize(self): | |
60 | self._login() | |
61 | ||
62 | def _login(self): | |
63 | (username, password) = self._get_login_info() | |
64 | if username is None: | |
c23e2664 | 65 | return |
483fc223 S |
66 | |
67 | login_page = self._download_webpage( | |
68 | self._LOGIN_URL, None, 'Downloading login page') | |
69 | ||
70 | login_form = self._hidden_inputs(login_page) | |
71 | ||
72 | login_form.update({ | |
244cd042 S |
73 | 'Username': username, |
74 | 'Password': password, | |
483fc223 S |
75 | }) |
76 | ||
77 | post_url = self._search_regex( | |
78 | r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, | |
79 | 'post url', default=self._LOGIN_URL, group='url') | |
80 | ||
81 | if not post_url.startswith('http'): | |
82 | post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url) | |
83 | ||
483fc223 | 84 | response = self._download_webpage( |
30317f48 S |
85 | post_url, None, 'Logging in as %s' % username, |
86 | data=urlencode_postdata(login_form), | |
87 | headers={'Content-Type': 'application/x-www-form-urlencoded'}) | |
483fc223 S |
88 | |
89 | error = self._search_regex( | |
90 | r'<span[^>]+class="field-validation-error"[^>]*>([^<]+)</span>', | |
91 | response, 'error message', default=None) | |
92 | if error: | |
93 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
94 | ||
7e508ff2 | 95 | if all(p not in response for p in ('__INITIAL_STATE__', '"currentUser"')): |
9dd5408c S |
96 | BLOCKED = 'Your account has been blocked due to suspicious activity' |
97 | if BLOCKED in response: | |
98 | raise ExtractorError( | |
99 | 'Unable to login: %s' % BLOCKED, expected=True) | |
7e508ff2 S |
100 | raise ExtractorError('Unable to log in') |
101 | ||
8c3e35dd S |
102 | def _get_subtitles(self, author, clip_id, lang, name, duration, video_id): |
103 | captions_post = { | |
104 | 'a': author, | |
105 | 'cn': clip_id, | |
106 | 'lc': lang, | |
107 | 'm': name, | |
108 | } | |
109 | captions = self._download_json( | |
9df6b03c | 110 | '%s/player/retrieve-captions' % self._API_BASE, video_id, |
8c3e35dd S |
111 | 'Downloading captions JSON', 'Unable to download captions JSON', |
112 | fatal=False, data=json.dumps(captions_post).encode('utf-8'), | |
113 | headers={'Content-Type': 'application/json;charset=utf-8'}) | |
114 | if captions: | |
115 | return { | |
116 | lang: [{ | |
117 | 'ext': 'json', | |
118 | 'data': json.dumps(captions), | |
119 | }, { | |
120 | 'ext': 'srt', | |
121 | 'data': self._convert_subtitles(duration, captions), | |
122 | }] | |
123 | } | |
124 | ||
125 | @staticmethod | |
126 | def _convert_subtitles(duration, subs): | |
127 | srt = '' | |
425f3fdf S |
128 | TIME_OFFSET_KEYS = ('displayTimeOffset', 'DisplayTimeOffset') |
129 | TEXT_KEYS = ('text', 'Text') | |
8c3e35dd S |
130 | for num, current in enumerate(subs): |
131 | current = subs[num] | |
425f3fdf S |
132 | start, text = ( |
133 | float_or_none(dict_get(current, TIME_OFFSET_KEYS)), | |
134 | dict_get(current, TEXT_KEYS)) | |
8c3e35dd S |
135 | if start is None or text is None: |
136 | continue | |
137 | end = duration if num == len(subs) - 1 else float_or_none( | |
425f3fdf | 138 | dict_get(subs[num + 1], TIME_OFFSET_KEYS)) |
30317f48 S |
139 | if end is None: |
140 | continue | |
8c3e35dd S |
141 | srt += os.linesep.join( |
142 | ( | |
143 | '%d' % num, | |
144 | '%s --> %s' % ( | |
145 | srt_subtitles_timecode(start), | |
146 | srt_subtitles_timecode(end)), | |
147 | text, | |
148 | os.linesep, | |
149 | )) | |
150 | return srt | |
151 | ||
483fc223 | 152 | def _real_extract(self, url): |
71bd93b8 S |
153 | qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) |
154 | ||
155 | author = qs.get('author', [None])[0] | |
156 | name = qs.get('name', [None])[0] | |
157 | clip_id = qs.get('clip', [None])[0] | |
9df6b03c | 158 | course_name = qs.get('course', [None])[0] |
71bd93b8 | 159 | |
9df6b03c | 160 | if any(not f for f in (author, name, clip_id, course_name,)): |
71bd93b8 | 161 | raise ExtractorError('Invalid URL', expected=True) |
483fc223 S |
162 | |
163 | display_id = '%s-%s' % (name, clip_id) | |
164 | ||
9df6b03c | 165 | course = self._download_json( |
b494d685 S |
166 | 'https://app.pluralsight.com/player/user/api/v1/player/payload', |
167 | display_id, data=urlencode_postdata({'courseId': course_name}), | |
168 | headers={'Referer': url}) | |
9df6b03c S |
169 | |
170 | collection = course['modules'] | |
483fc223 | 171 | |
d212c93d | 172 | clip = None |
483fc223 S |
173 | |
174 | for module_ in collection: | |
02f0da20 | 175 | if name in (module_.get('moduleName'), module_.get('name')): |
483fc223 S |
176 | for clip_ in module_.get('clips', []): |
177 | clip_index = clip_.get('clipIndex') | |
02f0da20 S |
178 | if clip_index is None: |
179 | clip_index = clip_.get('index') | |
483fc223 S |
180 | if clip_index is None: |
181 | continue | |
182 | if compat_str(clip_index) == clip_id: | |
183 | clip = clip_ | |
184 | break | |
185 | ||
186 | if not clip: | |
187 | raise ExtractorError('Unable to resolve clip') | |
188 | ||
8e5a7c5e | 189 | title = clip['title'] |
8c3e35dd | 190 | |
483fc223 S |
191 | QUALITIES = { |
192 | 'low': {'width': 640, 'height': 480}, | |
193 | 'medium': {'width': 848, 'height': 640}, | |
194 | 'high': {'width': 1024, 'height': 768}, | |
756926ff | 195 | 'high-widescreen': {'width': 1280, 'height': 720}, |
483fc223 S |
196 | } |
197 | ||
756926ff S |
198 | QUALITIES_PREFERENCE = ('low', 'medium', 'high', 'high-widescreen',) |
199 | quality_key = qualities(QUALITIES_PREFERENCE) | |
200 | ||
4c57b485 S |
201 | AllowedQuality = collections.namedtuple('AllowedQuality', ['ext', 'qualities']) |
202 | ||
483fc223 | 203 | ALLOWED_QUALITIES = ( |
756926ff S |
204 | AllowedQuality('webm', ['high', ]), |
205 | AllowedQuality('mp4', ['low', 'medium', 'high', ]), | |
483fc223 S |
206 | ) |
207 | ||
756926ff S |
208 | # Some courses also offer widescreen resolution for high quality (see |
209 | # https://github.com/rg3/youtube-dl/issues/7766) | |
9df6b03c | 210 | widescreen = course.get('supportsWideScreenVideoFormats') is True |
756926ff S |
211 | best_quality = 'high-widescreen' if widescreen else 'high' |
212 | if widescreen: | |
213 | for allowed_quality in ALLOWED_QUALITIES: | |
214 | allowed_quality.qualities.append(best_quality) | |
215 | ||
cf186b77 S |
216 | # In order to minimize the number of calls to ViewClip API and reduce |
217 | # the probability of being throttled or banned by Pluralsight we will request | |
0eebf34d | 218 | # only single format until formats listing was explicitly requested. |
4c57b485 S |
219 | if self._downloader.params.get('listformats', False): |
220 | allowed_qualities = ALLOWED_QUALITIES | |
221 | else: | |
222 | def guess_allowed_qualities(): | |
223 | req_format = self._downloader.params.get('format') or 'best' | |
edc70f4a | 224 | req_format_split = req_format.split('-', 1) |
4c57b485 S |
225 | if len(req_format_split) > 1: |
226 | req_ext, req_quality = req_format_split | |
227 | for allowed_quality in ALLOWED_QUALITIES: | |
228 | if req_ext == allowed_quality.ext and req_quality in allowed_quality.qualities: | |
229 | return (AllowedQuality(req_ext, (req_quality, )), ) | |
230 | req_ext = 'webm' if self._downloader.params.get('prefer_free_formats') else 'mp4' | |
756926ff | 231 | return (AllowedQuality(req_ext, (best_quality, )), ) |
4c57b485 S |
232 | allowed_qualities = guess_allowed_qualities() |
233 | ||
483fc223 | 234 | formats = [] |
756926ff S |
235 | for ext, qualities_ in allowed_qualities: |
236 | for quality in qualities_: | |
483fc223 S |
237 | f = QUALITIES[quality].copy() |
238 | clip_post = { | |
9df6b03c S |
239 | 'author': author, |
240 | 'includeCaptions': False, | |
241 | 'clipIndex': int(clip_id), | |
242 | 'courseName': course_name, | |
243 | 'locale': 'en', | |
244 | 'moduleName': name, | |
245 | 'mediaType': ext, | |
246 | 'quality': '%dx%d' % (f['width'], f['height']), | |
483fc223 | 247 | } |
483fc223 | 248 | format_id = '%s-%s' % (ext, quality) |
f8ae2c7f | 249 | viewclip = self._download_json( |
9df6b03c | 250 | '%s/video/clips/viewclip' % self._API_BASE, display_id, |
f8ae2c7f | 251 | 'Downloading %s viewclip JSON' % format_id, fatal=False, |
30317f48 S |
252 | data=json.dumps(clip_post).encode('utf-8'), |
253 | headers={'Content-Type': 'application/json;charset=utf-8'}) | |
38eb2968 S |
254 | |
255 | # Pluralsight tracks multiple sequential calls to ViewClip API and start | |
256 | # to return 429 HTTP errors after some time (see | |
257 | # https://github.com/rg3/youtube-dl/pull/6989). Moreover it may even lead | |
258 | # to account ban (see https://github.com/rg3/youtube-dl/issues/6842). | |
259 | # To somewhat reduce the probability of these consequences | |
260 | # we will sleep random amount of time before each call to ViewClip. | |
261 | self._sleep( | |
262 | random.randint(2, 5), display_id, | |
263 | '%(video_id)s: Waiting for %(timeout)s seconds to avoid throttling') | |
264 | ||
f8ae2c7f | 265 | if not viewclip: |
483fc223 | 266 | continue |
f8ae2c7f S |
267 | |
268 | clip_urls = viewclip.get('urls') | |
269 | if not isinstance(clip_urls, list): | |
270 | continue | |
271 | ||
272 | for clip_url_data in clip_urls: | |
273 | clip_url = clip_url_data.get('url') | |
274 | if not clip_url: | |
275 | continue | |
276 | cdn = clip_url_data.get('cdn') | |
277 | clip_f = f.copy() | |
278 | clip_f.update({ | |
279 | 'url': clip_url, | |
280 | 'ext': ext, | |
281 | 'format_id': '%s-%s' % (format_id, cdn) if cdn else format_id, | |
282 | 'quality': quality_key(quality), | |
283 | 'source_preference': int_or_none(clip_url_data.get('rank')), | |
284 | }) | |
285 | formats.append(clip_f) | |
286 | ||
483fc223 S |
287 | self._sort_formats(formats) |
288 | ||
8c3e35dd S |
289 | duration = int_or_none( |
290 | clip.get('duration')) or parse_duration(clip.get('formattedDuration')) | |
291 | ||
292 | # TODO: other languages? | |
293 | subtitles = self.extract_subtitles( | |
294 | author, clip_id, 'en', name, duration, display_id) | |
483fc223 S |
295 | |
296 | return { | |
bc0550c2 | 297 | 'id': clip.get('clipName') or clip['name'], |
8c3e35dd S |
298 | 'title': title, |
299 | 'duration': duration, | |
483fc223 | 300 | 'creator': author, |
8c3e35dd S |
301 | 'formats': formats, |
302 | 'subtitles': subtitles, | |
483fc223 S |
303 | } |
304 | ||
305 | ||
563772ed | 306 | class PluralsightCourseIE(PluralsightBaseIE): |
483fc223 | 307 | IE_NAME = 'pluralsight:course' |
a5cd0eb8 | 308 | _VALID_URL = r'https?://(?:(?:www|app)\.)?pluralsight\.com/(?:library/)?courses/(?P<id>[^/]+)' |
c23e2664 | 309 | _TESTS = [{ |
483fc223 S |
310 | # Free course from Pluralsight Starter Subscription for Microsoft TechNet |
311 | # https://offers.pluralsight.com/technet?loc=zTS3z&prod=zOTprodz&tech=zOttechz&prog=zOTprogz&type=zSOz&media=zOTmediaz&country=zUSz | |
312 | 'url': 'http://www.pluralsight.com/courses/hosting-sql-server-windows-azure-iaas', | |
313 | 'info_dict': { | |
314 | 'id': 'hosting-sql-server-windows-azure-iaas', | |
315 | 'title': 'Hosting SQL Server in Microsoft Azure IaaS Fundamentals', | |
316 | 'description': 'md5:61b37e60f21c4b2f91dc621a977d0986', | |
317 | }, | |
318 | 'playlist_count': 31, | |
c23e2664 S |
319 | }, { |
320 | # available without pluralsight account | |
321 | 'url': 'https://www.pluralsight.com/courses/angularjs-get-started', | |
322 | 'only_matching': True, | |
a5cd0eb8 S |
323 | }, { |
324 | 'url': 'https://app.pluralsight.com/library/courses/understanding-microsoft-azure-amazon-aws/table-of-contents', | |
325 | 'only_matching': True, | |
c23e2664 | 326 | }] |
483fc223 S |
327 | |
328 | def _real_extract(self, url): | |
329 | course_id = self._match_id(url) | |
330 | ||
2b6bda1e S |
331 | # TODO: PSM cookie |
332 | ||
483fc223 | 333 | course = self._download_json( |
3d7e3aaa S |
334 | '%s/player/functions/rpc' % self._API_BASE, course_id, |
335 | 'Downloading course JSON', | |
336 | data=json.dumps({ | |
337 | 'fn': 'bootstrapPlayer', | |
338 | 'payload': { | |
339 | 'courseId': course_id, | |
340 | } | |
341 | }).encode('utf-8'), | |
342 | headers={ | |
343 | 'Content-Type': 'application/json;charset=utf-8' | |
344 | })['payload']['course'] | |
483fc223 S |
345 | |
346 | title = course['title'] | |
3d7e3aaa S |
347 | course_name = course['name'] |
348 | course_data = course['modules'] | |
483fc223 S |
349 | description = course.get('description') or course.get('shortDescription') |
350 | ||
483fc223 | 351 | entries = [] |
8018028d | 352 | for num, module in enumerate(course_data, 1): |
3d7e3aaa S |
353 | author = module.get('author') |
354 | module_name = module.get('name') | |
355 | if not author or not module_name: | |
356 | continue | |
483fc223 | 357 | for clip in module.get('clips', []): |
3d7e3aaa S |
358 | clip_index = int_or_none(clip.get('index')) |
359 | if clip_index is None: | |
483fc223 | 360 | continue |
3d7e3aaa S |
361 | clip_url = update_url_query( |
362 | '%s/player' % self._API_BASE, query={ | |
363 | 'mode': 'live', | |
364 | 'course': course_name, | |
365 | 'author': author, | |
366 | 'name': module_name, | |
367 | 'clip': clip_index, | |
368 | }) | |
8018028d S |
369 | entries.append({ |
370 | '_type': 'url_transparent', | |
3d7e3aaa | 371 | 'url': clip_url, |
8018028d S |
372 | 'ie_key': PluralsightIE.ie_key(), |
373 | 'chapter': module.get('title'), | |
374 | 'chapter_number': num, | |
375 | 'chapter_id': module.get('moduleRef'), | |
376 | }) | |
483fc223 | 377 | |
483fc223 | 378 | return self.playlist_result(entries, course_id, title, description) |