-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
696 lines (521 loc) · 18.6 KB
/
utils.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
const Url = require('url');
const HTTPS = require("https");
const HTTP = require("http");
const XMLWriter = require('xml-writer');
const Winston = require('winston');
const Moment = require('moment');
const Path = require('path');
const Constants = require('./constants');
const FS = require('fs');
const PVR_GENRE_INDEX = 0;
const TV_HEAD_PVR_GENRE_INDEX = 1;
let WinstonTransportFile = new Winston.transports.File({ filename: Path.join(calculateLogPath(__filename), 'manager.log') , level: 'info', format: Winston.format.simple(), 'timestamp':true });
let Log = Winston.createLogger({
level: 'info',
// format: winston.format.json(),
// defaultMeta: { service: 'user-service' },
transports: []
});
Log.add(WinstonTransportFile)
Log.add(new Winston.transports.Console());
function setLogLevel(level) {
WinstonTransportFile.level = level || global.Config.LogLevel || 'info';
}
function cleanUpString( str ) {
return str ? str.replace( /^"|"$/g, '' ) : str;
}
function request(url, headers, callback, streaming) {
const urlObj = Url.parse( url );
const opts = Object.assign(urlObj, {headers: headers});
const protocol = opts.protocol.toLowerCase();
if ( ['http:', 'https:'].indexOf(protocol) <= -1 ) {
callback( 'protocol not supported', url);
return;
}
const MODULE = protocol.indexOf('https') === 0 ? HTTPS : HTTP;
MODULE.get(opts, (res) => {
if ( res.statusCode >= 200 && res.statusCode < 300 ) {
if ( streaming ) {
return callback(null, res);
}
const bufs = [];
res.on('data', (chunk) => {
bufs.push(chunk);
});
res.on('end', () => {
const buf = Buffer.concat(bufs);
const string = buf.toString('utf8');
Log.debug(`Finish getting data from ${url} ${string.length} bytes`);
callback( null, string );
})
} else if ( (res.statusCode > 300 && res.statusCode <= 303) && (res.headers && res.headers.location)) {
request(res.headers.location, headers, callback, streaming);
} else /* if ( res.statusCode >= 400 ) */ {
let error = res.statusCode >= 300 && res.statusCode < 400 ? `url redirects to ${res.headers && res.headers.location}` : res.statusCode;
Log.warn(`Error: ${error}`);
callback( error, null );
}
});
}
function _URL_(str, base) {
return new URL(str, base);
}
function urlShouldBeComputed(url, base) {
if ( typeof url === 'string' ) {
url = _URL_(url, base);
}
const pathname = url.pathname;
const ext = pathname.split('.').pop();
return ['htm', 'html', 'm3u', 'm3u8'].indexOf( ext.toLowerCase() ) > -1;
}
function responseToString(res) {
return new Promise( (resolve, reject) => {
const buff = [];
// res.setEncoding('utf8');
res.on('data', (chunk) => {
buff.push(chunk);
});
res.on('end', () => {
const b = Buffer.concat(buff);
const string = b.toString('utf8');
resolve(string);
});
});
}
function calculateNewUrlToCompute(url, base) {
let nurl = null;
try {
nurl = new URL(url, base);
} catch(e) {
Log.error(`Cannot resolve url '${url}' based on '${base}'`);
throw e;
}
return computeChannelStreamUrl({StreamUrl: nurl.href});
}
function computeChannelStreamUrl(channel) {
const chl_url = channel.StreamUrl;
const sc = urlShouldBeComputed( chl_url );
const urlObj = Url.parse( chl_url );
const protocol = urlObj.protocol.toLowerCase();
Log.debug(`Compute channel stream url for protocol: ${protocol}`);
return new Promise( (resolve, reject) => {
if ( !sc ) {
Log.debug('url doesn\'t need to be computed');
return resolve( chl_url );
}
if ( ['http:', 'https:'].indexOf(protocol) <= -1 ) {
Log.warn(`stream protocol cannot be computed. Use the original one: ${urlObj.protocol.toLowerCase()}`)
resolve(chl_url);
return;
}
const MODULE = protocol.indexOf('https') === 0 ? HTTPS : HTTP;
const chl_url_obj = Url.parse(chl_url);
const opts = Object.assign(chl_url_obj, {
method: 'GET',
headers: {
"user-agent": "VLC"
}
});
const Req = MODULE.request(opts, (res) => {
const contentType = res.headers['content-type'];
const location = res.headers['location'];
if ( res.statusCode >= 200 && res.statusCode < 300 ) {
// we have a direct response
if ( contentType.indexOf('mpegURL') > -1 ) {
responseToString(res).then( (data) => {
let schl = null;
try {
schl = parseM3U( data );
} catch(e) {
Log.error(`Cannot parse m3u while computing due to: ${e}`);
return resolve( chl_url );
}
let cnutc = null;
try {
cnutc = calculateNewUrlToCompute(schl.StreamUrl, chl_url);
} catch(e) {
return resolve(chl_url);
}
cnutc.then( (new_url) => {
resolve( new_url );
});
});
}
} else if ( res.statusCode >= 300 && res.statusCode < 400 ) {
let cnutc = null;
try {
cnutc = calculateNewUrlToCompute(location, chl_url);
} catch(e) {
return resolve(chl_url);
}
cnutc.then( (new_url) => {
resolve( new_url );
});
} else {
Log.error(`Error while computing stream-url: Status ${res.statusCode}`);
resolve( chl_url );
}
});
Req.on('error', (e) => {
Log.error(`An error occurred while computing channel stream-url: ${e}`);
resolve( chl_url );
});
Req.end();
});
}
function parseM3U(str) {
const M3UK = require('./modules/m3u').M3U;
const M3U = new M3UK();
M3U.load(str);
return M3U.groups[0].channels[0];
}
function createXMLKodiLive(groups, base_url) {
const XW = new XMLWriter();
XW.startDocument('1.0', 'UTF-8');
const Data = XW.startElement('data');
const Type = Data.startElement('type');
Type.writeAttribute('name', 'list');
for( let group of groups ) {
const Item = Type.startElement('item');
const Name = Item.startElement('name');
Name.text( group.Name );
Name.endElement();
const Link = Item.startElement('link');
Link.text( `${[base_url, group.Id].join('/')}.m3u8` )
Link.endElement();
const Icon = Item.startElement('icon');
Icon.endElement();
const Fanart = Item.startElement('fanart');
Fanart.endElement();
const Color = Item.startElement('color');
Color.text('green')
Color.endElement();
Item.endElement();
}
Type.endElement();
Data.endElement();
XW.endDocument();
return XW;
}
function createXMLTV(EPG, SHIFT, DETAILED, ASSOCIATIONS) {
if ( ! Array.isArray(SHIFT) ) {
SHIFT = [SHIFT];
}
if ( SHIFT[0] !== 0 ) {
SHIFT.unshift( 0 );
}
Log.info('creating XMLTV');
Log.debug(`Shift hours ${SHIFT.join(', ')}`)
const XW = new XMLWriter();
XW.startDocument('1.0', 'UTF-8');
const TV = XW.startElement('tv');
TV.writeAttribute('source-info-name', 'EPG');
TV.writeAttribute('generator-info-name', 'simple tv grab it');
TV.writeAttribute('generator-info-url', '');
let module_names = Object.keys(EPG);
// LOOP FOR CHANNELs
for ( let module_name of module_names ) {
let association_module = null;
if ( ASSOCIATIONS ) {
if ( ! (module_name in ASSOCIATIONS) ) {
Log.info(`${module_name} epg-module will be skipped`);
// next module
continue;
}
association_module = ASSOCIATIONS[ module_name ];
}
let module_channel_list = EPG[ module_name ];
for( let CHL of module_channel_list ) {
let OriginalIdEpg = CHL.IdEpg;
if ( association_module ) {
if ( ! (OriginalIdEpg in association_module) ) {
Log.debug(`${OriginalIdEpg} of ${module_name} has not been requested, skip!`);
continue;
}
OriginalIdEpg = association_module[ OriginalIdEpg ] || OriginalIdEpg;
}
for ( let shift of SHIFT ) {
let IdEpg = OriginalIdEpg;
const chl_id = shift ? `${IdEpg} +${shift}` : IdEpg;
const chl_name = shift ? `${CHL.Name} +${shift}` : CHL.Name;
const chl_el = TV.startElement('channel');
chl_el.writeAttribute('id', chl_id);
chl_el.writeAttribute('name', chl_name);
if ( ! shift ) {
chl_el.writeAttribute('number', CHL.Number);
}
chl_el.startElement('display-name')
.writeAttribute('lang', 'it')
.text(chl_name)
.endElement();
if ( !shift && CHL.Number ) {
chl_el.startElement('display-name')
.writeAttribute('lang', 'it')
.text(CHL.Number)
.endElement();
}
chl_el.startElement('icon').writeAttribute('src', CHL.Logo).endElement();
if ( CHL.Url ) {
chl_el.startElement('url').text( CHL.Url ).endElement();
}
chl_el.endElement();
}
}
}
// LOOP FOR PROGRAMMEs
for ( let module_name of module_names ) {
let association_module = null;
if ( ASSOCIATIONS ) {
if ( ! (module_name in ASSOCIATIONS) ) {
Log.info(`${module_name} epg-module will be skipped`);
// next module
continue;
}
association_module = ASSOCIATIONS[ module_name ];
}
let module_channel_list = EPG[ module_name ];
for( let CHL of module_channel_list ) {
let OriginalIdEpg = CHL.IdEpg;
if ( association_module ) {
if ( ! (OriginalIdEpg in association_module) ) {
Log.debug(`${OriginalIdEpg} of ${module_name} has not been requested, skip!`);
continue;
}
OriginalIdEpg = association_module[ OriginalIdEpg ] || OriginalIdEpg;
}
for( let shift of SHIFT ) {
let IdEpg = OriginalIdEpg;
const chl_id = shift ? `${IdEpg} +${shift}` : IdEpg;
const dates = Object.keys( CHL.Epg );
for ( let datetime_str of dates ) {
const programs = CHL.Epg[ datetime_str ];
for ( let PRG of programs ) {
const prg_el = TV.startElement('programme');
let starttime = new Date(PRG.Start);
starttime.setMinutes( starttime.getMinutes() + (60 * shift) );
prg_el.writeAttribute('start', Moment(starttime).format('YYYYMMDDHHmmss Z').replace(':', '') );
let endtime = new Date(PRG.Stop);
endtime.setMinutes( endtime.getMinutes() + (60 * shift) );
prg_el.writeAttribute('stop', Moment(endtime).format('YYYYMMDDHHmmss Z').replace(':', '') );
prg_el.writeAttribute('channel', chl_id);
let prg_title = PRG.Title;
if ( PRG.Prima ) {
if ( prg_title.indexOf('1^ TV') < 0 ) {
prg_title = `${prg_title.trim()} - 1^ TV`;
}
}
const title_el = prg_el.startElement('title').writeAttribute('lang', 'it')
.text(prg_title || '')
.endElement();
const description_el = prg_el.startElement('desc').writeAttribute('lang', 'it')
if ( PRG.Description ) {
description_el.text(PRG.Description || '');
}
description_el.endElement();
const subtitles_el = prg_el.startElement('sub-title').writeAttribute('lang', 'it')
if ( PRG.data.desc ) {
subtitles_el.text( PRG.data.desc.substring(0, 50) );
}
subtitles_el.endElement();
const genre_el = prg_el.startElement('category').writeAttribute('lang', 'it')
.text(PRG.Genre || PRG.Subgenre || '')
.endElement();
if ( DETAILED !== false ) {
// print all data if detailed requested
let category;
category = extractCategoryByGenre(PRG.Genre, PRG.Subgenre, PVR_GENRE_INDEX);
Log.debug(`extracted PVR category: ${category}`);
if ( category ) {
const category_el = prg_el.startElement('category').writeAttribute('lang', 'it')
.text( category )
.endElement();
}
category = extractCategoryByGenre(PRG.Genre, PRG.Subgenre, TV_HEAD_PVR_GENRE_INDEX);
Log.debug(`extracted TvHeadEnd category : ${category}`);
if ( category ) {
const category_el = prg_el.startElement('category').writeAttribute('lang', 'it')
.text( category )
.endElement();
}
const subgenre_el = prg_el.startElement('category').writeAttribute('lang', 'it')
.text(PRG.Subgenre || '')
.endElement();
const country_el = prg_el.startElement('country')
.text('IT')
.endElement();
if ( PRG.Poster ) {
const thumbnail_url_el = prg_el.startElement('icon')
.text(PRG.Poster || '')
.endElement();
}
if ( PRG.Date ) {
const date_el = prg_el.startElement('date')
.text( PRG.Date )
.endElement();
}
if ( PRG.Director || (PRG.Actors && PRG.Actors.length > 0) ) {
const credits_el = prg_el.startElement('credits')
if ( PRG.Director ) {
credits_el.startElement('director')
.text( PRG.Director )
.endElement();
}
if ( PRG.Actors && PRG.Actors.length > 0) {
for ( let act of PRG.Actors ){
if ( !act ) continue;
credits_el.startElement('actor')
.text( act )
.endElement();
}
}
credits_el.endElement();
}
if ( PRG.Episode ) {
let _epidose_str = PRG.Episode;
prg_el.startElement('episode-num')
.writeAttribute('system', 'xmltv_ns')
.text( _epidose_str )
.endElement();
let _eps = _epidose_str.split('.');
let s = parseInt(_eps[0], 10 );
let e = parseInt(_eps[1], 10 );
s = s || s == '0' ? `S${s + 1}` : false;
if ( s ) {
e = e || e == '0' ? `E${e + 1}` : '';
prg_el.startElement('episode-num')
.writeAttribute('system', 'onscreen')
.text( `${s}${e}` )
.endElement();
}
}
if ( PRG.Url ) {
prg_el.startElement('url').text( PRG.Url ).endElement();
}
}
prg_el.endElement();
}
}
}
}
}
TV.endElement();
XW.endDocument();
return XW;
}
function extractCategoryByGenre(genre, subgenre, index) {
genre = `${genre || ''}`.toLowerCase();
subgenre = `${subgenre || ''}`.toLowerCase();
// Arts / Culture (without music)
// Children's / Youth programs
// Education / Science / Factual topics
// Leisure hobbies
// Movie / Drama
// Music / Ballet / Dance
// News / Current affairs
// Show / Game show
// Social / Political issues / Economics
// Sports
// TV HEAD END
// genre = ` ${genre} `.toLowerCase();
// Log.debug(`extracting cateogry from ${genre}`);
// if ( genre.indexOf(` musica ` ) > -1 ) {
// return 'Music / Ballet / Dance';
// } else if ( genre.indexOf( ' informazione ' ) > -1 || genre.indexOf( ' notiziario ' ) > -1 ) {
// return 'News / Current affairs';
// } else if ( genre.indexOf( ' mondo ' ) > -1 || genre.indexOf( ' tendenze ' ) > -1 ) {
// return 'Education / Science / Factual topics';
// } else if ( genre.indexOf(` educational ` ) > -1 ) {
// return 'Education / Science / Factual topics'
// } else if ( genre.indexOf(` cartoni animati ` ) > -1 ) {
// return 'Children\'s / Youth programs';
// } else if ( genre.indexOf(` notizie ` ) > -1 ) {
// return 'News / Current affairs';
// } else if ( genre.indexOf(` storia ` ) > -1 ) {
// return 'Arts / Culture'
// } else if ( genre.indexOf(`sport` ) > -1 ) {
// return 'Sports';
// } else {
// return null
// }
// any KODI PVR
Log.debug(`extracting category from ${genre}`);
let Categories = Constants.Categories;
if ( genre in Categories ) {
Log.debug(`found category ${genre}`);
let subCategories = Categories[ genre ];
let def = subCategories.Default;
if ( subgenre in subCategories ) {
Log.debug(`found sub-category ${subgenre}`);
return subCategories[ subgenre ][ index ];
}
Log.debug(`using default sub-category`);
return def ? def[ index ] : null;
}
return null
}
function calculateLogPath(filename) {
let logfilepath = global.Argv.logfilepath || global.Config.Log;
if ( logfilepath ) {
let path = '';
try {
if ( FS.lstatSync(logfilepath).isDirectory() ) {
path = logfilepath;
} else {
path = Path.dirname(logfilepath);
}
} catch(e) {
console.warn(logfilepath, 'error', e);
path = Path.dirname(logfilepath);
}
if ( FS.existsSync(path) ) {
return path
} else {
console.warn(logfilepath, 'does not exist');
}
}
return Constants.calculatePath(filename);
}
function rewriteChannelUrl(rewrite, channel, listName) {
let streamurl = channel.Redirect;
if ( rewrite ) {
// let sourceobj = {
// channel,
// ListName: listName
// }
let surl = rewrite;
let re = /\{(.*?)\}/;
let reFn = /\{(((.*?)\()?)(.*?)\)?\}/;
surl = surl.replace( new RegExp(re, "gi"), function(s){
let exp = s.match( reFn )[4];
// let fn = s.match(reFn)[3];
let bodyFn = `
try {
return ${exp};
} catch(e) {
Log.warn("cannot rewrite url '${exp}' - " + e);
return '';
}
`;
let execFn = new Function('channel', 'ListName', 'Log', bodyFn);
// let obj = sourceobj;
// let exps = exp.split('.');
// while ( exps.length && obj ) {
// obj = obj[ exps.shift() ];
// }
// if ( obj ) {
// if ( fn ) {
// fn = sourceObj[ fn ];
// if ( fn ) {
// obj = fn( obj );
// }
// }
// return obj
// }
let ret = execFn(channel, listName, Log);
return ret;
});
return surl;
}
return streamurl;
}
module.exports = {cleanUpString, request, createXMLTV, Log, setLogLevel, computeChannelStreamUrl, _URL_, urlShouldBeComputed, createXMLKodiLive, rewriteChannelUrl};