]>
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 |
21c1a00d | 7 | import re |
483fc223 S |
8 | |
9 | from .common import InfoExtractor | |
10 | from ..compat import ( | |
11 | compat_str, | |
483fc223 S |
12 | compat_urlparse, |
13 | ) | |
14 | from ..utils import ( | |
425f3fdf | 15 | dict_get, |
483fc223 | 16 | ExtractorError, |
8c3e35dd | 17 | float_or_none, |
483fc223 S |
18 | int_or_none, |
19 | parse_duration, | |
756926ff | 20 | qualities, |
8c3e35dd | 21 | srt_subtitles_timecode, |
93d0583e | 22 | try_get, |
3d7e3aaa | 23 | update_url_query, |
6e6bc8da | 24 | urlencode_postdata, |
483fc223 S |
25 | ) |
26 | ||
27 | ||
563772ed | 28 | class PluralsightBaseIE(InfoExtractor): |
9df6b03c | 29 | _API_BASE = 'https://app.pluralsight.com' |
563772ed | 30 | |
836ef484 S |
31 | _GRAPHQL_EP = '%s/player/api/graphql' % _API_BASE |
32 | _GRAPHQL_HEADERS = { | |
33 | 'Content-Type': 'application/json;charset=UTF-8', | |
34 | } | |
35 | _GRAPHQL_COURSE_TMPL = ''' | |
36 | query BootstrapPlayer { | |
37 | rpc { | |
38 | bootstrapPlayer { | |
39 | profile { | |
40 | firstName | |
41 | lastName | |
42 | ||
43 | username | |
44 | userHandle | |
45 | authed | |
46 | isAuthed | |
47 | plan | |
48 | } | |
49 | course(courseId: "%s") { | |
50 | name | |
51 | title | |
52 | courseHasCaptions | |
53 | translationLanguages { | |
54 | code | |
55 | name | |
56 | } | |
57 | supportsWideScreenVideoFormats | |
58 | timestamp | |
59 | modules { | |
60 | name | |
61 | title | |
62 | duration | |
63 | formattedDuration | |
64 | author | |
65 | authorized | |
66 | clips { | |
67 | authorized | |
68 | clipId | |
69 | duration | |
70 | formattedDuration | |
71 | id | |
72 | index | |
73 | moduleIndex | |
74 | moduleTitle | |
75 | name | |
76 | title | |
77 | watched | |
78 | } | |
79 | } | |
80 | } | |
81 | } | |
82 | } | |
83 | }''' | |
84 | ||
93d0583e S |
85 | def _download_course(self, course_id, url, display_id): |
86 | try: | |
87 | return self._download_course_rpc(course_id, url, display_id) | |
88 | except ExtractorError: | |
89 | # Old API fallback | |
90 | return self._download_json( | |
91 | 'https://app.pluralsight.com/player/user/api/v1/player/payload', | |
92 | display_id, data=urlencode_postdata({'courseId': course_id}), | |
93 | headers={'Referer': url}) | |
94 | ||
95 | def _download_course_rpc(self, course_id, url, display_id): | |
96 | response = self._download_json( | |
836ef484 S |
97 | self._GRAPHQL_EP, display_id, data=json.dumps({ |
98 | 'query': self._GRAPHQL_COURSE_TMPL % course_id, | |
99 | 'variables': {} | |
100 | }).encode('utf-8'), headers=self._GRAPHQL_HEADERS) | |
101 | ||
102 | course = try_get( | |
103 | response, lambda x: x['data']['rpc']['bootstrapPlayer']['course'], | |
104 | dict) | |
93d0583e S |
105 | if course: |
106 | return course | |
107 | ||
108 | raise ExtractorError( | |
109 | '%s said: %s' % (self.IE_NAME, response['error']['message']), | |
110 | expected=True) | |
111 | ||
563772ed S |
112 | |
113 | class PluralsightIE(PluralsightBaseIE): | |
483fc223 | 114 | IE_NAME = 'pluralsight' |
b0dfcab6 | 115 | _VALID_URL = r'https?://(?:(?:www|app)\.)?pluralsight\.com/(?:training/)?player\?' |
c3a227d1 | 116 | _LOGIN_URL = 'https://app.pluralsight.com/id/' |
563772ed | 117 | |
483fc223 S |
118 | _NETRC_MACHINE = 'pluralsight' |
119 | ||
71bd93b8 | 120 | _TESTS = [{ |
483fc223 S |
121 | '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', |
122 | 'md5': '4d458cf5cf4c593788672419a8dd4cf8', | |
123 | 'info_dict': { | |
124 | 'id': 'hosting-sql-server-windows-azure-iaas-m7-mgmt-04', | |
125 | 'ext': 'mp4', | |
8e5a7c5e | 126 | 'title': 'Demo Monitoring', |
483fc223 S |
127 | 'duration': 338, |
128 | }, | |
129 | 'skip': 'Requires pluralsight account credentials', | |
71bd93b8 S |
130 | }, { |
131 | 'url': 'https://app.pluralsight.com/training/player?course=angularjs-get-started&author=scott-allen&name=angularjs-get-started-m1-introduction&clip=0&mode=live', | |
132 | 'only_matching': True, | |
c23e2664 S |
133 | }, { |
134 | # available without pluralsight account | |
135 | 'url': 'http://app.pluralsight.com/training/player?author=scott-allen&name=angularjs-get-started-m1-introduction&mode=live&clip=0&course=angularjs-get-started', | |
136 | 'only_matching': True, | |
b0dfcab6 S |
137 | }, { |
138 | 'url': 'https://app.pluralsight.com/player?course=ccna-intro-networking&author=ross-bagurdes&name=ccna-intro-networking-m06&clip=0', | |
139 | 'only_matching': True, | |
71bd93b8 | 140 | }] |
483fc223 | 141 | |
836ef484 S |
142 | GRAPHQL_VIEWCLIP_TMPL = ''' |
143 | query viewClip { | |
144 | viewClip(input: { | |
145 | author: "%(author)s", | |
146 | clipIndex: %(clipIndex)d, | |
147 | courseName: "%(courseName)s", | |
148 | includeCaptions: %(includeCaptions)s, | |
149 | locale: "%(locale)s", | |
150 | mediaType: "%(mediaType)s", | |
151 | moduleName: "%(moduleName)s", | |
152 | quality: "%(quality)s" | |
153 | }) { | |
154 | urls { | |
155 | url | |
156 | cdn | |
157 | rank | |
158 | source | |
159 | }, | |
160 | status | |
161 | } | |
162 | }''' | |
163 | ||
483fc223 S |
164 | def _real_initialize(self): |
165 | self._login() | |
166 | ||
167 | def _login(self): | |
68217024 | 168 | username, password = self._get_login_info() |
483fc223 | 169 | if username is None: |
c23e2664 | 170 | return |
483fc223 S |
171 | |
172 | login_page = self._download_webpage( | |
173 | self._LOGIN_URL, None, 'Downloading login page') | |
174 | ||
175 | login_form = self._hidden_inputs(login_page) | |
176 | ||
177 | login_form.update({ | |
244cd042 S |
178 | 'Username': username, |
179 | 'Password': password, | |
483fc223 S |
180 | }) |
181 | ||
182 | post_url = self._search_regex( | |
183 | r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, | |
184 | 'post url', default=self._LOGIN_URL, group='url') | |
185 | ||
186 | if not post_url.startswith('http'): | |
187 | post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url) | |
188 | ||
483fc223 | 189 | response = self._download_webpage( |
e4d95865 | 190 | post_url, None, 'Logging in', |
30317f48 S |
191 | data=urlencode_postdata(login_form), |
192 | headers={'Content-Type': 'application/x-www-form-urlencoded'}) | |
483fc223 S |
193 | |
194 | error = self._search_regex( | |
195 | r'<span[^>]+class="field-validation-error"[^>]*>([^<]+)</span>', | |
196 | response, 'error message', default=None) | |
197 | if error: | |
198 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
199 | ||
21c1a00d S |
200 | if all(not re.search(p, response) for p in ( |
201 | r'__INITIAL_STATE__', r'["\']currentUser["\']', | |
202 | # new layout? | |
203 | r'>\s*Sign out\s*<')): | |
9dd5408c S |
204 | BLOCKED = 'Your account has been blocked due to suspicious activity' |
205 | if BLOCKED in response: | |
206 | raise ExtractorError( | |
207 | 'Unable to login: %s' % BLOCKED, expected=True) | |
c94427dd S |
208 | MUST_AGREE = 'To continue using Pluralsight, you must agree to' |
209 | if any(p in response for p in (MUST_AGREE, '>Disagree<', '>Agree<')): | |
210 | raise ExtractorError( | |
211 | 'Unable to login: %s some documents. Go to pluralsight.com, ' | |
212 | 'log in and agree with what Pluralsight requires.' | |
213 | % MUST_AGREE, expected=True) | |
214 | ||
7e508ff2 S |
215 | raise ExtractorError('Unable to log in') |
216 | ||
36534313 S |
217 | def _get_subtitles(self, author, clip_idx, clip_id, lang, name, duration, video_id): |
218 | captions = None | |
219 | if clip_id: | |
220 | captions = self._download_json( | |
221 | '%s/transcript/api/v1/caption/json/%s/%s' | |
222 | % (self._API_BASE, clip_id, lang), video_id, | |
223 | 'Downloading captions JSON', 'Unable to download captions JSON', | |
224 | fatal=False) | |
225 | if not captions: | |
226 | captions_post = { | |
227 | 'a': author, | |
228 | 'cn': int(clip_idx), | |
229 | 'lc': lang, | |
230 | 'm': name, | |
231 | } | |
232 | captions = self._download_json( | |
233 | '%s/player/retrieve-captions' % self._API_BASE, video_id, | |
234 | 'Downloading captions JSON', 'Unable to download captions JSON', | |
235 | fatal=False, data=json.dumps(captions_post).encode('utf-8'), | |
236 | headers={'Content-Type': 'application/json;charset=utf-8'}) | |
8c3e35dd S |
237 | if captions: |
238 | return { | |
239 | lang: [{ | |
240 | 'ext': 'json', | |
241 | 'data': json.dumps(captions), | |
242 | }, { | |
243 | 'ext': 'srt', | |
244 | 'data': self._convert_subtitles(duration, captions), | |
245 | }] | |
246 | } | |
247 | ||
248 | @staticmethod | |
249 | def _convert_subtitles(duration, subs): | |
250 | srt = '' | |
425f3fdf S |
251 | TIME_OFFSET_KEYS = ('displayTimeOffset', 'DisplayTimeOffset') |
252 | TEXT_KEYS = ('text', 'Text') | |
8c3e35dd S |
253 | for num, current in enumerate(subs): |
254 | current = subs[num] | |
425f3fdf | 255 | start, text = ( |
2c8e11b4 | 256 | float_or_none(dict_get(current, TIME_OFFSET_KEYS, skip_false_values=False)), |
425f3fdf | 257 | dict_get(current, TEXT_KEYS)) |
8c3e35dd S |
258 | if start is None or text is None: |
259 | continue | |
260 | end = duration if num == len(subs) - 1 else float_or_none( | |
2c8e11b4 | 261 | dict_get(subs[num + 1], TIME_OFFSET_KEYS, skip_false_values=False)) |
30317f48 S |
262 | if end is None: |
263 | continue | |
8c3e35dd S |
264 | srt += os.linesep.join( |
265 | ( | |
266 | '%d' % num, | |
267 | '%s --> %s' % ( | |
268 | srt_subtitles_timecode(start), | |
269 | srt_subtitles_timecode(end)), | |
270 | text, | |
271 | os.linesep, | |
272 | )) | |
273 | return srt | |
274 | ||
483fc223 | 275 | def _real_extract(self, url): |
71bd93b8 S |
276 | qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query) |
277 | ||
278 | author = qs.get('author', [None])[0] | |
279 | name = qs.get('name', [None])[0] | |
a3f86160 | 280 | clip_idx = qs.get('clip', [None])[0] |
9df6b03c | 281 | course_name = qs.get('course', [None])[0] |
71bd93b8 | 282 | |
a3f86160 | 283 | if any(not f for f in (author, name, clip_idx, course_name,)): |
71bd93b8 | 284 | raise ExtractorError('Invalid URL', expected=True) |
483fc223 | 285 | |
a3f86160 | 286 | display_id = '%s-%s' % (name, clip_idx) |
483fc223 | 287 | |
93d0583e | 288 | course = self._download_course(course_name, url, display_id) |
9df6b03c S |
289 | |
290 | collection = course['modules'] | |
483fc223 | 291 | |
d212c93d | 292 | clip = None |
483fc223 S |
293 | |
294 | for module_ in collection: | |
02f0da20 | 295 | if name in (module_.get('moduleName'), module_.get('name')): |
483fc223 S |
296 | for clip_ in module_.get('clips', []): |
297 | clip_index = clip_.get('clipIndex') | |
02f0da20 S |
298 | if clip_index is None: |
299 | clip_index = clip_.get('index') | |
483fc223 S |
300 | if clip_index is None: |
301 | continue | |
a3f86160 | 302 | if compat_str(clip_index) == clip_idx: |
483fc223 S |
303 | clip = clip_ |
304 | break | |
305 | ||
306 | if not clip: | |
307 | raise ExtractorError('Unable to resolve clip') | |
308 | ||
8e5a7c5e | 309 | title = clip['title'] |
a3f86160 | 310 | clip_id = clip.get('clipName') or clip.get('name') or clip['clipId'] |
8c3e35dd | 311 | |
483fc223 S |
312 | QUALITIES = { |
313 | 'low': {'width': 640, 'height': 480}, | |
314 | 'medium': {'width': 848, 'height': 640}, | |
315 | 'high': {'width': 1024, 'height': 768}, | |
756926ff | 316 | 'high-widescreen': {'width': 1280, 'height': 720}, |
483fc223 S |
317 | } |
318 | ||
756926ff S |
319 | QUALITIES_PREFERENCE = ('low', 'medium', 'high', 'high-widescreen',) |
320 | quality_key = qualities(QUALITIES_PREFERENCE) | |
321 | ||
4c57b485 S |
322 | AllowedQuality = collections.namedtuple('AllowedQuality', ['ext', 'qualities']) |
323 | ||
483fc223 | 324 | ALLOWED_QUALITIES = ( |
756926ff S |
325 | AllowedQuality('webm', ['high', ]), |
326 | AllowedQuality('mp4', ['low', 'medium', 'high', ]), | |
483fc223 S |
327 | ) |
328 | ||
756926ff | 329 | # Some courses also offer widescreen resolution for high quality (see |
067aa17e | 330 | # https://github.com/ytdl-org/youtube-dl/issues/7766) |
9df6b03c | 331 | widescreen = course.get('supportsWideScreenVideoFormats') is True |
756926ff S |
332 | best_quality = 'high-widescreen' if widescreen else 'high' |
333 | if widescreen: | |
334 | for allowed_quality in ALLOWED_QUALITIES: | |
335 | allowed_quality.qualities.append(best_quality) | |
336 | ||
cf186b77 S |
337 | # In order to minimize the number of calls to ViewClip API and reduce |
338 | # the probability of being throttled or banned by Pluralsight we will request | |
0eebf34d | 339 | # only single format until formats listing was explicitly requested. |
a06916d9 | 340 | if self.get_param('listformats', False): |
4c57b485 S |
341 | allowed_qualities = ALLOWED_QUALITIES |
342 | else: | |
343 | def guess_allowed_qualities(): | |
a06916d9 | 344 | req_format = self.get_param('format') or 'best' |
edc70f4a | 345 | req_format_split = req_format.split('-', 1) |
4c57b485 S |
346 | if len(req_format_split) > 1: |
347 | req_ext, req_quality = req_format_split | |
fac188c6 | 348 | req_quality = '-'.join(req_quality.split('-')[:2]) |
4c57b485 S |
349 | for allowed_quality in ALLOWED_QUALITIES: |
350 | if req_ext == allowed_quality.ext and req_quality in allowed_quality.qualities: | |
351 | return (AllowedQuality(req_ext, (req_quality, )), ) | |
a06916d9 | 352 | req_ext = 'webm' if self.get_param('prefer_free_formats') else 'mp4' |
756926ff | 353 | return (AllowedQuality(req_ext, (best_quality, )), ) |
4c57b485 S |
354 | allowed_qualities = guess_allowed_qualities() |
355 | ||
483fc223 | 356 | formats = [] |
756926ff S |
357 | for ext, qualities_ in allowed_qualities: |
358 | for quality in qualities_: | |
483fc223 S |
359 | f = QUALITIES[quality].copy() |
360 | clip_post = { | |
9df6b03c | 361 | 'author': author, |
836ef484 | 362 | 'includeCaptions': 'false', |
a3f86160 | 363 | 'clipIndex': int(clip_idx), |
9df6b03c S |
364 | 'courseName': course_name, |
365 | 'locale': 'en', | |
366 | 'moduleName': name, | |
367 | 'mediaType': ext, | |
368 | 'quality': '%dx%d' % (f['width'], f['height']), | |
483fc223 | 369 | } |
483fc223 | 370 | format_id = '%s-%s' % (ext, quality) |
836ef484 S |
371 | |
372 | try: | |
373 | viewclip = self._download_json( | |
374 | self._GRAPHQL_EP, display_id, | |
375 | 'Downloading %s viewclip graphql' % format_id, | |
376 | data=json.dumps({ | |
377 | 'query': self.GRAPHQL_VIEWCLIP_TMPL % clip_post, | |
378 | 'variables': {} | |
379 | }).encode('utf-8'), | |
380 | headers=self._GRAPHQL_HEADERS)['data']['viewClip'] | |
381 | except ExtractorError: | |
382 | # Still works but most likely will go soon | |
383 | viewclip = self._download_json( | |
384 | '%s/video/clips/viewclip' % self._API_BASE, display_id, | |
385 | 'Downloading %s viewclip JSON' % format_id, fatal=False, | |
386 | data=json.dumps(clip_post).encode('utf-8'), | |
387 | headers={'Content-Type': 'application/json;charset=utf-8'}) | |
38eb2968 S |
388 | |
389 | # Pluralsight tracks multiple sequential calls to ViewClip API and start | |
390 | # to return 429 HTTP errors after some time (see | |
067aa17e S |
391 | # https://github.com/ytdl-org/youtube-dl/pull/6989). Moreover it may even lead |
392 | # to account ban (see https://github.com/ytdl-org/youtube-dl/issues/6842). | |
38eb2968 S |
393 | # To somewhat reduce the probability of these consequences |
394 | # we will sleep random amount of time before each call to ViewClip. | |
395 | self._sleep( | |
201c1459 | 396 | random.randint(5, 10), display_id, |
38eb2968 S |
397 | '%(video_id)s: Waiting for %(timeout)s seconds to avoid throttling') |
398 | ||
f8ae2c7f | 399 | if not viewclip: |
483fc223 | 400 | continue |
f8ae2c7f S |
401 | |
402 | clip_urls = viewclip.get('urls') | |
403 | if not isinstance(clip_urls, list): | |
404 | continue | |
405 | ||
406 | for clip_url_data in clip_urls: | |
407 | clip_url = clip_url_data.get('url') | |
408 | if not clip_url: | |
409 | continue | |
410 | cdn = clip_url_data.get('cdn') | |
411 | clip_f = f.copy() | |
412 | clip_f.update({ | |
413 | 'url': clip_url, | |
414 | 'ext': ext, | |
415 | 'format_id': '%s-%s' % (format_id, cdn) if cdn else format_id, | |
416 | 'quality': quality_key(quality), | |
417 | 'source_preference': int_or_none(clip_url_data.get('rank')), | |
418 | }) | |
419 | formats.append(clip_f) | |
420 | ||
483fc223 S |
421 | self._sort_formats(formats) |
422 | ||
8c3e35dd S |
423 | duration = int_or_none( |
424 | clip.get('duration')) or parse_duration(clip.get('formattedDuration')) | |
425 | ||
426 | # TODO: other languages? | |
427 | subtitles = self.extract_subtitles( | |
36534313 | 428 | author, clip_idx, clip.get('clipId'), 'en', name, duration, display_id) |
483fc223 S |
429 | |
430 | return { | |
a3f86160 | 431 | 'id': clip_id, |
8c3e35dd S |
432 | 'title': title, |
433 | 'duration': duration, | |
483fc223 | 434 | 'creator': author, |
8c3e35dd S |
435 | 'formats': formats, |
436 | 'subtitles': subtitles, | |
483fc223 S |
437 | } |
438 | ||
439 | ||
563772ed | 440 | class PluralsightCourseIE(PluralsightBaseIE): |
483fc223 | 441 | IE_NAME = 'pluralsight:course' |
a5cd0eb8 | 442 | _VALID_URL = r'https?://(?:(?:www|app)\.)?pluralsight\.com/(?:library/)?courses/(?P<id>[^/]+)' |
c23e2664 | 443 | _TESTS = [{ |
483fc223 S |
444 | # Free course from Pluralsight Starter Subscription for Microsoft TechNet |
445 | # https://offers.pluralsight.com/technet?loc=zTS3z&prod=zOTprodz&tech=zOttechz&prog=zOTprogz&type=zSOz&media=zOTmediaz&country=zUSz | |
446 | 'url': 'http://www.pluralsight.com/courses/hosting-sql-server-windows-azure-iaas', | |
447 | 'info_dict': { | |
448 | 'id': 'hosting-sql-server-windows-azure-iaas', | |
449 | 'title': 'Hosting SQL Server in Microsoft Azure IaaS Fundamentals', | |
450 | 'description': 'md5:61b37e60f21c4b2f91dc621a977d0986', | |
451 | }, | |
452 | 'playlist_count': 31, | |
c23e2664 S |
453 | }, { |
454 | # available without pluralsight account | |
455 | 'url': 'https://www.pluralsight.com/courses/angularjs-get-started', | |
456 | 'only_matching': True, | |
a5cd0eb8 S |
457 | }, { |
458 | 'url': 'https://app.pluralsight.com/library/courses/understanding-microsoft-azure-amazon-aws/table-of-contents', | |
459 | 'only_matching': True, | |
c23e2664 | 460 | }] |
483fc223 S |
461 | |
462 | def _real_extract(self, url): | |
463 | course_id = self._match_id(url) | |
464 | ||
2b6bda1e S |
465 | # TODO: PSM cookie |
466 | ||
93d0583e | 467 | course = self._download_course(course_id, url, course_id) |
483fc223 S |
468 | |
469 | title = course['title'] | |
3d7e3aaa S |
470 | course_name = course['name'] |
471 | course_data = course['modules'] | |
483fc223 S |
472 | description = course.get('description') or course.get('shortDescription') |
473 | ||
483fc223 | 474 | entries = [] |
8018028d | 475 | for num, module in enumerate(course_data, 1): |
3d7e3aaa S |
476 | author = module.get('author') |
477 | module_name = module.get('name') | |
478 | if not author or not module_name: | |
479 | continue | |
483fc223 | 480 | for clip in module.get('clips', []): |
3d7e3aaa S |
481 | clip_index = int_or_none(clip.get('index')) |
482 | if clip_index is None: | |
483fc223 | 483 | continue |
3d7e3aaa S |
484 | clip_url = update_url_query( |
485 | '%s/player' % self._API_BASE, query={ | |
486 | 'mode': 'live', | |
487 | 'course': course_name, | |
488 | 'author': author, | |
489 | 'name': module_name, | |
490 | 'clip': clip_index, | |
491 | }) | |
8018028d S |
492 | entries.append({ |
493 | '_type': 'url_transparent', | |
3d7e3aaa | 494 | 'url': clip_url, |
8018028d S |
495 | 'ie_key': PluralsightIE.ie_key(), |
496 | 'chapter': module.get('title'), | |
497 | 'chapter_number': num, | |
498 | 'chapter_id': module.get('moduleRef'), | |
499 | }) | |
483fc223 | 500 | |
483fc223 | 501 | return self.playlist_result(entries, course_id, title, description) |