-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (65 loc) · 2.49 KB
/
index.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
/* jshint node: true */
/*jshint esversion: 6 */
var streamer = function (configuration, callback) {
var generator = require('./conf_generator.js');
const {
spawn
} = require('child_process');
var fs = require('fs');
if (typeof (configuration) === 'string') {
generator.read(configuration, function (data) {
this.configuration = data;
});
} else if (typeof (configuration) === 'object') {
this.configuration = configuration;
if (!configuration.filename) {
console.log('[WARN]No playlist specified so creating a new one.'); // If no playlist is specified let us create ourselves
configuration.filename = __dirname + '/playlist.m3u';
fs.writeFile(configuration.filename, '#EXTM3U\n', function (err) {
if (err) throw err;
console.log('[INFO]New playlist file: ' + configuration.filename + ' is generated.');
if (typeof (callback) === 'function')
generator.create('ezstream-conf.xml', configuration, callback); // Hard coded for now
else generator.create('ezstream-conf.xml', configuration);
});
}
} else {
throw "icy-streamer constructor only takes either an object or a string! But yours is " + typeof (configuration);
}
this.running = false;
var self = this;
this.addSong = function (filename, callback) {
if (!fs.existsSync(filename)) throw "No file exists with this path : " + filename;
fs.appendFile(self.configuration.filename, filename + "\n", function (err) {
if (err) throw err;
console.log('[INFO]New song: ' + filename + ' added to playlist.');
if (self.running)
self.flushPlayList();
if (typeof (callback) === 'function') callback();
});
};
this.flushPlayList = function () {
self.Stream.kill('SIGHUP');
console.log('[INFO]Flushed the playlist.');
};
this.killStream = function () {
if (!self.running) throw "Tried to stop radio stream while it wasn't running already.";
self.Stream.kill();
self.running = false;
self.Stream = null;
};
this.startStream = function () {
try {
self.Stream = spawn('ezstream', ['-c', 'ezstream-conf.xml']);
self.Stream.on("exit", function () {
console.log("[INFO] Ezstream is dead now.");
self.running = false;
});
self.running = true;
} catch (error) {
throw error;
}
};
return this;
};
module.exports = streamer;