You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.4 KiB

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