-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmQuery.pl
317 lines (234 loc) · 6.63 KB
/
mQuery.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use 5.014;
use strict;
use warnings 'all';
#no warnings 'experimental::smartmatch';
# Fast but not in core
# use JSON::XS;
use JSON::PP 'decode_json';
use open qw(:std :encoding(UTF-8));
use utf8;
# ===== MAIN =====
# --- use HTTPS ---
use constant HTTPS
=> 'https://';
# --- Presentation detail link ---
use constant DETAIL_URI
=> '/Mediasite/manage#module=Sf.Manage.PresentationSummary&args[id]=';
# --- Mediasite URIs ---
my %m = (
tree => {
URI => '/Mediasite/Manage/Folder/GetInitialFolderTree',
PAYLOAD => '{"RootFolder":null}',
},
folder => {
URI => '/Mediasite/Manage/Folder/GetFoldersInFolder',
PAYLOAD => '{"RootFolder":"{{ID}}"}',
},
list => {
URI => '/Mediasite/Manage/Grid/GetListOptimized',
# SORT by Name
# set "rows" from 15 to 1000
PAYLOAD => '{"page":"0","rows":"1000","sortOrder":"Ascending","sortBy":"Sort_Name","searchBy":null,"facets":[],"Id":"{{ID}}","id":"{{ID}}"}',
},
detail => {
URI => '/Mediasite/Manage/Presentation/GetDisplay',
PAYLOAD => '{"Id":"{{ID}}","DisplayRecordedNotice":true}',
},
);
# --- OPTIONS ---
my $STREAMS = (shift // '') eq '--streams';
# ----- RUN -----
mQuery($ENV{M_HOST}, $ENV{M_AUTH});
# ================
=head2 mQuery
Query Mediasite EVP for video streams
=cut
sub mQuery {
# --- set up AUTH ---
my $auth = {
HOST => (shift // die "Setup ENV first\n"),
AUTH => (shift // die "Setup ENV first\n"),
};
# --- currently 'streams' option only ---
die "Must specify an option, like --streams\n" unless $STREAMS;
# --- Root folder ---
my $tree = get_tree(API($auth, payload($m{tree}, '')))
# Optionally set custom Folder ID
// { Id => '4c32c0e5155b4298a701d773a1ad4dac14', Name => 'Root' };
# --- Traverse ---
my @tree = $tree;
NODE:
while (@tree) {
my $node = pop @tree;
# push @tree, reverse get_folder(API(
# $auth,
# payload($m{folder}, $node->{Id})
# ));
# iterator
my $items = get_list(API(
$auth,
payload($m{list}, $node->{Id})
));
# Remove 'reverse' for going bottom up
push @tree, reverse @{ $items->{folder} };
VIDEO:
for my $video (@{ $items->{video} }) {
# skip scheduled presentations
next VIDEO if $video->{Status} eq 'Scheduled';
if ($STREAMS) {
my $url = HTTPS . $auth->{HOST} . DETAIL_URI . $video->{Id};
my $path = join ' >> ', @{ $node->{Path}};
my %stream = get_detail(API(
$auth,
payload($m{detail}, $video->{Id})
));
my $streams = join ' ', map { qq([$_:$stream{$_}]) } sort keys %stream;
say qq($url | ($path) $video->{Name} $streams);
}
elsif (1) {
# possibly add more options here
}
}
# FLUSH buffer
$| = 1;
}
}
=head2 get_tree
Get InitialFolderTree
=cut
sub get_tree {
# cursor
my $c = shift;
# root folder
my $tree = $c->{Folders}->[0]->{Children}->[1]
// die "Fatal error\n";
return {
Id => $tree->{Id},
Name => $tree->{Name},
Path => [ $tree->{Name} ],
};
}
=head2 get_folder
Get next folders
=cut
sub get_folder {
# cursor
my $c = shift;
my @f;
for my $f (@{ $c->{Folders} }) {
push @f, { Id => $f->{Id}, Name => $f->{Name} };
}
return @f;
}
=head2 get_list
=cut
sub get_list {
# cursor
my $c = shift;
# iterator
my %item = ( folder => [], video => [] );
for my $row (@{ $c->{Results}->{rows} }) {
# presentation data
my $i = $row->{ObjectData};
# FOLDER
if ($row->{EntityType} eq 'Folder') {
my (undef, @path) = ( split('/', $i->{Path}), $i->{Name} );
push @{ $item{folder} }, {
Id => $i->{Id},
Path => \@path,
};
}
# VIDEO
# skip presentation templates
elsif ($row->{EntityType} eq 'Presentation') {
push @{ $item{video} }, {
Id => $i->{Id},
Name => $i->{Name},
Status => $i->{Status},
# JSON::PP::Boolean
Private => $i->{IsPrivate},
# Size in bytes > 0
Size => $i->{TotalFileLength},
};
}
}
return \%item;
}
=head2 API
Send cURL and return decoded response
in: auth ref, URI ref
=cut
sub API {
my $auth = shift;
my $uri = shift;
my $data = cURL({
URL => HTTPS . $auth->{HOST} . $uri->{URI},
COOKIE => 'MediasitePortal=; MediasiteAuth=' . $auth->{AUTH},
DATA => $uri->{PAYLOAD},
});
# make UTF-8
utf8::encode($data);
my $res = decode_json($data);
die "Bad request, check AUTH\n" unless $res->{ResponseStatus} == 0;
return $res;
}
sub get_detail {
# cursor
my $c = shift;
# Player name
# $c->{Presentation}->{Player}->{Name};
my %stream;
for my $s (@{ $c->{Presentation}->{ContentStreamCollection}->{Streams} }) {
# STREAMS (video1 and/or video2 and/or slides)
# (!) video1 and video2 have null slides (ocr)
# AUDIO
push @{ $stream{AUDIO} }, 3 if ($s->{Audio} // $s->{PlaybackAudioSource});
# PLAYBACK
# video1/video2 input not Completed
push @{ $stream{PLAYBACK} }, $s->{Status};
# WMV
my $wmv = $s->{WMV}->{Status};
push @{ $stream{WMV} }, $wmv if defined $wmv;
# MP4
my $mp4 = $s->{MP4}->{Status};
push @{ $stream{MP4} }, $mp4 if defined $mp4;
# SmoothStreaming
my $ss = $s->{SmoothStreaming}->{Status};
push @{ $stream{SS} }, $ss if defined $ss;
# Live Content
my $live = $s->{Live}->{Status};
push @{ $stream{LIVE} }, $live if defined $live;
# SLIDES
my $slides = $s->{Slides}->{Status};
push @{ $stream{SLIDES} }, $slides if defined $slides;
# OCR
my $ocr = $s->{OCR}->{Status};
push @{ $stream{OCR} }, $ocr if defined $ocr;
}
# remap streams state
for my $s (keys %stream) {
# Check the state is => 3 <= (Completed) on all the existing video inputs
$stream{$s} = (map { ($_ == 3) ? () : 1 } @{ $stream{$s} }) ? 'bad' : 'good';
}
return %stream;
}
=head2 cURL
Send template cURL request
=cut
sub cURL {
my $req = shift;
my $cURL = qq(curl -s '$req->{URL}' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, br' -H 'User-Agent: mQuery/1.0' -H 'Content-Type: application/json; charset=UTF-8' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Cache-Control: no-cache' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: $req->{COOKIE}' -H 'Connection: keep-alive' --data-binary '$req->{DATA}' --compressed);
return do {
no warnings;
join '', qx!$cURL! || die "cURL bad\n";
};
}
=head2 payload
Inject a folder ID into URI template
=cut
sub payload {
my %uri = %{ shift @_ };
my $id = shift;
$uri{PAYLOAD} =~ s|{{ID}}|$id|g;
return { %uri };
}