[pluralsight] Update some more URLs
[youtube-dl] / youtube_dl / extractor / pluralsight.py
1 from __future__ import unicode_literals
2
3 import json
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_str,
8     compat_urllib_parse,
9     compat_urllib_request,
10     compat_urlparse,
11 )
12 from ..utils import (
13     ExtractorError,
14     int_or_none,
15     parse_duration,
16 )
17
18
19 class PluralsightIE(InfoExtractor):
20     IE_NAME = 'pluralsight'
21     _VALID_URL = r'https?://(?:(?:www|app)\.)?pluralsight\.com/training/player\?'
22     _LOGIN_URL = 'https://app.pluralsight.com/id/'
23     _API_BASE = 'http://app.pluralsight.com'
24     _NETRC_MACHINE = 'pluralsight'
25
26     _TESTS = [{
27         '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',
28         'md5': '4d458cf5cf4c593788672419a8dd4cf8',
29         'info_dict': {
30             'id': 'hosting-sql-server-windows-azure-iaas-m7-mgmt-04',
31             'ext': 'mp4',
32             'title': 'Management of SQL Server - Demo Monitoring',
33             'duration': 338,
34         },
35         'skip': 'Requires pluralsight account credentials',
36     }, {
37         'url': 'https://app.pluralsight.com/training/player?course=angularjs-get-started&author=scott-allen&name=angularjs-get-started-m1-introduction&clip=0&mode=live',
38         'only_matching': True,
39     }, {
40         # available without pluralsight account
41         'url': 'http://app.pluralsight.com/training/player?author=scott-allen&name=angularjs-get-started-m1-introduction&mode=live&clip=0&course=angularjs-get-started',
42         'only_matching': True,
43     }]
44
45     def _real_initialize(self):
46         self._login()
47
48     def _login(self):
49         (username, password) = self._get_login_info()
50         if username is None:
51             return
52
53         login_page = self._download_webpage(
54             self._LOGIN_URL, None, 'Downloading login page')
55
56         login_form = self._hidden_inputs(login_page)
57
58         login_form.update({
59             'Username': username.encode('utf-8'),
60             'Password': password.encode('utf-8'),
61         })
62
63         post_url = self._search_regex(
64             r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page,
65             'post url', default=self._LOGIN_URL, group='url')
66
67         if not post_url.startswith('http'):
68             post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
69
70         request = compat_urllib_request.Request(
71             post_url, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
72         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
73
74         response = self._download_webpage(
75             request, None, 'Logging in as %s' % username)
76
77         error = self._search_regex(
78             r'<span[^>]+class="field-validation-error"[^>]*>([^<]+)</span>',
79             response, 'error message', default=None)
80         if error:
81             raise ExtractorError('Unable to login: %s' % error, expected=True)
82
83     def _real_extract(self, url):
84         qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
85
86         author = qs.get('author', [None])[0]
87         name = qs.get('name', [None])[0]
88         clip_id = qs.get('clip', [None])[0]
89         course = qs.get('course', [None])[0]
90
91         if any(not f for f in (author, name, clip_id, course,)):
92             raise ExtractorError('Invalid URL', expected=True)
93
94         display_id = '%s-%s' % (name, clip_id)
95
96         webpage = self._download_webpage(url, display_id)
97
98         collection = self._parse_json(
99             self._search_regex(
100                 r'moduleCollection\s*:\s*new\s+ModuleCollection\((\[.+?\])\s*,\s*\$rootScope\)',
101                 webpage, 'modules'),
102             display_id)
103
104         module, clip = None, None
105
106         for module_ in collection:
107             if module_.get('moduleName') == name:
108                 module = module_
109                 for clip_ in module_.get('clips', []):
110                     clip_index = clip_.get('clipIndex')
111                     if clip_index is None:
112                         continue
113                     if compat_str(clip_index) == clip_id:
114                         clip = clip_
115                         break
116
117         if not clip:
118             raise ExtractorError('Unable to resolve clip')
119
120         QUALITIES = {
121             'low': {'width': 640, 'height': 480},
122             'medium': {'width': 848, 'height': 640},
123             'high': {'width': 1024, 'height': 768},
124         }
125
126         ALLOWED_QUALITIES = (
127             ('webm', ('high',)),
128             ('mp4', ('low', 'medium', 'high',)),
129         )
130
131         formats = []
132         for ext, qualities in ALLOWED_QUALITIES:
133             for quality in qualities:
134                 f = QUALITIES[quality].copy()
135                 clip_post = {
136                     'a': author,
137                     'cap': 'false',
138                     'cn': clip_id,
139                     'course': course,
140                     'lc': 'en',
141                     'm': name,
142                     'mt': ext,
143                     'q': '%dx%d' % (f['width'], f['height']),
144                 }
145                 request = compat_urllib_request.Request(
146                     '%s/training/Player/ViewClip' % self._API_BASE,
147                     json.dumps(clip_post).encode('utf-8'))
148                 request.add_header('Content-Type', 'application/json;charset=utf-8')
149                 format_id = '%s-%s' % (ext, quality)
150                 clip_url = self._download_webpage(
151                     request, display_id, 'Downloading %s URL' % format_id, fatal=False)
152                 if not clip_url:
153                     continue
154                 f.update({
155                     'url': clip_url,
156                     'ext': ext,
157                     'format_id': format_id,
158                 })
159                 formats.append(f)
160         self._sort_formats(formats)
161
162         # TODO: captions
163         # http://www.pluralsight.com/training/Player/ViewClip + cap = true
164         # or
165         # http://www.pluralsight.com/training/Player/Captions
166         # { a = author, cn = clip_id, lc = end, m = name }
167
168         return {
169             'id': clip['clipName'],
170             'title': '%s - %s' % (module['title'], clip['title']),
171             'duration': int_or_none(clip.get('duration')) or parse_duration(clip.get('formattedDuration')),
172             'creator': author,
173             'formats': formats
174         }
175
176
177 class PluralsightCourseIE(InfoExtractor):
178     IE_NAME = 'pluralsight:course'
179     _VALID_URL = r'https?://(?:(?:www|app)\.)?pluralsight\.com/(?:library/)?courses/(?P<id>[^/]+)'
180     _TESTS = [{
181         # Free course from Pluralsight Starter Subscription for Microsoft TechNet
182         # https://offers.pluralsight.com/technet?loc=zTS3z&prod=zOTprodz&tech=zOttechz&prog=zOTprogz&type=zSOz&media=zOTmediaz&country=zUSz
183         'url': 'http://www.pluralsight.com/courses/hosting-sql-server-windows-azure-iaas',
184         'info_dict': {
185             'id': 'hosting-sql-server-windows-azure-iaas',
186             'title': 'Hosting SQL Server in Microsoft Azure IaaS Fundamentals',
187             'description': 'md5:61b37e60f21c4b2f91dc621a977d0986',
188         },
189         'playlist_count': 31,
190     }, {
191         # available without pluralsight account
192         'url': 'https://www.pluralsight.com/courses/angularjs-get-started',
193         'only_matching': True,
194     }, {
195         'url': 'https://app.pluralsight.com/library/courses/understanding-microsoft-azure-amazon-aws/table-of-contents',
196         'only_matching': True,
197     }]
198
199     def _real_extract(self, url):
200         course_id = self._match_id(url)
201
202         # TODO: PSM cookie
203
204         course = self._download_json(
205             '%s/data/course/%s' % (self._API_BASE, course_id),
206             course_id, 'Downloading course JSON')
207
208         title = course['title']
209         description = course.get('description') or course.get('shortDescription')
210
211         course_data = self._download_json(
212             '%s/data/course/content/%s' % (self._API_BASE, course_id),
213             course_id, 'Downloading course data JSON')
214
215         entries = []
216         for module in course_data:
217             for clip in module.get('clips', []):
218                 player_parameters = clip.get('playerParameters')
219                 if not player_parameters:
220                     continue
221                 entries.append(self.url_result(
222                     '%s/training/player?%s' % (self._API_BASE, player_parameters),
223                     'Pluralsight'))
224
225         return self.playlist_result(entries, course_id, title, description)