2013-01-30 13:12:10 +01:00
|
|
|
/**
|
2013-01-30 13:21:46 +01:00
|
|
|
* Module Name: Rain
|
|
|
|
* Description: Quietly judges your choice of climate.
|
2013-01-30 13:12:10 +01:00
|
|
|
*/
|
|
|
|
var request = require('request'),
|
|
|
|
_ = require('underscore')._;
|
|
|
|
|
|
|
|
var rain = function(dbot) {
|
|
|
|
var commands = {
|
|
|
|
'~rain': function(event) {
|
2013-01-30 13:27:08 +01:00
|
|
|
var apikey = dbot.config.rain.apikey;
|
2013-01-30 14:29:49 +01:00
|
|
|
var place = event.input[1];
|
|
|
|
if (!place) { var place = "Aberystwyth"; }
|
2013-02-07 17:47:05 +01:00
|
|
|
var url = "http://api.wunderground.com/api/" + apikey + "/conditions/q/" + place + ".json";
|
2013-01-30 13:38:27 +01:00
|
|
|
request(url, function(error, response, body) {
|
|
|
|
if(response.statusCode == "200") {
|
|
|
|
var data = JSON.parse(body);
|
2013-01-30 14:29:49 +01:00
|
|
|
var obs = data["current_observation"];
|
|
|
|
if (obs) {
|
|
|
|
var precip = obs["precip_1hr_metric"];
|
|
|
|
var score = 2 * Math.pow(precip,0.5);
|
|
|
|
score = Math.ceil(score);
|
|
|
|
if (score > 10) { score = 11; }
|
|
|
|
} else {
|
|
|
|
var score = "u";
|
|
|
|
}
|
2013-01-30 13:38:27 +01:00
|
|
|
} else {
|
|
|
|
var score = "e";
|
|
|
|
}
|
2013-02-07 17:47:05 +01:00
|
|
|
event.reply(dbot.t("rain-"+score,{"place": place}));
|
2013-01-30 13:38:27 +01:00
|
|
|
});
|
2013-01-30 13:12:10 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-30 14:29:49 +01:00
|
|
|
commands['~rain'].regex = [/~rain (.+)/, 2];
|
2013-01-30 13:12:10 +01:00
|
|
|
this.commands = commands;
|
|
|
|
this.on = 'PRIVMSG';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new rain(dbot);
|
|
|
|
};
|