summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-05-02 17:24:42 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-05-02 17:24:42 +0200
commita55635dd4da622a82a091b13c533388e0d8a28cb (patch)
treebfa8dae08d942b2c20de4e0e4d4a22b628f011e4
parent6b39765e8e5c346eb7102a9f7b8d183da235fb5e (diff)
downloadbitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.tar.gz
bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.tar.bz2
bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.tar.xz
bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.zip
o Adding last-checkin.
-rw-r--r--lib/BitrafBot.js (renamed from lib/client.js)43
-rw-r--r--main.js11
2 files changed, 31 insertions, 23 deletions
diff --git a/lib/client.js b/lib/BitrafBot.js
index adbc25a..9388ddb 100644
--- a/lib/client.js
+++ b/lib/BitrafBot.js
@@ -8,7 +8,7 @@ var IRC = require('../node_modules/node-irc/IRC.js').IRC
var BitrafBot = function(config) {
var nick = '';
var nickCount = 0;
- var nickAdditions = [ '', '^', '_', '\\', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
+ var nickAdditions = [ '', '^', '-', '_', '\\', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
function findNick() {
if (nickCount == nickAdditions.length) {
@@ -27,34 +27,39 @@ var BitrafBot = function(config) {
});
irc.on('privmsg', function(from, to, message) {
if(message == ',checkins') {
- console.log('checkins request.');
- var now = new Date();
- var from = (now.getYear() + 1900) + "-" + (now.getMonth() + 1) + "-" + now.getDate();
+ var checkinsUrl = config.checkinsUrl
+ console.log('checkins request. url=' + checkinsUrl);
- var options = url.parse(config.checkinsUrl + '?from=' + from);
+ var options = url.parse(checkinsUrl);
options.headers = { Accept: 'application/vnd.collection+json' };
options.method = 'GET';
var req = http.get(options, function(res) {
- var chunks = "";
+ var chunks = '';
res.on('data', function (chunk) {
chunks += chunk;
});
res.on('end', function () {
- // console.log("chunks", chunks);
- data = JSON.parse(chunks);
- var checkins = -1;
- _.each(data["collection"].items, function(item, i) {
- _.each(item.data, function(item, i) {
- if(item.name === "checkins") {
- checkins = parseInt(item.value);
- }
+ try {
+ data = JSON.parse(chunks);
+ var checkins = -1;
+ var lastCheckin = 'unknown';
+ _.each(data['collection'].items, function(item, i) {
+ _.each(item.data, function(item, i) {
+ switch(item.name) {
+ case 'checkins': checkins = parseInt(item.value); break;
+ case 'last-checkin': lastCheckin = item.value; break;
+ }
+ });
});
- });
- if(checkins != -1) {
- irc.privmsg(to, 'checkins: ' + checkins);
+ if(checkins != -1) {
+ irc.privmsg(to, 'checkins: ' + checkins + ', last checkin: ' + lastCheckin);
+ }
+ else {
+ irc.privmsg(to, 'Could not find any checkins');
+ }
}
- else {
- irc.privmsg(to, 'Could not find any checkins');
+ catch(ex) {
+ irc.privmsg(to, 'Could not parse document');
}
});
}).on('error', function(e) {
diff --git a/main.js b/main.js
index 6774bae..105543d 100644
--- a/main.js
+++ b/main.js
@@ -12,17 +12,20 @@ parser.option("--checkinsUrl", "URL to use when looking for checkins");
var args = process.argv.slice(2);
var defaults = {
channel: "#bitraf",
- checkinsUrl: 'http://hermes.bitraf.no/~trygvis/checkins.php',
+ checkinsUrl: 'http://hermes.bitraf.no/~trygvis/checkins/checkins-by-day.php?from=today',
nick: os.hostname()
};
try {
var config = parser.parse(args);
+ if (config.help) {
+ console.log(parser.help());
+ return;
+ }
config = _.defaults(config, defaults);
- console.log("config", config);
- if (config.help) console.log(parser.help());
- var bot = require('./lib/client.js')(config);
+ console.log("configuration: ", config);
+ var bot = require('./lib/BitrafBot.js')(config);
var r = repl.start('irc> ');
r.context.bot = bot;
r.context.config = config;