From 87a1198b2075c43754c4f45a62219365b97197ef Mon Sep 17 00:00:00 2001 From: Yohei Sasaki Date: Sun, 27 Feb 2011 18:23:05 +0900 Subject: [PATCH] autoFollow plugin, improve error handling, friendship bug fixes. --- lib/plugins/autoFollow.js | 50 +++++++++++++++++++++++++++++++++ lib/twitter/friendships.js | 8 +++--- lib/twitter/index.js | 1 + lib/util.js | 6 +++- test/twitter/test_friedships.js | 28 ++++++++++++++++++ test/twitter/test_tweets.js | 2 +- 6 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 lib/plugins/autoFollow.js create mode 100644 test/twitter/test_friedships.js diff --git a/lib/plugins/autoFollow.js b/lib/plugins/autoFollow.js new file mode 100644 index 0000000..7680150 --- /dev/null +++ b/lib/plugins/autoFollow.js @@ -0,0 +1,50 @@ +/** + * When the new follower is notified, the bot follow him/her. + * + * @author yssk22@gmail.com + * + */ +var inspect = require('util').inspect; +exports.configure = function(options){ + if( !options ){ + options = {}; + } + this._autoFollow = options; +} + +var events = []; +events.push( + ['data', function(data){ + if(data.event == 'follow'){ + console.log(inspect(data)); + var bot = this; + bot.client.friendships.create({screen_name: data.source.screen_name}, function(err, result){ + if( err ){ + if( err.data && err.data.error ){ + bot.logger.warn(err.data.error); + }else{ + bot.logger.error(inspect(err)); + } + }else{ + bot.logger.info('AutoFollow: source = ' + data.source.screen_name + ')'); + } + }); + } + }] +); + +exports.events = events; + +// for manual testing +if(!module.parent){ + var TwBot = require('../twbot').TwBot; + var bot = new TwBot({ + consumerKey : process.argv[2], + consumerSecret : process.argv[3], + accessKey : process.argv[4], + accessSecret : process.argv[5] + }); + // debug pluin dumps all data event to stderr. + bot.loadPlugin(__filename, {autoUnfollow: true}); + bot.startUserStream(); +} diff --git a/lib/twitter/friendships.js b/lib/twitter/friendships.js index a49741a..a9d60cc 100644 --- a/lib/twitter/friendships.js +++ b/lib/twitter/friendships.js @@ -7,7 +7,7 @@ */ function uidFun(path){ - return function(uid, params, callback){ + return function(params, callback){ if( typeof params == "string" ){ params = { user_id: params @@ -19,13 +19,13 @@ function uidFun(path){ module.exports = { /** - * http://dev.twitter.com/doc/get/friendships/create + * http://dev.twitter.com/doc/post/friendships/create */ create: uidFun("/friendships/create.json"), /** - * http://dev.twitter.com/doc/get/friendships/delete + * http://dev.twitter.com/doc/post/friendships/destroy */ - delete: uidFun("/friendships/delete.json"), + destroy: uidFun("/friendships/destroy.json"), /** * http://dev.twitter.com/doc/get/friendships/show diff --git a/lib/twitter/index.js b/lib/twitter/index.js index 96fe42e..1c215e7 100644 --- a/lib/twitter/index.js +++ b/lib/twitter/index.js @@ -74,6 +74,7 @@ exports.createClient = function(consumerKey, consumerSecret, options){ } addModule('account'); + addModule('friendships'); addModule('tweets'); addModule('timeline'); addModule('list'); diff --git a/lib/util.js b/lib/util.js index 951b620..d1bd278 100644 --- a/lib/util.js +++ b/lib/util.js @@ -20,7 +20,11 @@ module.exports = { // for 4XX/5XX error var e = new Error(err.statusCode + ': ' + err.data); e.statusCode = err.statusCode; - e.data = err.data; + try{ + e.data = JSON.parse(err.data); + }catch(er){ + e.data = err.data; + } return e; }else{ // unknown error diff --git a/test/twitter/test_friedships.js b/test/twitter/test_friedships.js new file mode 100644 index 0000000..54944fa --- /dev/null +++ b/test/twitter/test_friedships.js @@ -0,0 +1,28 @@ +var assert = require('assert'); +var twitter = require('twitter'); + +try{ + var config = JSON.parse(require('fs').readFileSync(__dirname + '/../auth.json')); +}catch(e){ + console.error('Please prepare test/auth.json for testing'); + process.exit(1); +} + +var client = new twitter.createClient(config.consumerKey, config.consumerSecret, + { + accessKey: config.accessKey, + accessSecret: config.accessSecret + }).friendships; + +module.exports = { + "test createAndDelete": function(){ + client.create({screen_name: 'yssk22_dev2'}, function(err, data){ + assert.isUndefined(err); + assert.isNotNull(data); + client.destroy({screen_name: 'yssk22_dev2'}, function(err, data){ + assert.isUndefined(err); + assert.isNotNull(data); + }); + }); + } +} \ No newline at end of file diff --git a/test/twitter/test_tweets.js b/test/twitter/test_tweets.js index 8aaa378..87f3d10 100644 --- a/test/twitter/test_tweets.js +++ b/test/twitter/test_tweets.js @@ -28,7 +28,7 @@ module.exports = { client.update(text, function(err, data){ assert.isUndefined(err); assert.isNotNull(data); - client.show(data.id, function(err, data){ + client.show(data.id_str, function(err, data){ assert.isUndefined(err); assert.isNotNull(data); assert.eql(data.text, text);