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.

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