5 from datetime
import datetime
6 from random
import random
8 from .common
import InfoExtractor
10 compat_urllib_parse_urlparse
,
21 srt_subtitles_timecode
,
26 class PanoptoBaseIE(InfoExtractor
):
27 BASE_URL_RE
= r
'(?P<base_url>https?://[\w.-]+\.panopto.(?:com|eu)/Panopto)'
55 def _call_api(self
, base_url
, path
, video_id
, data
=None, fatal
=True, **kwargs
):
56 response
= self
._download
_json
(
57 base_url
+ path
, video_id
, data
=json
.dumps(data
).encode('utf8') if data
else None,
58 fatal
=fatal
, headers
={'accept': 'application/json', 'content-type': 'application/json'}, **kwargs
)
61 error_code
= traverse_obj(response
, 'ErrorCode')
63 self
.raise_login_required(method
='cookies')
64 elif error_code
is not None:
65 msg
= f
'Panopto said: {response.get("ErrorMessage")}'
67 raise ExtractorError(msg
, video_id
=video_id
, expected
=True)
69 self
.report_warning(msg
, video_id
=video_id
)
73 def _parse_fragment(url
):
74 return {k
: json
.loads(v
[0]) for k
, v
in compat_urlparse
.parse_qs(compat_urllib_parse_urlparse(url
).fragment
).items()}
77 def _extract_urls(webpage
):
78 return [m
.group('url') for m
in re
.finditer(
79 r
'<iframe[^>]+src=["\'](?P
<url
>%s/Pages
/(Viewer|Embed|Sessions
/List
)\
.aspx
[^
"\']+)' % PanoptoIE.BASE_URL_RE,
83 class PanoptoIE(PanoptoBaseIE):
84 _VALID_URL = PanoptoBaseIE.BASE_URL_RE + r'/Pages/(Viewer|Embed)\.aspx.*(?:\?|&)id=(?P<id>[a-f0-9-]+)'
87 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=26b3ae9e-4a48-4dcc-96ba-0befba08a0fb',
89 'id': '26b3ae9e-4a48-4dcc-96ba-0befba08a0fb',
90 'title': 'Panopto for Business - Use Cases',
91 'timestamp': 1459184200,
92 'thumbnail': r're:https://demo\.hosted\.panopto\.com/.+',
93 'upload_date': '20160328',
97 'duration': 88.17099999999999,
98 'average_rating': int,
99 'uploader_id': '2db6b718-47a0-4b0b-9e17-ab0b00f42b1e',
100 'channel_id': 'e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a',
101 'channel': 'Showcase Videos'
105 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=ed01b077-c9e5-4c7b-b8ff-15fa306d7a59',
107 'id': 'ed01b077-c9e5-4c7b-b8ff-15fa306d7a59',
108 'title': 'Overcoming Top 4 Challenges of Enterprise Video',
109 'uploader': 'Panopto Support',
110 'timestamp': 1449409251,
111 'thumbnail': r're:https://demo\.hosted\.panopto\.com/.+',
112 'upload_date': '20151206',
114 'chapters': 'count:12',
115 'cast': ['Panopto Support'],
116 'uploader_id': 'a96d1a31-b4de-489b-9eee-b4a5b414372c',
117 'average_rating': int,
118 'description': 'md5:4391837802b3fc856dadf630c4b375d1',
119 'duration': 1088.2659999999998,
120 'channel_id': '9f3c1921-43bb-4bda-8b3a-b8d2f05a8546',
121 'channel': 'Webcasts',
125 # Extra params in URL
126 'url': 'https://howtovideos.hosted.panopto.com/Panopto/Pages/Viewer.aspx?randomparam=thisisnotreal&id=5fa74e93-3d87-4694-b60e-aaa4012214ed&advance=true',
128 'id': '5fa74e93-3d87-4694-b60e-aaa4012214ed',
131 'cast': ['Kathryn Kelly'],
132 'uploader_id': '316a0a58-7fa2-4cd9-be1c-64270d284a56',
133 'timestamp': 1569845768,
134 'tags': ['Viewer', 'Enterprise'],
136 'upload_date': '20190930',
137 'thumbnail': r're:https://howtovideos\.hosted\.panopto\.com/.+',
138 'description': 'md5:2d844aaa1b1a14ad0e2601a0993b431f',
139 'title': 'Getting Started: View a Video',
140 'average_rating': int,
141 'uploader': 'Kathryn Kelly',
142 'channel_id': 'fb93bc3c-6750-4b80-a05b-a921013735d3',
143 'channel': 'Getting Started',
147 # Does not allow normal Viewer.aspx. AUDIO livestream has no url, so should be skipped and only give one stream.
148 'url': 'https://unisa.au.panopto.com/Panopto/Pages/Embed.aspx?id=9d9a0fa3-e99a-4ebd-a281-aac2017f4da4',
150 'id': '9d9a0fa3-e99a-4ebd-a281-aac2017f4da4',
152 'cast': ['LTS CLI Script'],
155 'description': 'md5:ee5cf653919f55b72bce2dbcf829c9fa',
156 'channel_id': 'b23e673f-c287-4cb1-8344-aae9005a69f8',
157 'average_rating': int,
158 'uploader_id': '38377323-6a23-41e2-9ff6-a8e8004bf6f7',
159 'uploader': 'LTS CLI Script',
160 'timestamp': 1572458134,
161 'title': 'WW2 Vets Interview 3 Ronald Stanley George',
162 'thumbnail': r're:https://unisa\.au\.panopto\.com/.+',
163 'channel': 'World War II Veteran Interviews',
164 'upload_date': '20191030',
169 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=a7f12f1d-3872-4310-84b0-f8d8ab15326b',
171 'id': 'a7f12f1d-3872-4310-84b0-f8d8ab15326b',
173 'timestamp': 1448798857,
174 'duration': 4712.681,
175 'title': 'Cache Memory - CompSci 15-213, Lecture 12',
176 'channel_id': 'e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a',
177 'uploader_id': 'a96d1a31-b4de-489b-9eee-b4a5b414372c',
178 'upload_date': '20151129',
180 'uploader': 'Panopto Support',
181 'channel': 'Showcase Videos',
182 'description': 'md5:55e51d54233ddb0e6c2ed388ca73822c',
183 'cast': ['ISR Videographer', 'Panopto Support'],
184 'chapters': 'count:28',
185 'thumbnail': r're:https://demo\.hosted\.panopto\.com/.+',
187 'params': {'format': 'mhtml', 'skip_download': True}
190 'url': 'https://na-training-1.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=8285224a-9a2b-4957-84f2-acb0000c4ea9',
192 'id': '8285224a-9a2b-4957-84f2-acb0000c4ea9',
195 'title': 'Company Policy',
197 'timestamp': 1615058901,
198 'channel': 'Human Resources',
199 'tags': ['HumanResources'],
200 'duration': 1604.243,
201 'thumbnail': r're:https://na-training-1\.hosted\.panopto\.com/.+',
202 'uploader_id': '8e8ba0a3-424f-40df-a4f1-ab3a01375103',
203 'uploader': 'Cait M.',
204 'upload_date': '20210306',
206 'subtitles': {'en-US': [{'ext': 'srt', 'data': 'md5:a3f4d25963fdeace838f327097c13265'}],
207 'es-ES': [{'ext': 'srt', 'data': 'md5:57e9dad365fd0fbaf0468eac4949f189'}]},
209 'params': {'writesubtitles': True, 'skip_download': True}
211 # On Panopto there are two subs: "Default
" and en-US. en-US is blank and should be skipped.
212 'url': 'https://na-training-1.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=940cbd41-f616-4a45-b13e-aaf1000c915b',
214 'id': '940cbd41-f616-4a45-b13e-aaf1000c915b',
216 'subtitles': 'count:1',
217 'title': 'HR Benefits Review Meeting*',
218 'cast': ['Panopto Support'],
220 'timestamp': 1575024251,
221 'thumbnail': r're:https://na-training-1\.hosted\.panopto\.com/.+',
223 'description': 'md5:04f90a9c2c68b7828144abfb170f0106',
224 'uploader': 'Panopto Support',
226 'duration': 409.34499999999997,
227 'uploader_id': 'b6ac04ad-38b8-4724-a004-a851004ea3df',
228 'upload_date': '20191129',
231 'params': {'writesubtitles': True, 'skip_download': True}
234 'url': 'https://ucc.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=0e8484a4-4ceb-4d98-a63f-ac0200b455cb',
235 'only_matching': True
238 'url': 'https://brown.hosted.panopto.com/Panopto/Pages/Embed.aspx?id=0b3ff73b-36a0-46c5-8455-aadf010a3638',
239 'only_matching': True
244 def suitable(cls, url):
245 return False if PanoptoPlaylistIE.suitable(url) else super().suitable(url)
247 def _mark_watched(self, base_url, video_id, delivery_info):
248 duration = traverse_obj(delivery_info, ('Delivery', 'Duration'), expected_type=float)
249 invocation_id = delivery_info.get('InvocationId')
250 stream_id = traverse_obj(delivery_info, ('Delivery', 'Streams', ..., 'PublicID'), get_all=False, expected_type=str)
251 if invocation_id and stream_id and duration:
252 timestamp_str = f'/Date({calendar.timegm(datetime.utcnow().timetuple())}000)/'
256 'ClientTimeStamp': timestamp_str,
258 'InvocationID': invocation_id,
260 'SecondsListened': duration - 1,
261 'SecondsRejected': 0,
265 'StreamID': stream_id,
266 'TimeStamp': timestamp_str,
271 self._download_webpage(
272 base_url + '/Services/Analytics.svc/AddStreamRequests', video_id,
273 fatal=False, data=json.dumps(data).encode('utf8'), headers={'content-type': 'application/json'},
274 note='Marking watched', errnote='Unable to mark watched')
277 def _extract_chapters(timestamps):
279 for timestamp in timestamps or []:
280 caption = timestamp.get('Caption')
281 start, duration = int_or_none(timestamp.get('Time')), int_or_none(timestamp.get('Duration'))
282 if not caption or start is None or duration is None:
286 'end_time': start + duration,
292 def _extract_mhtml_formats(base_url, timestamps):
294 for timestamp in timestamps or []:
295 duration = timestamp.get('Duration')
296 obj_id, obj_sn = timestamp.get('ObjectIdentifier'), timestamp.get('ObjectSequenceNumber'),
297 if timestamp.get('EventTargetType') == 'PowerPoint' and obj_id is not None and obj_sn is not None:
298 image_frags.setdefault('slides', []).append({
299 'url': base_url + f'/Pages/Viewer/Image.aspx?id={obj_id}&number={obj_sn}',
303 obj_pid, session_id, abs_time = timestamp.get('ObjectPublicIdentifier'), timestamp.get('SessionID'), timestamp.get('AbsoluteTime')
304 if None not in (obj_pid, session_id, abs_time):
305 image_frags.setdefault('chapter', []).append({
306 'url': base_url + f'/Pages/Viewer/Thumb.aspx?eventTargetPID={obj_pid}&sessionPID={session_id}&number={obj_sn}&isPrimary=false&absoluteTime={abs_time}',
307 'duration': duration,
309 for name, fragments in image_frags.items():
316 'url': 'about:invalid',
317 'fragments': fragments
321 def _json2srt(data, delivery):
323 for i, line in enumerate(data):
324 start_time = line['Time']
325 duration = line.get('Duration')
327 end_time = start_time + duration
329 end_time = traverse_obj(data, (i + 1, 'Time')) or delivery['Duration']
330 yield f'{i + 1}\n{srt_subtitles_timecode(start_time)} --> {srt_subtitles_timecode(end_time)}\n{line["Caption
"]}'
331 return '\n\n'.join(_gen_lines())
333 def _get_subtitles(self, base_url, video_id, delivery):
335 for lang in delivery.get('AvailableLanguages') or []:
336 response = self._call_api(
337 base_url, '/Pages/Viewer/DeliveryInfo.aspx', video_id, fatal=False,
338 note='Downloading captions JSON metadata', query={
339 'deliveryId': video_id,
341 'language': str(lang),
342 'responseType': 'json'
345 if not isinstance(response, list):
347 subtitles.setdefault(self._SUB_LANG_MAPPING.get(lang) or 'default', []).append({
349 'data': self._json2srt(response, delivery),
353 def _extract_streams_formats_and_subtitles(self, video_id, streams, **fmt_kwargs):
356 for stream in streams or []:
358 http_stream_url = stream.get('StreamHttpUrl')
359 stream_url = stream.get('StreamUrl')
362 stream_formats.append({'url': http_stream_url})
365 media_type = stream.get('ViewerMediaFileTypeName')
366 if media_type in ('hls', ):
367 m3u8_formats, stream_subtitles = self._extract_m3u8_formats_and_subtitles(stream_url, video_id)
368 stream_formats.extend(m3u8_formats)
369 subtitles = self._merge_subtitles(subtitles, stream_subtitles)
371 stream_formats.append({
374 for fmt in stream_formats:
376 'format_note': stream.get('Tag'),
379 formats.extend(stream_formats)
381 return formats, subtitles
383 def _real_extract(self, url):
384 base_url, video_id = self._match_valid_url(url).group('base_url', 'id')
385 delivery_info = self._call_api(
386 base_url, '/Pages/Viewer/DeliveryInfo.aspx', video_id,
388 'deliveryId': video_id,
390 'isLiveNotes': 'false',
391 'refreshAuthCookie': 'true',
392 'isActiveBroadcast': 'false',
393 'isEditing': 'false',
394 'isKollectiveAgentInstalled': 'false',
396 'responseType': 'json',
400 delivery = delivery_info['Delivery']
401 session_start_time = int_or_none(delivery.get('SessionStartTime'))
402 timestamps = delivery.get('Timestamps')
404 # Podcast stream is usually the combined streams. We will prefer that by default.
405 podcast_formats, podcast_subtitles = self._extract_streams_formats_and_subtitles(
406 video_id, delivery.get('PodcastStreams'), format_note='PODCAST')
408 streams_formats, streams_subtitles = self._extract_streams_formats_and_subtitles(
409 video_id, delivery.get('Streams'), preference=-10)
411 formats = podcast_formats + streams_formats
412 formats.extend(self._extract_mhtml_formats(base_url, timestamps))
413 subtitles = self._merge_subtitles(
414 podcast_subtitles, streams_subtitles, self.extract_subtitles(base_url, video_id, delivery))
416 self._sort_formats(formats)
417 self.mark_watched(base_url, video_id, delivery_info)
421 'title': delivery.get('SessionName'),
422 'cast': traverse_obj(delivery, ('Contributors', ..., 'DisplayName'), default=[], expected_type=lambda x: x or None),
423 'timestamp': session_start_time - 11640000000 if session_start_time else None,
424 'duration': delivery.get('Duration'),
425 'thumbnail': base_url + f'/Services/FrameGrabber.svc/FrameRedirect?objectId={video_id}&mode=Delivery&random={random()}',
426 'average_rating': delivery.get('AverageRating'),
427 'chapters': self._extract_chapters(timestamps),
428 'uploader': delivery.get('OwnerDisplayName') or None,
429 'uploader_id': delivery.get('OwnerId'),
430 'description': delivery.get('SessionAbstract'),
431 'tags': traverse_obj(delivery, ('Tags', ..., 'Content')),
432 'channel_id': delivery.get('SessionGroupPublicID'),
433 'channel': traverse_obj(delivery, 'SessionGroupLongName', 'SessionGroupShortName', get_all=False),
435 'subtitles': subtitles
439 class PanoptoPlaylistIE(PanoptoBaseIE):
440 _VALID_URL = PanoptoBaseIE.BASE_URL_RE + r'/Pages/(Viewer|Embed)\.aspx.*(?:\?|&)pid=(?P<id>[a-f0-9-]+)'
443 'url': 'https://howtovideos.hosted.panopto.com/Panopto/Pages/Viewer.aspx?pid=f3b39fcf-882f-4849-93d6-a9f401236d36&id=5fa74e93-3d87-4694-b60e-aaa4012214ed&advance=true',
445 'title': 'Featured Video Tutorials',
446 'id': 'f3b39fcf-882f-4849-93d6-a9f401236d36',
449 'playlist_mincount': 36
452 'url': 'https://utsa.hosted.panopto.com/Panopto/Pages/Viewer.aspx?pid=e2900555-3ad4-4bdb-854d-ad2401686190',
454 'title': 'Library Website Introduction Playlist',
455 'id': 'e2900555-3ad4-4bdb-854d-ad2401686190',
456 'description': 'md5:f958bca50a1cbda15fdc1e20d32b3ecb',
458 'playlist_mincount': 4
463 def _entries(self, base_url, playlist_id, session_list_id):
464 session_list_info = self._call_api(
465 base_url, f'/Api/SessionLists/{session_list_id}?collections[0].maxCount=500&collections[0].name=items', playlist_id)
467 items = session_list_info['Items']
469 if item.get('TypeName') != 'Session':
470 self.report_warning('Got an item in the playlist that is not a Session' + bug_reports_message(), only_once=True)
474 'id': item.get('Id'),
475 'url': item.get('ViewerUri'),
476 'title': item.get('Name'),
477 'description': item.get('Description'),
478 'duration': item.get('Duration'),
479 'channel': traverse_obj(item, ('Parent', 'Name')),
480 'channel_id': traverse_obj(item, ('Parent', 'Id'))
483 def _real_extract(self, url):
484 base_url, playlist_id = self._match_valid_url(url).group('base_url', 'id')
486 video_id = get_first(parse_qs(url), 'id')
488 if self.get_param('noplaylist'):
489 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
490 return self.url_result(base_url + f'/Pages/Viewer.aspx?id={video_id}', ie_key=PanoptoIE.ie_key(), video_id=video_id)
492 self.to_screen(f'Downloading playlist {playlist_id}; add --no-playlist to just download video {video_id}')
494 playlist_info = self._call_api(base_url, f'/Api/Playlists/{playlist_id}', playlist_id)
495 return self.playlist_result(
496 self._entries(base_url, playlist_id, playlist_info['SessionListId']),
497 playlist_id=playlist_id, playlist_title=playlist_info.get('Name'),
498 playlist_description=playlist_info.get('Description'))
501 class PanoptoListIE(PanoptoBaseIE):
502 _VALID_URL = PanoptoBaseIE.BASE_URL_RE + r'/Pages/Sessions/List\.aspx'
506 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx#folderID=%22e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a%22',
508 'id': 'e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a',
509 'title': 'Showcase Videos'
511 'playlist_mincount': 140
515 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx#view=2&maxResults=250',
517 'id': 'panopto_list',
518 'title': 'panopto_list'
520 'playlist_mincount': 300
523 # Folder that contains 8 folders and a playlist
524 'url': 'https://howtovideos.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx?noredirect=true#folderID=%224b9de7ae-0080-4158-8496-a9ba01692c2e%22',
526 'id': '4b9de7ae-0080-4158-8496-a9ba01692c2e',
527 'title': 'Video Tutorials'
529 'playlist_mincount': 9
534 def _fetch_page(self, base_url, query_params, display_id, page):
538 'getFolderData': True,
539 'includePlaylists': True,
542 'maxResults': self._PAGE_SIZE,
545 response = self._call_api(
546 base_url, '/Services/Data.svc/GetSessions', f'{display_id} page {page+1}',
547 data={'queryParameters': params}, fatal=False)
549 for result in get_first(response, 'Results', default=[]):
550 # This could be a video, playlist (or maybe something else)
551 item_id = result.get('DeliveryID')
555 'title': result.get('SessionName'),
556 'url': traverse_obj(result, 'ViewerUrl', 'EmbedUrl', get_all=False) or (base_url + f'/Pages/Viewer.aspx?id={item_id}'),
557 'duration': result.get('Duration'),
558 'channel': result.get('FolderName'),
559 'channel_id': result.get('FolderID'),
562 for folder in get_first(response, 'Subfolders', default=[]):
563 folder_id = folder.get('ID')
564 yield self.url_result(
565 base_url + f'/Pages/Sessions/List.aspx#folderID="{folder_id}
"',
566 ie_key=PanoptoListIE.ie_key(), video_id=folder_id, title=folder.get('Name'))
568 def _extract_folder_metadata(self, base_url, folder_id):
569 response = self._call_api(
570 base_url, '/Services/Data.svc/GetFolderInfo', folder_id,
571 data={'folderID': folder_id}, fatal=False)
573 'title': get_first(response, 'Name', default=[])
576 def _real_extract(self, url):
577 mobj = self._match_valid_url(url)
578 base_url = mobj.group('base_url')
580 query_params = self._parse_fragment(url)
581 folder_id, display_id = query_params.get('folderID'), 'panopto_list'
583 if query_params.get('isSubscriptionsPage'):
584 display_id = 'subscriptions'
585 if not query_params.get('subscribableTypes'):
586 query_params['subscribableTypes'] = [0, 1, 2]
587 elif query_params.get('isSharedWithMe'):
588 display_id = 'sharedwithme'
590 display_id = folder_id
592 query = query_params.get('query')
594 display_id += f': query "{query}
"'
602 info.update(self._extract_folder_metadata(base_url, folder_id))
604 info['entries'] = OnDemandPagedList(
605 functools.partial(self._fetch_page, base_url, query_params, display_id), self._PAGE_SIZE)