-
Notifications
You must be signed in to change notification settings - Fork 0
/
IcecastMetadataStats.js
500 lines (450 loc) · 17.2 KB
/
IcecastMetadataStats.js
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/**
* @license
* @see https://github.com/eshaz/icecast-metadata-js
* @copyright 2021 Ethan Halsall
* This file is part of icecast-metadata-stats.
*
* icecast-metadata-stats free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* icecast-metadata-stats distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*/
let IcecastReadableStream;
import('icecast-metadata-js/browser.js').then(mod => {
IcecastReadableStream = mod.IcecastReadableStream;
});
const convert = require('xml2json');
const noOp = () => {};
const STOPPED = 'stopped';
const RUNNING = 'running';
const FETCHING = 'fetching';
const p = new WeakMap();
// variables
const icyController = Symbol();
const icyFetchStatus = Symbol();
const oggController = Symbol();
const oggFetchStatus = Symbol();
const icestatsEndpoint = Symbol();
const icestatsController = Symbol();
const icestatsFetchStatus = Symbol();
const streamonkeyEndpoint = Symbol();
const streamonkeyController = Symbol();
const streamonkeyFetchStatus = Symbol();
const historyEndpoint = Symbol();
const historyController = Symbol();
const historyFetchStatus = Symbol();
const statsEndpoint = Symbol();
const statsController = Symbol();
const statsFetchStatus = Symbol();
const nextsongsEndpoint = Symbol();
const nextsongsController = Symbol();
const nextsongsFetchStatus = Symbol();
const sevenhtmlEndpoint = Symbol();
const sevenhtmlController = Symbol();
const sevenhtmlFetchStatus = Symbol();
const streamEndpoint = Symbol();
const icyMetaInt = Symbol();
const icyCharacterEncoding = Symbol();
const icyDetectionTimeout = Symbol();
const streamonkeySessionId = Symbol();
const historyEtag = Symbol();
const sources = Symbol();
const interval = Symbol();
const onStats = Symbol();
const onStatsFetch = Symbol();
const state = Symbol();
const intervalId = Symbol();
// methods
const fetchStats = Symbol();
const getStreamMetadata = Symbol();
module.exports = {
IcecastMetadataStats: class IcecastMetadataStats {
/**
* @constructor
* @param {URL} endpoint Stream endpoint
* @param {object} [options] Options object
*
* @callback [options.onStats] Called when the automatic query completes
* @callback [options.onStatsFetch] Called when the automatic query begins
* @param {Array} [options.sources] List of sources to automatically query ["icy", "ogg", "icestats", "stats", "sevenhtml", "nextsongs"]
* @param {number} [options.interval] Time in seconds to wait between automatically queries
* @param {URL} [options.icestatsEndpoint] Endpoint for the `status-json.xsl` source
* @param {URL} [options.statsEndpoint] Endpoint for the `stats` source
* @param {URL} [options.nextsongsEndpoint] Endpoint for the `nextsongs` source
* @param {URL} [options.sevenhtmlEndpoint] Endpoint for the `7.html` source
* @param {number} [options.icyMetaInt] Manually sets the ICY metadata interval
* @param {string} [options.icyCharacterEncoding] Character encoding to use for ICY metadata (defaults to "utf-8")
* @param {number} [options.icyDetectionTimeout] Time in milliseconds to search for ICY metadata
* @param {string} [options.streamonkeySessionId] StreaMonkey Streaming Session ID
*/
constructor(endpoint, options = {}) {
const serverPath = endpoint.split('/').slice(0, -1).join('/');
let ep = endpoint.split('/').slice(0, 4).join('/');
let q = ep.indexOf('?');
if (q > 0) ep = ep.substring(0, q);
// prettier-ignore
p.set(this, {
[streamEndpoint]: endpoint,
[icestatsEndpoint]: options.icestatsEndpoint || `${serverPath}/status-json.xsl`,
[statsEndpoint] : options.statsEndpoint || `${serverPath}/stats`,
[nextsongsEndpoint] : options.nextsongsEndpoint || `${serverPath}/nextsongs`,
[sevenhtmlEndpoint] : options.sevenhtmlEndpoint || `${serverPath}/7.html`,
[streamonkeyEndpoint] : options.streamonkeyEndpoint || `${ep}/mystats`,
[historyEndpoint]: options.historyEndpoint || `${ep}/history`,
[sources]: options.sources || [],
[interval]: (options.interval || 30) * 1000,
[onStats]: options.onStats || noOp,
[onStatsFetch]: options.onStatsFetch || noOp,
[icyMetaInt]: options.icyMetaInt,
[icyCharacterEncoding]: options.icyCharacterEncoding,
[icyDetectionTimeout]: options.icyDetectionTimeout,
[streamonkeySessionId]: options.streamonkeySessionId || null,
[historyEtag]: options.historyEtag || null,
[icyController]: new AbortController(),
[oggController]: new AbortController(),
[icestatsController]: new AbortController(),
[statsController]: new AbortController(),
[streamonkeyController]: new AbortController(),
[historyController]: new AbortController(),
[nextsongsController]: new AbortController(),
[sevenhtmlController]: new AbortController(),
[state]: STOPPED,
});
}
static xml2Json(xml) {
let res = convert.toJson(xml, { coerce: true });
console.log(xml, res);
return JSON.parse(res);
}
/**
* @returns The current state ["stopped", "running", "fetching"]
*/
get state() {
return p.get(this)[state];
}
/**
* @returns The generated `status-json.xsl` endpoint
*/
get icestatsEndpoint() {
return p.get(this)[icestatsEndpoint];
}
/**
* @returns The generated `stats` endpoint
*/
get statsEndpoint() {
return p.get(this)[statsEndpoint];
}
/**
* @returns The generated `streamonkey` endpoint
*/
get streamonkeyEndpoint() {
return p.get(this)[streamonkeyEndpoint];
}
/**
* @returns The generated `history` endpoint
*/
get historyEndpoint() {
return p.get(this)[historyEndpoint];
}
/**
* @returns The generated `history` etag
*/
get historyEtag() {
return p.get(this)[historyEtag];
}
/**
* @returns The generated `nextsongs` endpoint
*/
get nextsongsEndpoint() {
return p.get(this)[nextsongsEndpoint];
}
/**
* @returns The generated `7.html` endpoint
*/
get sevenhtmlEndpoint() {
return p.get(this)[sevenhtmlEndpoint];
}
/**
* @description Starts automatically fetching stats
*/
start() {
if (p.get(this)[state] === STOPPED) {
p.get(this)[state] = RUNNING;
this.fetch().then(p.get(this)[onStats]);
p.get(this)[intervalId] = setInterval(() => {
this.fetch().then(p.get(this)[onStats]);
}, p.get(this)[interval]);
}
}
/**
* @description Stops automatically fetching stats and cancels any inprogress stats
*/
stop() {
if (p.get(this)[state] !== STOPPED) {
p.get(this)[state] = STOPPED;
clearInterval(p.get(this)[intervalId]);
p.get(this)[icyController].abort();
p.get(this)[oggController].abort();
p.get(this)[icestatsController].abort();
p.get(this)[statsController].abort();
p.get(this)[sevenhtmlController].abort();
p.get(this)[streamonkeyController].abort();
p.get(this)[historyController].abort();
}
}
/**
* @description Manually fetches stats from the sources passed in to the `options.sources` parameter
* @async
* @returns {object} Object containing the stats from the sources
*/
async fetch() {
if (p.get(this)[state] !== FETCHING) {
const oldState = p.get(this)[state];
p.get(this)[state] = FETCHING;
p.get(this)[onStatsFetch](p.get(this)[sources]);
const promises = [];
if (p.get(this)[sources].includes('icestats')) promises.push(this.getIcestats());
if (p.get(this)[sources].includes('sevenhtml')) promises.push(this.getSevenhtml());
if (p.get(this)[sources].includes('stats')) promises.push(this.getStats());
if (p.get(this)[sources].includes('nextsongs')) promises.push(this.getNextsongs());
if (p.get(this)[sources].includes('icy')) promises.push(this.getIcyMetadata());
if (p.get(this)[sources].includes('ogg')) promises.push(this.getOggMetadata());
if (p.get(this)[sources].includes('streamonkey')) promises.push(this.getStreamonkey());
if (p.get(this)[sources].includes('history')) promises.push(this.getHistory());
const stats = await Promise.all(promises).then(stats => stats.reduce((acc, stat) => ({ ...acc, ...stat }), {}));
p.get(this)[state] = p.get(this)[state] !== FETCHING ? p.get(this)[state] : oldState;
return stats;
}
}
/**
* @description Fetches the data from the `/status-json.xsl` endpoint
* @async
* @returns {object} Object containing results of `/status-json.xsl`
*/
async getIcestats() {
return this[fetchStats]({
status: icestatsFetchStatus,
endpoint: icestatsEndpoint,
controller: icestatsController,
mapper: res => (/^(?:text|application)\/json/.test(res.headers.get('content-type')) ? res.json() : undefined)
}).then(stats => ({ icestats: stats && stats.icestats }));
}
/*
<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>350,1,132,1000,41,128,Dj Mixes Sety</body></html>
,141,1000,50,128,Gra AutoPilot audycje Energy 2000</body></html>
,27,1000,8,128,Gra Wavelogic audycje Rave With The Wave</body></html>
,578,1000,233,128,youtube.com/RadioPartyOfficial</body></html>
,15,1000,5,64,youtube.com/RadioPartyOfficial</body></html>
*/
// http://wiki.winamp.com/wiki/SHOUTcast_DNAS_Server_2_XML_Reponses#Equivalent_of_7.html
// CURRENTLISTENERS STREAMSTATUS PEAKLISTENERS MAXLISTENERS UNIQUELISTENERS BITRATE SONGTITLE
/**
* @description Fetches the data from the `/7.html` endpoint
* @async
* @returns {object} Object containing results of `/7.html`
*/
async getSevenhtml() {
// TODO: request correct sid (works only if input url ist /stream/<sid>)
return this[fetchStats]({
status: sevenhtmlFetchStatus,
endpoint: sevenhtmlEndpoint,
controller: sevenhtmlController,
mapper: async res =>
/^text\/html/.test(res.headers.get('content-type'))
? (await res.text()).match(/(.*?)<\/body>/gi).map(s => {
const stats = s.match(/(<body>|,)(?<stats>.*)<\/body>/i).groups.stats.split(',');
return stats.length === 7
? {
StreamTitle: stats[6],
currentListeners: parseInt(stats[4]),
peakListeners: parseInt(stats[2]),
maxListeners: parseInt(stats[3]),
bitrate: parseInt(stats[5]),
status: parseInt(stats[1]),
serverListeners: parseInt(stats[0])
}
: {
StreamTitle: stats[4],
currentListeners: parseInt(stats[2]),
peakListeners: parseInt(stats[0]),
maxListeners: parseInt(stats[1]),
bitrate: parseInt(stats[3])
};
})
: undefined
}).then(sevenhtml => ({
sevenhtml
}));
}
// http://wiki.winamp.com/wiki/SHOUTcast_DNAS_Server_2_XML_Reponses#General_Server_Summary
/**
* @description Fetches the data from the `/stats` endpoint
* @async
* @returns {object} Object containing results of `/stats`
*/
async getStats() {
// TODO: request correct sid (validate if input url matches /stream/<sid> or, after request, if STREAMPATH matches)
return this[fetchStats]({
status: statsFetchStatus,
endpoint: statsEndpoint,
controller: statsController,
mapper: async res => (/^(?:text|application)\/xml/.test(res.headers.get('content-type')) ? IcecastMetadataStats.xml2Json(await res.text()).SHOUTCASTSERVER : undefined)
}).then(stats => {
console.log(stats);
return {
stats
};
});
}
// http://wiki.winamp.com/wiki/SHOUTcast_DNAS_Server_2_XML_Reponses#Nextsongs
/**
* @description Fetches the data from the `/nextsongs` endpoint
* @async
* @returns {object} Object containing results of `/nextsongs`
*/
async getNextsongs() {
return this[fetchStats]({
status: nextsongsFetchStatus,
endpoint: nextsongsEndpoint,
controller: nextsongsController,
mapper: async res => (/^(?:text|application)\/xml/.test(res.headers.get('content-type')) ? IcecastMetadataStats.xml2Json(await res.text()).SHOUTCASTSERVER.NEXTSONGS : undefined)
}).then(nextsongs => ({
nextsongs
}));
}
async getStreamonkey() {
if (!p.get(this)[streamonkeySessionId]) {
console.log('no sesid');
p.get(this)[streamonkeySessionId] = await this[fetchStats]({
status: streamonkeyFetchStatus,
endpoint: streamEndpoint,
controller: streamonkeyController,
mapper: async res => {
return res.headers.get('sessionid');
}
});
}
console.log(p.get(this)[streamonkeySessionId]);
return this[fetchStats]({
status: streamonkeyFetchStatus,
endpoint: streamonkeyEndpoint,
controller: streamonkeyController,
headers: {
Cookie: `streamingsession=${p.get(this)[streamonkeySessionId]}`,
'Accept-Encoding': 'gzip'
},
mapper: r => (/^(?:text|application)\/json/.test(r.headers.get('content-type')) ? r.json() : undefined)
}).then(async r => {
console.log('got r');
return { streamonkey: r };
});
}
async getHistory() {
let headers = {
'Accept-Encoding': 'gzip'
}
if(p.get(this)[historyEtag])headers['If-None-Match'] = p.get(this)[historyEtag]
return this[fetchStats]({
status: historyFetchStatus,
endpoint: historyEndpoint,
controller: historyController,
mapper: r => {
if(r.status !== 304)
p.get(this)[historyEtag] = r.headers.get('etag')??null
return (/^(?:text|application)\/json/.test(r.headers.get('content-type')) ? r.json() : undefined)
},
headers
}).then(r => ({ history: r }));
}
/**
* @description Fetches the first ICY metadata update from the stream
* @async
* @returns {object} Object containing ICY metadata
*/
async getIcyMetadata() {
return this[getStreamMetadata]({
status: icyFetchStatus,
endpoint: streamEndpoint,
controller: icyController,
metadataType: 'icy',
headers: { 'Icy-MetaData': 1, 'User-Agent': 'Some agent there' }
});
}
/**
* @description Fetches the first Ogg metadata update from the stream
* @async
* @returns {object} Object containing Ogg metadata
*/
async getOggMetadata() {
return this[getStreamMetadata]({
status: oggFetchStatus,
endpoint: streamEndpoint,
controller: oggController,
metadataType: 'ogg'
});
}
async [getStreamMetadata]({ status, endpoint, controller, headers, metadataType }) {
return this[fetchStats]({
status,
endpoint,
controller,
headers,
mapper: async res =>
new Promise(resolve => {
new IcecastReadableStream(res, {
onMetadata: ({ metadata }) => {
p.get(this)[controller].abort();
resolve(metadata);
},
onMetadataFailed: () => {
p.get(this)[controller].abort();
resolve();
},
metadataTypes: metadataType,
icyMetaInt: p.get(this)[icyMetaInt],
icyCharacterEncoding: p.get(this)[icyCharacterEncoding],
icyDetectionTimeout: p.get(this)[icyDetectionTimeout]
}).startReading();
})
}).then(metadata => ({ [metadataType]: metadata }));
}
async [fetchStats]({ status, endpoint, controller, mapper, headers = {} }) {
if (!p.get(this)[status]) {
p.get(this)[status] = true;
return fetch(p.get(this)[endpoint], {
method: 'GET',
headers,
signal: p.get(this)[controller].signal,
timeout: 2000
})
.then(res => {
if (!res.ok &&!res.status==304) {
res.text().then(t => {
console.log(t);
});
throw new Error(`HTTP Error ${res.status}`);
}
return res;
})
.then(mapper)
.catch(e => {
if (e.name !== 'AbortError') {
console.warn(`Failed to fetch ${p.get(this)[endpoint]}`, e);
}
})
.finally(() => {
p.get(this)[status] = false;
p.get(this)[controller] = new AbortController();
});
}
}
}
};