5 from datetime
import datetime
6 from random
import random
8 from .common
import InfoExtractor
10 compat_urllib_parse_urlparse
,
25 class PanoptoBaseIE(InfoExtractor
):
26 BASE_URL_RE
= r
'(?P<base_url>https?://[\w.]+\.panopto.(?:com|eu)/Panopto)'
28 def _call_api(self
, base_url
, path
, video_id
, data
=None, fatal
=True, **kwargs
):
29 response
= self
._download
_json
(
30 base_url
+ path
, video_id
, data
=json
.dumps(data
).encode('utf8') if data
else None,
31 fatal
=fatal
, headers
={'accept': 'application/json', 'content-type': 'application/json'}
, **kwargs
)
34 error_code
= response
.get('ErrorCode')
36 self
.raise_login_required(method
='cookies')
37 elif error_code
is not None:
38 msg
= f
'Panopto said: {response.get("ErrorMessage")}'
40 raise ExtractorError(msg
, video_id
=video_id
, expected
=True)
42 self
.report_warning(msg
, video_id
=video_id
)
46 def _parse_fragment(url
):
47 return {k: json.loads(v[0]) for k, v in compat_urlparse.parse_qs(compat_urllib_parse_urlparse(url).fragment).items()}
50 def _extract_urls(webpage
):
51 return [m
.group('url') for m
in re
.finditer(
52 r
'<iframe[^>]+src=["\'](?P
<url
>%s/Pages
/(Viewer|Embed|Sessions
/List
)\
.aspx
[^
"\']+)' % PanoptoIE.BASE_URL_RE,
56 class PanoptoIE(PanoptoBaseIE):
57 _VALID_URL = PanoptoBaseIE.BASE_URL_RE + r'/Pages/(Viewer|Embed)\.aspx.*(?:\?|&)id=(?P<id>[a-f0-9-]+)'
60 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=26b3ae9e-4a48-4dcc-96ba-0befba08a0fb',
62 'id': '26b3ae9e-4a48-4dcc-96ba-0befba08a0fb',
63 'title': 'Panopto for Business - Use Cases',
64 'timestamp': 1459184200,
65 'thumbnail': r're:https://demo\.hosted\.panopto\.com/Panopto/Services/FrameGrabber\.svc/FrameRedirect\?objectId=26b3ae9e-4a48-4dcc-96ba-0befba08a0fb&mode=Delivery&random=[\d.]+',
66 'upload_date': '20160328',
69 'duration': 88.17099999999999,
70 'average_rating': int,
71 'uploader_id': '2db6b718-47a0-4b0b-9e17-ab0b00f42b1e',
72 'channel_id': 'e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a',
73 'channel': 'Showcase Videos'
77 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=ed01b077-c9e5-4c7b-b8ff-15fa306d7a59',
79 'id': 'ed01b077-c9e5-4c7b-b8ff-15fa306d7a59',
80 'title': 'Overcoming Top 4 Challenges of Enterprise Video',
81 'uploader': 'Panopto Support',
82 'timestamp': 1449409251,
83 'thumbnail': r're:https://demo\.hosted\.panopto\.com/Panopto/Services/FrameGrabber\.svc/FrameRedirect\?objectId=ed01b077-c9e5-4c7b-b8ff-15fa306d7a59&mode=Delivery&random=[\d.]+',
84 'upload_date': '20151206',
86 'chapters': 'count:21',
87 'cast': ['Panopto Support'],
88 'uploader_id': 'a96d1a31-b4de-489b-9eee-b4a5b414372c',
89 'average_rating': int,
90 'description': 'md5:4391837802b3fc856dadf630c4b375d1',
91 'duration': 1088.2659999999998,
92 'channel_id': '9f3c1921-43bb-4bda-8b3a-b8d2f05a8546',
93 'channel': 'Webcasts',
98 'url': 'https://howtovideos.hosted.panopto.com/Panopto/Pages/Viewer.aspx?randomparam=thisisnotreal&id=5fa74e93-3d87-4694-b60e-aaa4012214ed&advance=true',
100 'id': '5fa74e93-3d87-4694-b60e-aaa4012214ed',
103 'cast': ['Kathryn Kelly'],
104 'uploader_id': '316a0a58-7fa2-4cd9-be1c-64270d284a56',
105 'timestamp': 1569845768,
106 'tags': ['Viewer', 'Enterprise'],
107 'upload_date': '20190930',
108 'thumbnail': r're:https://howtovideos\.hosted\.panopto\.com/Panopto/Services/FrameGrabber.svc/FrameRedirect\?objectId=5fa74e93-3d87-4694-b60e-aaa4012214ed&mode=Delivery&random=[\d.]+',
109 'description': 'md5:2d844aaa1b1a14ad0e2601a0993b431f',
110 'title': 'Getting Started: View a Video',
111 'average_rating': int,
112 'uploader': 'Kathryn Kelly',
113 'channel_id': 'fb93bc3c-6750-4b80-a05b-a921013735d3',
114 'channel': 'Getting Started',
118 # Does not allow normal Viewer.aspx. AUDIO livestream has no url, so should be skipped and only give one stream.
119 'url': 'https://unisa.au.panopto.com/Panopto/Pages/Embed.aspx?id=9d9a0fa3-e99a-4ebd-a281-aac2017f4da4',
121 'id': '9d9a0fa3-e99a-4ebd-a281-aac2017f4da4',
123 'cast': ['LTS CLI Script'],
125 'description': 'md5:ee5cf653919f55b72bce2dbcf829c9fa',
126 'channel_id': 'b23e673f-c287-4cb1-8344-aae9005a69f8',
127 'average_rating': int,
128 'uploader_id': '38377323-6a23-41e2-9ff6-a8e8004bf6f7',
129 'uploader': 'LTS CLI Script',
130 'timestamp': 1572458134,
131 'title': 'WW2 Vets Interview 3 Ronald Stanley George',
132 'thumbnail': r're:https://unisa\.au\.panopto\.com/Panopto/Services/FrameGrabber.svc/FrameRedirect\?objectId=9d9a0fa3-e99a-4ebd-a281-aac2017f4da4&mode=Delivery&random=[\d.]+',
133 'channel': 'World War II Veteran Interviews',
134 'upload_date': '20191030',
138 'url': 'https://ucc.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=0e8484a4-4ceb-4d98-a63f-ac0200b455cb',
139 'only_matching': True
142 'url': 'https://brown.hosted.panopto.com/Panopto/Pages/Embed.aspx?id=0b3ff73b-36a0-46c5-8455-aadf010a3638',
143 'only_matching': True
148 def suitable(cls, url):
149 return False if PanoptoPlaylistIE.suitable(url) else super().suitable(url)
151 def _mark_watched(self, base_url, video_id, delivery_info):
152 duration = traverse_obj(delivery_info, ('Delivery', 'Duration'), expected_type=float)
153 invocation_id = delivery_info.get('InvocationId')
154 stream_id = traverse_obj(delivery_info, ('Delivery', 'Streams', ..., 'PublicID'), get_all=False, expected_type=str)
155 if invocation_id and stream_id and duration:
156 timestamp_str = f'/Date({calendar.timegm(datetime.utcnow().timetuple())}000)/'
160 'ClientTimeStamp': timestamp_str,
162 'InvocationID': invocation_id,
164 'SecondsListened': duration - 1,
165 'SecondsRejected': 0,
169 'StreamID': stream_id,
170 'TimeStamp': timestamp_str,
175 self._download_webpage(
176 base_url + '/Services/Analytics.svc/AddStreamRequests', video_id,
177 fatal=False, data=json.dumps(data).encode('utf8'), headers={'content-type': 'application/json'},
178 note='Marking watched', errnote='Unable to mark watched')
181 def _extract_chapters(delivery):
183 for timestamp in delivery.get('Timestamps', []):
184 start, duration = int_or_none(timestamp.get('Time')), int_or_none(timestamp.get('Duration'))
185 if start is None or duration is None:
189 'end_time': start + duration,
190 'title': timestamp.get('Caption')
194 def _extract_streams_formats_and_subtitles(self, video_id, streams, **fmt_kwargs):
197 for stream in streams or []:
199 http_stream_url = stream.get('StreamHttpUrl')
200 stream_url = stream.get('StreamUrl')
203 stream_formats.append({'url': http_stream_url})
206 media_type = stream.get('ViewerMediaFileTypeName')
207 if media_type in ('hls', ):
208 m3u8_formats, stream_subtitles = self._extract_m3u8_formats_and_subtitles(stream_url, video_id)
209 stream_formats.extend(m3u8_formats)
210 subtitles = self._merge_subtitles(subtitles, stream_subtitles)
212 stream_formats.append({
215 for fmt in stream_formats:
217 'format_note': stream.get('Tag'),
220 formats.extend(stream_formats)
222 return formats, subtitles
224 def _real_extract(self, url):
225 base_url, video_id = self._match_valid_url(url).group('base_url', 'id')
226 delivery_info = self._call_api(
227 base_url, '/Pages/Viewer/DeliveryInfo.aspx', video_id,
229 'deliveryId': video_id,
231 'isLiveNotes': 'false',
232 'refreshAuthCookie': 'true',
233 'isActiveBroadcast': 'false',
234 'isEditing': 'false',
235 'isKollectiveAgentInstalled': 'false',
237 'responseType': 'json',
241 delivery = delivery_info['Delivery']
242 session_start_time = int_or_none(delivery.get('SessionStartTime'))
244 # Podcast stream is usually the combined streams. We will prefer that by default.
245 podcast_formats, podcast_subtitles = self._extract_streams_formats_and_subtitles(
246 video_id, delivery.get('PodcastStreams'), format_note='PODCAST')
248 streams_formats, streams_subtitles = self._extract_streams_formats_and_subtitles(
249 video_id, delivery.get('Streams'), preference=-10)
251 formats = podcast_formats + streams_formats
252 subtitles = self._merge_subtitles(podcast_subtitles, streams_subtitles)
253 self._sort_formats(formats)
255 self.mark_watched(base_url, video_id, delivery_info)
259 'title': delivery.get('SessionName'),
260 'cast': traverse_obj(delivery, ('Contributors', ..., 'DisplayName'), default=[], expected_type=lambda x: x or None),
261 'timestamp': session_start_time - 11640000000 if session_start_time else None,
262 'duration': delivery.get('Duration'),
263 'thumbnail': base_url + f'/Services/FrameGrabber.svc/FrameRedirect?objectId={video_id}&mode=Delivery&random={random()}',
264 'average_rating': delivery.get('AverageRating'),
265 'chapters': self._extract_chapters(delivery) or None,
266 'uploader': delivery.get('OwnerDisplayName') or None,
267 'uploader_id': delivery.get('OwnerId'),
268 'description': delivery.get('SessionAbstract'),
269 'tags': traverse_obj(delivery, ('Tags', ..., 'Content')),
270 'channel_id': delivery.get('SessionGroupPublicID'),
271 'channel': traverse_obj(delivery, 'SessionGroupLongName', 'SessionGroupShortName', get_all=False),
273 'subtitles': subtitles
277 class PanoptoPlaylistIE(PanoptoBaseIE):
278 _VALID_URL = PanoptoBaseIE.BASE_URL_RE + r'/Pages/(Viewer|Embed)\.aspx.*(?:\?|&)pid=(?P<id>[a-f0-9-]+)'
281 'url': 'https://howtovideos.hosted.panopto.com/Panopto/Pages/Viewer.aspx?pid=f3b39fcf-882f-4849-93d6-a9f401236d36&id=5fa74e93-3d87-4694-b60e-aaa4012214ed&advance=true',
283 'title': 'Featured Video Tutorials',
284 'id': 'f3b39fcf-882f-4849-93d6-a9f401236d36',
287 'playlist_mincount': 36
290 'url': 'https://utsa.hosted.panopto.com/Panopto/Pages/Viewer.aspx?pid=e2900555-3ad4-4bdb-854d-ad2401686190',
292 'title': 'Library Website Introduction Playlist',
293 'id': 'e2900555-3ad4-4bdb-854d-ad2401686190',
294 'description': 'md5:f958bca50a1cbda15fdc1e20d32b3ecb',
296 'playlist_mincount': 4
301 def _entries(self, base_url, playlist_id, session_list_id):
302 session_list_info = self._call_api(
303 base_url, f'/Api/SessionLists/{session_list_id}?collections[0].maxCount=500&collections[0].name=items', playlist_id)
305 items = session_list_info['Items']
307 if item.get('TypeName') != 'Session':
308 self.report_warning('Got an item in the playlist that is not a Session' + bug_reports_message(), only_once=True)
312 'id': item.get('Id'),
313 'url': item.get('ViewerUri'),
314 'title': item.get('Name'),
315 'description': item.get('Description'),
316 'duration': item.get('Duration'),
317 'channel': traverse_obj(item, ('Parent', 'Name')),
318 'channel_id': traverse_obj(item, ('Parent', 'Id'))
321 def _real_extract(self, url):
322 base_url, playlist_id = self._match_valid_url(url).group('base_url', 'id')
324 video_id = get_first(parse_qs(url), 'id')
326 if self.get_param('noplaylist'):
327 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
328 return self.url_result(base_url + f'/Pages/Viewer.aspx?id={video_id}', ie_key=PanoptoIE.ie_key(), video_id=video_id)
330 self.to_screen(f'Downloading playlist {playlist_id}; add --no-playlist to just download video {video_id}')
332 playlist_info = self._call_api(base_url, f'/Api/Playlists/{playlist_id}', playlist_id)
333 return self.playlist_result(
334 self._entries(base_url, playlist_id, playlist_info['SessionListId']),
335 playlist_id=playlist_id, playlist_title=playlist_info.get('Name'),
336 playlist_description=playlist_info.get('Description'))
339 class PanoptoListIE(PanoptoBaseIE):
340 _VALID_URL = PanoptoBaseIE.BASE_URL_RE + r'/Pages/Sessions/List\.aspx'
344 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx#folderID=%22e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a%22',
346 'id': 'e4c6a2fc-1214-4ca0-8fb7-aef2e29ff63a',
347 'title': 'Showcase Videos'
349 'playlist_mincount': 140
353 'url': 'https://demo.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx#view=2&maxResults=250',
355 'id': 'panopto_list',
356 'title': 'panopto_list'
358 'playlist_mincount': 300
361 # Folder that contains 8 folders and a playlist
362 'url': 'https://howtovideos.hosted.panopto.com/Panopto/Pages/Sessions/List.aspx?noredirect=true#folderID=%224b9de7ae-0080-4158-8496-a9ba01692c2e%22',
364 'id': '4b9de7ae-0080-4158-8496-a9ba01692c2e',
365 'title': 'Video Tutorials'
367 'playlist_mincount': 9
372 def _fetch_page(self, base_url, query_params, display_id, page):
376 'getFolderData': True,
377 'includePlaylists': True,
380 'maxResults': self._PAGE_SIZE,
383 response = self._call_api(
384 base_url, '/Services/Data.svc/GetSessions', f'{display_id} page {page+1}',
385 data={'queryParameters': params}, fatal=False)
387 for result in get_first(response, 'Results', default=[]):
388 # This could be a video, playlist (or maybe something else)
389 item_id = result.get('DeliveryID')
393 'title': result.get('SessionName'),
394 'url': traverse_obj(result, 'ViewerUrl', 'EmbedUrl', get_all=False) or (base_url + f'/Pages/Viewer.aspx?id={item_id}'),
395 'duration': result.get('Duration'),
396 'channel': result.get('FolderName'),
397 'channel_id': result.get('FolderID'),
400 for folder in get_first(response, 'Subfolders', default=[]):
401 folder_id = folder.get('ID')
402 yield self.url_result(
403 base_url + f'/Pages/Sessions/List.aspx#folderID="{folder_id}
"',
404 ie_key=PanoptoListIE.ie_key(), video_id=folder_id, title=folder.get('Name'))
406 def _extract_folder_metadata(self, base_url, folder_id):
407 response = self._call_api(
408 base_url, '/Services/Data.svc/GetFolderInfo', folder_id,
409 data={'folderID': folder_id}, fatal=False)
411 'title': get_first(response, 'Name', default=[])
414 def _real_extract(self, url):
415 mobj = self._match_valid_url(url)
416 base_url = mobj.group('base_url')
418 query_params = self._parse_fragment(url)
419 folder_id, display_id = query_params.get('folderID'), 'panopto_list'
421 if query_params.get('isSubscriptionsPage'):
422 display_id = 'subscriptions'
423 if not query_params.get('subscribableTypes'):
424 query_params['subscribableTypes'] = [0, 1, 2]
425 elif query_params.get('isSharedWithMe'):
426 display_id = 'sharedwithme'
428 display_id = folder_id
430 query = query_params.get('query')
432 display_id += f': query "{query}
"'
440 info.update(self._extract_folder_metadata(base_url, folder_id))
442 info['entries'] = OnDemandPagedList(
443 functools.partial(self._fetch_page, base_url, query_params, display_id), self._PAGE_SIZE)