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