pluralsight.py 8.3 KB

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