diff --git a/modules/radio/config.json b/modules/radio/config.json new file mode 100644 index 0000000..3cb4868 --- /dev/null +++ b/modules/radio/config.json @@ -0,0 +1,5 @@ +{ + "stream": "", + "announce": [ { "server": "aberwiki", "name": "#dbot" } ], + "outputPrefix": "\u00033radio\u000f" +} diff --git a/modules/radio/radio.js b/modules/radio/radio.js new file mode 100644 index 0000000..38ce3c0 --- /dev/null +++ b/modules/radio/radio.js @@ -0,0 +1,68 @@ +/** + * 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 = { + 'startRadio': function() { + var stream = icecast.createReadStream(this.config.stream); + this.stream = stream; + + 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)); + + stream.on('end', function() { + this.stream.end(); + this.listening = false; + }.bind(this)); + }.bind(this), + + 'getRadio': function() { + dbot.api.timers.addTimer(20000, function() { + if(this.listening == false) { + this.internalAPI.startRadio(); + } + }.bind(this)); + }.bind(this) + }; + this.onLoad = function() { + this.internalAPI.getRadio(); + }.bind(this); + this.onDestroy = function() { + this.stream.abort(); + }.bind(this); +}; + +exports.fetch = function(dbot) { + return new radio(dbot); +}; diff --git a/modules/radio/strings.json b/modules/radio/strings.json new file mode 100644 index 0000000..ecd3157 --- /dev/null +++ b/modules/radio/strings.json @@ -0,0 +1,8 @@ +{ + "now_online": { + "en": "Now Online: {name} - {desc} - {url}" + }, + "now_playing": { + "en": "Now Playing: {name} - {song} - {url}" + } +}