From 0069bdf5a4af072322d0b1ce886cbd54b0efa588 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 27 Feb 2014 01:34:40 +0000 Subject: [PATCH] goodreads ~books --- modules/goodreads/goodreads.js | 96 +++++++++++++++++++++++++++++++++- modules/goodreads/strings.json | 6 +++ modules/profile/api.js | 2 +- 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/modules/goodreads/goodreads.js b/modules/goodreads/goodreads.js index 837f7b0..10fb7c2 100644 --- a/modules/goodreads/goodreads.js +++ b/modules/goodreads/goodreads.js @@ -12,6 +12,22 @@ var _ = require('underscore')._, var goodreads = function(dbot) { this.ApiRoot = 'https://www.goodreads.com/'; + this.internalAPI = { + 'getGoodreads': function(server, nick, callback) { + dbot.api.profile.getProfile(server, nick, function(err, user, profile) { + if(user) { + if(profile && _.has(profile.profile, 'goodreads')) { + callback(user, profile.profile.goodreads); + } else { + callback(user, null); + } + } else { + callback(null, null); + } + }); + } + }; + this.api = { 'searchBook': function(term, callback) { request.get({ @@ -21,7 +37,7 @@ var goodreads = function(dbot) { 'key': this.config.api_key } }, function(err, response, body) { - if(!_.isUndefined(body)) { + if(!_.isUndefined(body) && !err) { parseString(body, function(err, result) { // This is why we don't use XML kids var result = result['GoodreadsResponse'].search[0].results[0]; @@ -40,6 +56,32 @@ var goodreads = function(dbot) { callback(true, null); } }.bind(this)); + }, + + 'getProfile': function(id, callback) { + request.get({ + 'url': this.ApiRoot + 'user/show.xml', + 'qs': { + 'username': id, + 'key': this.config.api_key + } + }, function(err, response, body) { + if(!_.isUndefined(body) && !err) { + parseString(body, function(err, result) { + if(result && _.has(result, 'GoodreadsResponse')) { + result = result['GoodreadsResponse'].user[0].user_shelves[0].user_shelf; + _.each(result, function(shelf) { + shelves[shelf.name[0]] = shelf.book_count[0]['_']; + }); + callback(null, shelves); + } else { + callback(true, null); + } + }); + } else { + callback(true, null); + } + }); } }; @@ -57,9 +99,61 @@ var goodreads = function(dbot) { event.reply(dbot.t('gr_nobook')); } }.bind(this)); + }, + + '~books': function(event) { + var user = event.rUser, + gr = event.rProfile.goodreads; + if(event.res[0]) { + user = event.res[0].user; + gr = event.res[0].gr; + } + + this.api.getProfile(gr, function(err, profile) { + if(!err) { + event.reply(dbot.t('gr_books', { + 'user': user.currentNick, + 'read': profile.read, + 'currently_reading': profile['currently-reading'] + })); + } else { + event.reply(dbot.t('gr_unknown')); + } + }); } }; this.commands['~book'].regex = [/^book ([\d\w\s-]*)/, 2]; + + _.each(this.commands, function(command) { + command.resolver = function(event, callback) { + if(event.rProfile && _.has(event.rProfile, 'goodreads')) { + if(event.params[1]) { + this.internalAPI.getGoodreads(event.server, event.params[1], function(user, gr) { + if(user && lfm) { + event.res.push({ + 'user': user, + 'gr': gr + }); + callback(false); + } else { + if(!user) { + event.reply('Unknown user.'); + } else { + event.reply(user.currentNick + ': Set a Goodreads username with "~set goodreads username"'); + } + callback(true); + } + }); + } else { + callback(false); + } + } else { + event.reply(event.user + ': Set a goodreads username with "~set goodreads username"'); + callback(true); + } + }.bind(this); + }, this); + }; exports.fetch = function(dbot) { diff --git a/modules/goodreads/strings.json b/modules/goodreads/strings.json index 1949cf2..deff680 100644 --- a/modules/goodreads/strings.json +++ b/modules/goodreads/strings.json @@ -4,5 +4,11 @@ }, "gr_nobook": { "en": "No books found." + }, + "gr_books": { + "en": "{user} has read {read} books and is currently reading {currently_reading} books." + }, + "gr_unknown": { + "en": "Unknown Goodreads username!" } } diff --git a/modules/profile/api.js b/modules/profile/api.js index db3107a..9324270 100644 --- a/modules/profile/api.js +++ b/modules/profile/api.js @@ -78,7 +78,7 @@ var api = function(dbot) { } }); }, - + 'setProperty': function(server, nick, field, value, callback){ this.api.getProfile(server, nick, function(err, user, profile){ if(!err){