]>
Commit | Line | Data |
---|---|---|
8056c854 | 1 | import re |
2 | import json | |
3 | ||
4 | from .common import InfoExtractor | |
2ca7ed41 S |
5 | from ..compat import ( |
6 | compat_str, | |
7 | compat_urlparse, | |
8 | ) | |
8056c854 | 9 | from ..utils import ( |
10 | ExtractorError, | |
8056c854 | 11 | float_or_none, |
2ca7ed41 | 12 | mimetype2ext, |
4014a486 | 13 | str_or_none, |
c4f60dd7 | 14 | try_call, |
4014a486 | 15 | try_get, |
2ca7ed41 S |
16 | unescapeHTML, |
17 | unsmuggle_url, | |
3052a30d | 18 | url_or_none, |
2ca7ed41 | 19 | urljoin, |
8056c854 | 20 | ) |
21 | ||
22 | ||
efee62ac | 23 | _ID_RE = r'(?:[0-9a-f]{32,34}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12,14})' |
4014a486 S |
24 | |
25 | ||
8056c854 | 26 | class MediasiteIE(InfoExtractor): |
c72967d5 | 27 | _VALID_URL = r'(?xi)https?://[^/]+/Mediasite/(?:Play|Showcase/[^/#?]+/Presentation)/(?P<id>%s)(?P<query>\?[^#]+|)' % _ID_RE |
8056c854 | 28 | _TESTS = [ |
29 | { | |
30 | 'url': 'https://hitsmediaweb.h-its.org/mediasite/Play/2db6c271681e4f199af3c60d1f82869b1d', | |
31 | 'info_dict': { | |
32 | 'id': '2db6c271681e4f199af3c60d1f82869b1d', | |
33 | 'ext': 'mp4', | |
34 | 'title': 'Lecture: Tuesday, September 20, 2016 - Sir Andrew Wiles', | |
35 | '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.', | |
36 | 'timestamp': 1474268400.0, | |
37 | 'upload_date': '20160919', | |
38 | }, | |
39 | }, | |
40 | { | |
41 | 'url': 'http://mediasite.uib.no/Mediasite/Play/90bb363295d945d6b548c867d01181361d?catalog=a452b7df-9ae1-46b7-a3ba-aceeb285f3eb', | |
42 | 'info_dict': { | |
43 | 'id': '90bb363295d945d6b548c867d01181361d', | |
44 | 'ext': 'mp4', | |
45 | 'upload_date': '20150429', | |
46 | 'title': '5) IT-forum 2015-Dag 1 - Dungbeetle - How and why Rain created a tiny bug tracker for Unity', | |
47 | 'timestamp': 1430311380.0, | |
48 | }, | |
49 | }, | |
50 | { | |
51 | 'url': 'https://collegerama.tudelft.nl/Mediasite/Play/585a43626e544bdd97aeb71a0ec907a01d', | |
52 | 'md5': '481fda1c11f67588c0d9d8fbdced4e39', | |
53 | 'info_dict': { | |
54 | 'id': '585a43626e544bdd97aeb71a0ec907a01d', | |
55 | 'ext': 'mp4', | |
56 | 'title': 'Een nieuwe wereld: waarden, bewustzijn en techniek van de mensheid 2.0.', | |
57 | 'description': '', | |
58 | 'thumbnail': r're:^https?://.*\.jpg(?:\?.*)?$', | |
59 | 'duration': 7713.088, | |
60 | 'timestamp': 1413309600, | |
61 | 'upload_date': '20141014', | |
62 | }, | |
63 | }, | |
64 | { | |
65 | 'url': 'https://collegerama.tudelft.nl/Mediasite/Play/86a9ea9f53e149079fbdb4202b521ed21d?catalog=fd32fd35-6c99-466c-89d4-cd3c431bc8a4', | |
66 | 'md5': 'ef1fdded95bdf19b12c5999949419c92', | |
67 | 'info_dict': { | |
68 | 'id': '86a9ea9f53e149079fbdb4202b521ed21d', | |
69 | 'ext': 'wmv', | |
70 | 'title': '64ste Vakantiecursus: Afvalwater', | |
71 | 'description': 'md5:7fd774865cc69d972f542b157c328305', | |
72 | 'thumbnail': r're:^https?://.*\.jpg(?:\?.*?)?$', | |
73 | 'duration': 10853, | |
74 | 'timestamp': 1326446400, | |
75 | 'upload_date': '20120113', | |
76 | }, | |
77 | }, | |
78 | { | |
79 | 'url': 'http://digitalops.sandia.gov/Mediasite/Play/24aace4429fc450fb5b38cdbf424a66e1d', | |
80 | 'md5': '9422edc9b9a60151727e4b6d8bef393d', | |
81 | 'info_dict': { | |
82 | 'id': '24aace4429fc450fb5b38cdbf424a66e1d', | |
83 | 'ext': 'mp4', | |
84 | 'title': 'Xyce Software Training - Section 1', | |
85 | 'description': r're:(?s)SAND Number: SAND 2013-7800.{200,}', | |
86 | 'upload_date': '20120409', | |
87 | 'timestamp': 1333983600, | |
88 | 'duration': 7794, | |
89 | } | |
cfd13c4c DV |
90 | }, |
91 | { | |
92 | 'url': 'https://collegerama.tudelft.nl/Mediasite/Showcase/livebroadcast/Presentation/ada7020854f743c49fbb45c9ec7dbb351d', | |
93 | 'only_matching': True, | |
94 | }, | |
4ee18454 S |
95 | { |
96 | 'url': 'https://mediasite.ntnu.no/Mediasite/Showcase/default/Presentation/7d8b913259334b688986e970fae6fcb31d', | |
97 | 'only_matching': True, | |
98 | }, | |
efee62ac S |
99 | { |
100 | # dashed id | |
101 | 'url': 'https://hitsmediaweb.h-its.org/mediasite/Play/2db6c271-681e-4f19-9af3-c60d1f82869b1d', | |
102 | 'only_matching': True, | |
103 | } | |
8056c854 | 104 | ] |
105 | ||
106 | # look in Mediasite.Core.js (Mediasite.ContentStreamType[*]) | |
107 | _STREAM_TYPES = { | |
2ca7ed41 | 108 | 0: 'video1', # the main video |
8056c854 | 109 | 2: 'slide', |
110 | 3: 'presentation', | |
2ca7ed41 | 111 | 4: 'video2', # screencast? |
8056c854 | 112 | 5: 'video3', |
113 | } | |
114 | ||
2ca7ed41 S |
115 | @staticmethod |
116 | def _extract_urls(webpage): | |
117 | return [ | |
118 | unescapeHTML(mobj.group('url')) | |
119 | for mobj in re.finditer( | |
4014a486 | 120 | r'(?xi)<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:(?:https?:)?//[^/]+)?/Mediasite/Play/%s(?:\?.*?)?)\1' % _ID_RE, |
2ca7ed41 S |
121 | webpage)] |
122 | ||
3fd4c2a5 | 123 | def __extract_slides(self, *, stream_id, snum, Stream, duration, images): |
124 | slide_base_url = Stream['SlideBaseUrl'] | |
125 | ||
126 | fname_template = Stream['SlideImageFileNameTemplate'] | |
127 | if fname_template != 'slide_{0:D4}.jpg': | |
128 | self.report_warning('Unusual slide file name template; report a bug if slide downloading fails') | |
129 | fname_template = re.sub(r'\{0:D([0-9]+)\}', r'{0:0\1}', fname_template) | |
130 | ||
131 | fragments = [] | |
132 | for i, slide in enumerate(Stream['Slides']): | |
133 | if i == 0: | |
134 | if slide['Time'] > 0: | |
135 | default_slide = images.get('DefaultSlide') | |
136 | if default_slide is None: | |
137 | default_slide = images.get('DefaultStreamImage') | |
138 | if default_slide is not None: | |
139 | default_slide = default_slide['ImageFilename'] | |
140 | if default_slide is not None: | |
141 | fragments.append({ | |
142 | 'path': default_slide, | |
143 | 'duration': slide['Time'] / 1000, | |
144 | }) | |
145 | ||
c4f60dd7 | 146 | next_time = try_call( |
147 | lambda: Stream['Slides'][i + 1]['Time'], | |
148 | lambda: duration, | |
149 | lambda: slide['Time'], | |
150 | expected_type=(int, float)) | |
3fd4c2a5 | 151 | |
152 | fragments.append({ | |
153 | 'path': fname_template.format(slide.get('Number', i + 1)), | |
154 | 'duration': (next_time - slide['Time']) / 1000 | |
155 | }) | |
156 | ||
157 | return { | |
158 | 'format_id': '%s-%u.slides' % (stream_id, snum), | |
159 | 'ext': 'mhtml', | |
160 | 'url': slide_base_url, | |
161 | 'protocol': 'mhtml', | |
162 | 'acodec': 'none', | |
163 | 'vcodec': 'none', | |
164 | 'format_note': 'Slides', | |
165 | 'fragments': fragments, | |
166 | 'fragment_base_url': slide_base_url, | |
167 | } | |
168 | ||
8056c854 | 169 | def _real_extract(self, url): |
170 | url, data = unsmuggle_url(url, {}) | |
5ad28e7f | 171 | mobj = self._match_valid_url(url) |
2ca7ed41 S |
172 | resource_id = mobj.group('id') |
173 | query = mobj.group('query') | |
8056c854 | 174 | |
2ca7ed41 | 175 | webpage, urlh = self._download_webpage_handle(url, resource_id) # XXX: add UrlReferrer? |
7947a1f7 | 176 | redirect_url = urlh.geturl() |
8056c854 | 177 | |
178 | # XXX: might have also extracted UrlReferrer and QueryString from the html | |
2ca7ed41 S |
179 | service_path = compat_urlparse.urljoin(redirect_url, self._html_search_regex( |
180 | r'<div[^>]+\bid=["\']ServicePath[^>]+>(.+?)</div>', webpage, resource_id, | |
8056c854 | 181 | default='/Mediasite/PlayerService/PlayerService.svc/json')) |
182 | ||
2ca7ed41 S |
183 | player_options = self._download_json( |
184 | '%s/GetPlayerOptions' % service_path, resource_id, | |
8056c854 | 185 | headers={ |
186 | 'Content-type': 'application/json; charset=utf-8', | |
187 | 'X-Requested-With': 'XMLHttpRequest', | |
188 | }, | |
189 | data=json.dumps({ | |
190 | 'getPlayerOptionsRequest': { | |
2ca7ed41 S |
191 | 'ResourceId': resource_id, |
192 | 'QueryString': query, | |
8056c854 | 193 | 'UrlReferrer': data.get('UrlReferrer', ''), |
194 | 'UseScreenReader': False, | |
195 | } | |
2ca7ed41 S |
196 | }).encode('utf-8'))['d'] |
197 | ||
198 | presentation = player_options['Presentation'] | |
199 | title = presentation['Title'] | |
200 | ||
201 | if presentation is None: | |
202 | raise ExtractorError( | |
203 | 'Mediasite says: %s' % player_options['PlayerPresentationStatusMessage'], | |
8056c854 | 204 | expected=True) |
205 | ||
206 | thumbnails = [] | |
207 | formats = [] | |
2ca7ed41 S |
208 | for snum, Stream in enumerate(presentation['Streams']): |
209 | stream_type = Stream.get('StreamType') | |
210 | if stream_type is None: | |
211 | continue | |
212 | ||
213 | video_urls = Stream.get('VideoUrls') | |
214 | if not isinstance(video_urls, list): | |
215 | video_urls = [] | |
216 | ||
217 | stream_id = self._STREAM_TYPES.get( | |
218 | stream_type, 'type%u' % stream_type) | |
8056c854 | 219 | |
220 | stream_formats = [] | |
2ca7ed41 | 221 | for unum, VideoUrl in enumerate(video_urls): |
3052a30d S |
222 | video_url = url_or_none(VideoUrl.get('Location')) |
223 | if not video_url: | |
2ca7ed41 | 224 | continue |
8056c854 | 225 | # XXX: if Stream.get('CanChangeScheme', False), switch scheme to HTTP/HTTPS |
226 | ||
2ca7ed41 S |
227 | media_type = VideoUrl.get('MediaType') |
228 | if media_type == 'SS': | |
8056c854 | 229 | stream_formats.extend(self._extract_ism_formats( |
2ca7ed41 S |
230 | video_url, resource_id, |
231 | ism_id='%s-%u.%u' % (stream_id, snum, unum), | |
232 | fatal=False)) | |
233 | elif media_type == 'Dash': | |
234 | stream_formats.extend(self._extract_mpd_formats( | |
235 | video_url, resource_id, | |
236 | mpd_id='%s-%u.%u' % (stream_id, snum, unum), | |
237 | fatal=False)) | |
238 | else: | |
239 | stream_formats.append({ | |
240 | 'format_id': '%s-%u.%u' % (stream_id, snum, unum), | |
241 | 'url': video_url, | |
242 | 'ext': mimetype2ext(VideoUrl.get('MimeType')), | |
243 | }) | |
8056c854 | 244 | |
3fd4c2a5 | 245 | if Stream.get('HasSlideContent', False): |
246 | images = player_options['PlayerLayoutOptions']['Images'] | |
247 | stream_formats.append(self.__extract_slides( | |
248 | stream_id=stream_id, | |
249 | snum=snum, | |
250 | Stream=Stream, | |
251 | duration=presentation.get('Duration'), | |
252 | images=images, | |
253 | )) | |
8056c854 | 254 | |
255 | # disprefer 'secondary' streams | |
2ca7ed41 | 256 | if stream_type != 0: |
8056c854 | 257 | for fmt in stream_formats: |
f983b875 | 258 | fmt['quality'] = -10 |
8056c854 | 259 | |
2ca7ed41 S |
260 | thumbnail_url = Stream.get('ThumbnailUrl') |
261 | if thumbnail_url: | |
8056c854 | 262 | thumbnails.append({ |
2ca7ed41 S |
263 | 'id': '%s-%u' % (stream_id, snum), |
264 | 'url': urljoin(redirect_url, thumbnail_url), | |
265 | 'preference': -1 if stream_type != 0 else 0, | |
8056c854 | 266 | }) |
267 | formats.extend(stream_formats) | |
268 | ||
269 | self._sort_formats(formats) | |
270 | ||
271 | # XXX: Presentation['Presenters'] | |
272 | # XXX: Presentation['Transcript'] | |
273 | ||
274 | return { | |
2ca7ed41 S |
275 | 'id': resource_id, |
276 | 'title': title, | |
277 | 'description': presentation.get('Description'), | |
278 | 'duration': float_or_none(presentation.get('Duration'), 1000), | |
279 | 'timestamp': float_or_none(presentation.get('UnixTime'), 1000), | |
8056c854 | 280 | 'formats': formats, |
281 | 'thumbnails': thumbnails, | |
282 | } | |
4014a486 S |
283 | |
284 | ||
285 | class MediasiteCatalogIE(InfoExtractor): | |
286 | _VALID_URL = r'''(?xi) | |
287 | (?P<url>https?://[^/]+/Mediasite) | |
288 | /Catalog/Full/ | |
289 | (?P<catalog_id>{0}) | |
290 | (?: | |
291 | /(?P<current_folder_id>{0}) | |
292 | /(?P<root_dynamic_folder_id>{0}) | |
293 | )? | |
294 | '''.format(_ID_RE) | |
295 | _TESTS = [{ | |
296 | 'url': 'http://events7.mediasite.com/Mediasite/Catalog/Full/631f9e48530d454381549f955d08c75e21', | |
297 | 'info_dict': { | |
298 | 'id': '631f9e48530d454381549f955d08c75e21', | |
299 | 'title': 'WCET Summit: Adaptive Learning in Higher Ed: Improving Outcomes Dynamically', | |
300 | }, | |
301 | 'playlist_count': 6, | |
302 | 'expected_warnings': ['is not a supported codec'], | |
303 | }, { | |
304 | # with CurrentFolderId and RootDynamicFolderId | |
305 | 'url': 'https://medaudio.medicine.iu.edu/Mediasite/Catalog/Full/9518c4a6c5cf4993b21cbd53e828a92521/97a9db45f7ab47428c77cd2ed74bb98f14/9518c4a6c5cf4993b21cbd53e828a92521', | |
306 | 'info_dict': { | |
307 | 'id': '9518c4a6c5cf4993b21cbd53e828a92521', | |
308 | 'title': 'IUSM Family and Friends Sessions', | |
309 | }, | |
310 | 'playlist_count': 2, | |
311 | }, { | |
312 | 'url': 'http://uipsyc.mediasite.com/mediasite/Catalog/Full/d5d79287c75243c58c50fef50174ec1b21', | |
313 | 'only_matching': True, | |
314 | }, { | |
315 | # no AntiForgeryToken | |
316 | 'url': 'https://live.libraries.psu.edu/Mediasite/Catalog/Full/8376d4b24dd1457ea3bfe4cf9163feda21', | |
317 | 'only_matching': True, | |
318 | }, { | |
319 | 'url': 'https://medaudio.medicine.iu.edu/Mediasite/Catalog/Full/9518c4a6c5cf4993b21cbd53e828a92521/97a9db45f7ab47428c77cd2ed74bb98f14/9518c4a6c5cf4993b21cbd53e828a92521', | |
320 | 'only_matching': True, | |
efee62ac S |
321 | }, { |
322 | # dashed id | |
323 | 'url': 'http://events7.mediasite.com/Mediasite/Catalog/Full/631f9e48-530d-4543-8154-9f955d08c75e', | |
324 | 'only_matching': True, | |
4014a486 S |
325 | }] |
326 | ||
327 | def _real_extract(self, url): | |
5ad28e7f | 328 | mobj = self._match_valid_url(url) |
4014a486 S |
329 | mediasite_url = mobj.group('url') |
330 | catalog_id = mobj.group('catalog_id') | |
331 | current_folder_id = mobj.group('current_folder_id') or catalog_id | |
332 | root_dynamic_folder_id = mobj.group('root_dynamic_folder_id') | |
333 | ||
334 | webpage = self._download_webpage(url, catalog_id) | |
335 | ||
336 | # AntiForgeryToken is optional (e.g. [1]) | |
337 | # 1. https://live.libraries.psu.edu/Mediasite/Catalog/Full/8376d4b24dd1457ea3bfe4cf9163feda21 | |
338 | anti_forgery_token = self._search_regex( | |
339 | r'AntiForgeryToken\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', | |
340 | webpage, 'anti forgery token', default=None, group='value') | |
341 | if anti_forgery_token: | |
342 | anti_forgery_header = self._search_regex( | |
343 | r'AntiForgeryHeaderName\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', | |
344 | webpage, 'anti forgery header name', | |
345 | default='X-SOFO-AntiForgeryHeader', group='value') | |
346 | ||
347 | data = { | |
348 | 'IsViewPage': True, | |
349 | 'IsNewFolder': True, | |
350 | 'AuthTicket': None, | |
351 | 'CatalogId': catalog_id, | |
352 | 'CurrentFolderId': current_folder_id, | |
353 | 'RootDynamicFolderId': root_dynamic_folder_id, | |
354 | 'ItemsPerPage': 1000, | |
355 | 'PageIndex': 0, | |
356 | 'PermissionMask': 'Execute', | |
357 | 'CatalogSearchType': 'SearchInFolder', | |
358 | 'SortBy': 'Date', | |
359 | 'SortDirection': 'Descending', | |
360 | 'StartDate': None, | |
361 | 'EndDate': None, | |
362 | 'StatusFilterList': None, | |
363 | 'PreviewKey': None, | |
364 | 'Tags': [], | |
365 | } | |
366 | ||
367 | headers = { | |
368 | 'Content-Type': 'application/json; charset=UTF-8', | |
369 | 'Referer': url, | |
370 | 'X-Requested-With': 'XMLHttpRequest', | |
371 | } | |
372 | if anti_forgery_token: | |
373 | headers[anti_forgery_header] = anti_forgery_token | |
374 | ||
375 | catalog = self._download_json( | |
376 | '%s/Catalog/Data/GetPresentationsForFolder' % mediasite_url, | |
377 | catalog_id, data=json.dumps(data).encode(), headers=headers) | |
378 | ||
379 | entries = [] | |
380 | for video in catalog['PresentationDetailsList']: | |
381 | if not isinstance(video, dict): | |
382 | continue | |
383 | video_id = str_or_none(video.get('Id')) | |
384 | if not video_id: | |
385 | continue | |
386 | entries.append(self.url_result( | |
387 | '%s/Play/%s' % (mediasite_url, video_id), | |
388 | ie=MediasiteIE.ie_key(), video_id=video_id)) | |
389 | ||
390 | title = try_get( | |
391 | catalog, lambda x: x['CurrentFolder']['Name'], compat_str) | |
392 | ||
393 | return self.playlist_result(entries, catalog_id, title,) | |
efee62ac S |
394 | |
395 | ||
396 | class MediasiteNamedCatalogIE(InfoExtractor): | |
397 | _VALID_URL = r'(?xi)(?P<url>https?://[^/]+/Mediasite)/Catalog/catalogs/(?P<catalog_name>[^/?#&]+)' | |
398 | _TESTS = [{ | |
399 | 'url': 'https://msite.misis.ru/Mediasite/Catalog/catalogs/2016-industrial-management-skriabin-o-o', | |
400 | 'only_matching': True, | |
401 | }] | |
402 | ||
403 | def _real_extract(self, url): | |
5ad28e7f | 404 | mobj = self._match_valid_url(url) |
efee62ac S |
405 | mediasite_url = mobj.group('url') |
406 | catalog_name = mobj.group('catalog_name') | |
407 | ||
408 | webpage = self._download_webpage(url, catalog_name) | |
409 | ||
410 | catalog_id = self._search_regex( | |
411 | r'CatalogId\s*:\s*["\'](%s)' % _ID_RE, webpage, 'catalog id') | |
412 | ||
413 | return self.url_result( | |
414 | '%s/Catalog/Full/%s' % (mediasite_url, catalog_id), | |
415 | ie=MediasiteCatalogIE.ie_key(), video_id=catalog_id) |