pluralsight.py 8.3 KB

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