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.

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