pluralsight.py 8.1 KB

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