diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-05-02 17:24:42 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-05-02 17:24:42 +0200 |
commit | a55635dd4da622a82a091b13c533388e0d8a28cb (patch) | |
tree | bfa8dae08d942b2c20de4e0e4d4a22b628f011e4 /lib | |
parent | 6b39765e8e5c346eb7102a9f7b8d183da235fb5e (diff) | |
download | bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.tar.gz bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.tar.bz2 bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.tar.xz bitraf-bot-a55635dd4da622a82a091b13c533388e0d8a28cb.zip |
o Adding last-checkin.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/BitrafBot.js (renamed from lib/client.js) | 43 |
1 files changed, 24 insertions, 19 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) { |