Skip to content
This repository has been archived by the owner on Sep 28, 2018. It is now read-only.

Commit

Permalink
autoFollow plugin, improve error handling, friendship bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
yssk22 committed Feb 27, 2011
1 parent c6726ce commit 87a1198
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
50 changes: 50 additions & 0 deletions 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();
}
8 changes: 4 additions & 4 deletions lib/twitter/friendships.js
Expand Up @@ -7,7 +7,7 @@
*/

function uidFun(path){
return function(uid, params, callback){
return function(params, callback){
if( typeof params == "string" ){
params = {
user_id: params
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/index.js
Expand Up @@ -74,6 +74,7 @@ exports.createClient = function(consumerKey, consumerSecret, options){
}

addModule('account');
addModule('friendships');
addModule('tweets');
addModule('timeline');
addModule('list');
Expand Down
6 changes: 5 additions & 1 deletion lib/util.js
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions 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);
});
});
}
}
2 changes: 1 addition & 1 deletion test/twitter/test_tweets.js
Expand Up @@ -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);
Expand Down

0 comments on commit 87a1198

Please sign in to comment.