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