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.

214 lines
8.3 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_str,
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. float_or_none,
  13. mimetype2ext,
  14. unescapeHTML,
  15. unsmuggle_url,
  16. urljoin,
  17. )
  18. class MediasiteIE(InfoExtractor):
  19. _VALID_URL = r'(?xi)https?://[^/]+/Mediasite/Play/(?P<id>[0-9a-f]{32,34})(?P<query>\?[^#]+|)'
  20. _TESTS = [
  21. {
  22. 'url': 'https://hitsmediaweb.h-its.org/mediasite/Play/2db6c271681e4f199af3c60d1f82869b1d',
  23. 'info_dict': {
  24. 'id': '2db6c271681e4f199af3c60d1f82869b1d',
  25. 'ext': 'mp4',
  26. 'title': 'Lecture: Tuesday, September 20, 2016 - Sir Andrew Wiles',
  27. 'description': 'Sir Andrew Wiles: “Equations in arithmetic”\\n\\nI will describe some of the interactions between modern number theory and the problem of solving equations in rational numbers or integers\\u0027.',
  28. 'timestamp': 1474268400.0,
  29. 'upload_date': '20160919',
  30. },
  31. },
  32. {
  33. 'url': 'http://mediasite.uib.no/Mediasite/Play/90bb363295d945d6b548c867d01181361d?catalog=a452b7df-9ae1-46b7-a3ba-aceeb285f3eb',
  34. 'info_dict': {
  35. 'id': '90bb363295d945d6b548c867d01181361d',
  36. 'ext': 'mp4',
  37. 'upload_date': '20150429',
  38. 'title': '5) IT-forum 2015-Dag 1 - Dungbeetle - How and why Rain created a tiny bug tracker for Unity',
  39. 'timestamp': 1430311380.0,
  40. },
  41. },
  42. {
  43. 'url': 'https://collegerama.tudelft.nl/Mediasite/Play/585a43626e544bdd97aeb71a0ec907a01d',
  44. 'md5': '481fda1c11f67588c0d9d8fbdced4e39',
  45. 'info_dict': {
  46. 'id': '585a43626e544bdd97aeb71a0ec907a01d',
  47. 'ext': 'mp4',
  48. 'title': 'Een nieuwe wereld: waarden, bewustzijn en techniek van de mensheid 2.0.',
  49. 'description': '',
  50. 'thumbnail': r're:^https?://.*\.jpg(?:\?.*)?$',
  51. 'duration': 7713.088,
  52. 'timestamp': 1413309600,
  53. 'upload_date': '20141014',
  54. },
  55. },
  56. {
  57. 'url': 'https://collegerama.tudelft.nl/Mediasite/Play/86a9ea9f53e149079fbdb4202b521ed21d?catalog=fd32fd35-6c99-466c-89d4-cd3c431bc8a4',
  58. 'md5': 'ef1fdded95bdf19b12c5999949419c92',
  59. 'info_dict': {
  60. 'id': '86a9ea9f53e149079fbdb4202b521ed21d',
  61. 'ext': 'wmv',
  62. 'title': '64ste Vakantiecursus: Afvalwater',
  63. 'description': 'md5:7fd774865cc69d972f542b157c328305',
  64. 'thumbnail': r're:^https?://.*\.jpg(?:\?.*?)?$',
  65. 'duration': 10853,
  66. 'timestamp': 1326446400,
  67. 'upload_date': '20120113',
  68. },
  69. },
  70. {
  71. 'url': 'http://digitalops.sandia.gov/Mediasite/Play/24aace4429fc450fb5b38cdbf424a66e1d',
  72. 'md5': '9422edc9b9a60151727e4b6d8bef393d',
  73. 'info_dict': {
  74. 'id': '24aace4429fc450fb5b38cdbf424a66e1d',
  75. 'ext': 'mp4',
  76. 'title': 'Xyce Software Training - Section 1',
  77. 'description': r're:(?s)SAND Number: SAND 2013-7800.{200,}',
  78. 'upload_date': '20120409',
  79. 'timestamp': 1333983600,
  80. 'duration': 7794,
  81. }
  82. }
  83. ]
  84. # look in Mediasite.Core.js (Mediasite.ContentStreamType[*])
  85. _STREAM_TYPES = {
  86. 0: 'video1', # the main video
  87. 2: 'slide',
  88. 3: 'presentation',
  89. 4: 'video2', # screencast?
  90. 5: 'video3',
  91. }
  92. @staticmethod
  93. def _extract_urls(webpage):
  94. return [
  95. unescapeHTML(mobj.group('url'))
  96. for mobj in re.finditer(
  97. r'(?xi)<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:(?:https?:)?//[^/]+)?/Mediasite/Play/[0-9a-f]{32,34}(?:\?.*?)?)\1',
  98. webpage)]
  99. def _real_extract(self, url):
  100. url, data = unsmuggle_url(url, {})
  101. mobj = re.match(self._VALID_URL, url)
  102. resource_id = mobj.group('id')
  103. query = mobj.group('query')
  104. webpage, urlh = self._download_webpage_handle(url, resource_id) # XXX: add UrlReferrer?
  105. redirect_url = compat_str(urlh.geturl())
  106. # XXX: might have also extracted UrlReferrer and QueryString from the html
  107. service_path = compat_urlparse.urljoin(redirect_url, self._html_search_regex(
  108. r'<div[^>]+\bid=["\']ServicePath[^>]+>(.+?)</div>', webpage, resource_id,
  109. default='/Mediasite/PlayerService/PlayerService.svc/json'))
  110. player_options = self._download_json(
  111. '%s/GetPlayerOptions' % service_path, resource_id,
  112. headers={
  113. 'Content-type': 'application/json; charset=utf-8',
  114. 'X-Requested-With': 'XMLHttpRequest',
  115. },
  116. data=json.dumps({
  117. 'getPlayerOptionsRequest': {
  118. 'ResourceId': resource_id,
  119. 'QueryString': query,
  120. 'UrlReferrer': data.get('UrlReferrer', ''),
  121. 'UseScreenReader': False,
  122. }
  123. }).encode('utf-8'))['d']
  124. presentation = player_options['Presentation']
  125. title = presentation['Title']
  126. if presentation is None:
  127. raise ExtractorError(
  128. 'Mediasite says: %s' % player_options['PlayerPresentationStatusMessage'],
  129. expected=True)
  130. thumbnails = []
  131. formats = []
  132. for snum, Stream in enumerate(presentation['Streams']):
  133. stream_type = Stream.get('StreamType')
  134. if stream_type is None:
  135. continue
  136. video_urls = Stream.get('VideoUrls')
  137. if not isinstance(video_urls, list):
  138. video_urls = []
  139. stream_id = self._STREAM_TYPES.get(
  140. stream_type, 'type%u' % stream_type)
  141. stream_formats = []
  142. for unum, VideoUrl in enumerate(video_urls):
  143. video_url = VideoUrl.get('Location')
  144. if not video_url or not isinstance(video_url, compat_str):
  145. continue
  146. # XXX: if Stream.get('CanChangeScheme', False), switch scheme to HTTP/HTTPS
  147. media_type = VideoUrl.get('MediaType')
  148. if media_type == 'SS':
  149. stream_formats.extend(self._extract_ism_formats(
  150. video_url, resource_id,
  151. ism_id='%s-%u.%u' % (stream_id, snum, unum),
  152. fatal=False))
  153. elif media_type == 'Dash':
  154. stream_formats.extend(self._extract_mpd_formats(
  155. video_url, resource_id,
  156. mpd_id='%s-%u.%u' % (stream_id, snum, unum),
  157. fatal=False))
  158. else:
  159. stream_formats.append({
  160. 'format_id': '%s-%u.%u' % (stream_id, snum, unum),
  161. 'url': video_url,
  162. 'ext': mimetype2ext(VideoUrl.get('MimeType')),
  163. })
  164. # TODO: if Stream['HasSlideContent']:
  165. # synthesise an MJPEG video stream '%s-%u.slides' % (stream_type, snum)
  166. # from Stream['Slides']
  167. # this will require writing a custom downloader...
  168. # disprefer 'secondary' streams
  169. if stream_type != 0:
  170. for fmt in stream_formats:
  171. fmt['preference'] = -1
  172. thumbnail_url = Stream.get('ThumbnailUrl')
  173. if thumbnail_url:
  174. thumbnails.append({
  175. 'id': '%s-%u' % (stream_id, snum),
  176. 'url': urljoin(redirect_url, thumbnail_url),
  177. 'preference': -1 if stream_type != 0 else 0,
  178. })
  179. formats.extend(stream_formats)
  180. self._sort_formats(formats)
  181. # XXX: Presentation['Presenters']
  182. # XXX: Presentation['Transcript']
  183. return {
  184. 'id': resource_id,
  185. 'title': title,
  186. 'description': presentation.get('Description'),
  187. 'duration': float_or_none(presentation.get('Duration'), 1000),
  188. 'timestamp': float_or_none(presentation.get('UnixTime'), 1000),
  189. 'formats': formats,
  190. 'thumbnails': thumbnails,
  191. }