3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00
dbot/modules/radio/radio.js

89 lines
2.5 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;
this.stream = false;
this.internalAPI = {
2014-02-17 21:00:31 +01:00
'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;
_.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']
}));
});
}.bind(this));
stream.on('metadata', function(metadata) {
var title = icecast.parseMetadata(metadata).StreamTitle;
_.each(this.config.announce, function(a) {
dbot.say(a.server, a.name, dbot.t('now_playing', {
'name': this.data['icy-name'],
'song': title,
'url': this.data['icy-url']
}));
}, this);
}.bind(this));
2014-02-17 21:11:11 +01:00
stream.on('end', function() {
2014-02-17 21:51:20 +01:00
this.stream.end();
2014-02-17 21:11:11 +01:00
this.listening = false;
}.bind(this));
}.bind(this),
'getRadio': function() {
dbot.api.timers.addTimer(20000, function() {
if(this.listening == false) {
this.internalAPI.startRadio();
}
}.bind(this));
2014-02-17 21:13:29 +01:00
}.bind(this)
2014-02-17 21:00:31 +01:00
};
//requesting music by pinging the current DJ
//dj should be icy-description inside the headers
//user should be the requesting user
//request should be the event
//TODO:pm dj
this.commands={
'~request music': function(event){
var dj=this.data['icy-description'];
var user=event.user;
var request=event;
dbot.say(dbot.t('request',{
'dj':dj,
'user':user,
'song':song
}));
}
};
2014-02-17 21:00:31 +01:00
this.onLoad = function() {
2014-02-17 21:11:11 +01:00
this.internalAPI.getRadio();
2014-02-17 21:00:31 +01:00
}.bind(this);
2014-02-17 21:31:20 +01:00
this.onDestroy = function() {
2014-02-17 21:51:20 +01:00
this.stream.abort();
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);
};