-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathriver.js
29 lines (24 loc) · 1.04 KB
/
river.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
var sys = require('sys');
var http = require('http');
var util = require('util');
function addRiverAnswerer(bot, properties, riverCode, command) {
var url = 'http://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=' + riverCode + '&output=xml';
var commandRegex = new RegExp(command);
util.log("Adding river query " + url);
bot.addCommandListener(command, commandRegex, "water level", function() {
http.get(url, function(response) {
var data = "";
response.setEncoding('utf8');
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
var match = data.match(/<valid timezone="UTC">\d+-\d+-\d+T(\d\d:\d\d:\d\d)-00:00<\/valid><primary name="Stage" units="ft">(\d+\.\d+)<\/primary>/);
if (match) {
bot.say("As of " + match[1] + " UTC, the river is at " + match[2] + " feet.");
}
});
});
});
}
module.exports = addRiverAnswerer;