3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-27 21:22:38 +01:00
dbot/modules/radio/radio.js

112 lines
3.7 KiB
JavaScript
Raw Normal View History

2014-02-17 21:00:31 +01:00
/**
* Module Name: Radio
* Description: Various icecast functionality.
*/
var _ = require('underscore')._,
icecast = require('icecast-stack');
var radio = function(dbot) {
this.listening = false;
this.data = false;
2015-05-06 20:18:27 +02:00
this.title = false;
2014-02-17 21:31:20 +01:00
this.stream = false;
this.recentUpdate = false;
2014-02-17 21:00:31 +01:00
this.internalAPI = {
'startRadio': function() {
var stream = icecast.createReadStream(this.config.stream);
2014-02-17 21:31:20 +01:00
this.stream = stream;
2014-02-17 21:00:31 +01:00
stream.on('connect', function() {
this.listening = true;
}.bind(this));
stream.on('response', function(res) {
this.data = res.headers;
2014-09-18 05:33:40 +02:00
if(res.headers['icy-name']) {
_.each(this.config.announce, function(a) {
dbot.say(a.server, a.name, dbot.t('now_online', {
'name': res.headers['icy-name'],
'desc': res.headers['icy-description'],
'url': res.headers['icy-url']
}));
});
}
2014-02-17 21:00:31 +01:00
}.bind(this));
stream.on('metadata', function(metadata) {
2015-05-06 20:17:06 +02:00
this.title = icecast.parseMetadata(metadata).StreamTitle;
2015-05-06 20:18:27 +02:00
if(!_.isUndefined(this.title) && this.data['icy-name']) { // sowwy jesus
2014-07-19 21:58:22 +02:00
_.each(this.config.announce, function(a) {
2015-07-20 17:19:57 +02:00
if(this.recentUpdate == false) {
2014-07-19 21:58:22 +02:00
dbot.say(a.server, a.name, dbot.t('now_playing', {
'name': this.data['icy-name'],
2015-05-06 20:17:06 +02:00
'song': this.title,
2014-07-19 21:58:22 +02:00
'url': this.data['icy-url']
}));
2015-07-20 17:21:56 +02:00
this.recentUpdate = true;
}
2014-07-19 21:58:22 +02:00
}, this);
}
2014-02-17 21:00:31 +01:00
}.bind(this));
2014-02-17 21:11:11 +01:00
stream.on('end', function() {
this.listening = false;
}.bind(this));
}.bind(this),
2014-02-17 21:00:31 +01:00
};
2014-02-20 21:10:45 +01:00
this.commands={
'~request': function(event){
var dj = this.data['icy-description'],
song = event.input[1];
2014-07-19 18:57:45 +02:00
2014-09-04 16:54:49 +02:00
dbot.api.users.resolveUser(event.server, dj, function(err, user) {
2014-07-19 18:57:45 +02:00
if(user) {
dbot.say(event.server, user.currentNick, dbot.t('radio_request',{
'user': event.user,
'song': song
}));
event.reply('Song request sent to DJ ' + user.currentNick + '!');
} else {
event.reply('Couldn\'t find DJ ' + dj + ' on IRC :(');
}
});
2015-05-03 11:29:36 +02:00
},
'~nowplaying': function(event) {
if(this.listening) {
2015-05-06 20:20:25 +02:00
event.reply(dbot.t('now_playing', {
2015-05-03 11:29:36 +02:00
'name': this.data['icy-name'],
2015-05-06 20:17:06 +02:00
'song': this.title,
2015-05-03 11:29:36 +02:00
'url': this.data['icy-url']
2015-05-06 20:20:25 +02:00
}));
2015-05-03 11:29:36 +02:00
} else {
event.reply('Radio not playing.');
}
2014-02-20 21:10:45 +01:00
}
};
2014-07-19 21:58:22 +02:00
this.commands['~request'].regex = [/^request (.*)/, 2];
2014-02-17 21:00:31 +01:00
this.onLoad = function() {
2014-02-20 18:38:58 +01:00
this.internalAPI.startRadio();
dbot.api.timers.addTimer(20000, function() {
2014-07-19 21:58:22 +02:00
if(this.listening === false) {
2014-02-20 18:38:58 +01:00
this.internalAPI.startRadio();
}
}.bind(this));
dbot.api.timers.addTimer(120000, function() {
this.recentUpdate = false;
}.bind(this));
2014-02-17 21:00:31 +01:00
}.bind(this);
2014-02-17 21:31:20 +01:00
this.onDestroy = function() {
2014-02-20 18:38:58 +01:00
this.stream.end();
this.stream.destroy();
2014-02-17 21:31:20 +01:00
}.bind(this);
2014-02-17 21:00:31 +01:00
};
exports.fetch = function(dbot) {
return new radio(dbot);
};