-
-
Save sjehutch/60cbe72088b85c9dfac7f62b5ed7e63b to your computer and use it in GitHub Desktop.
Twitter bot using node.js and twit module to retweet interesting info of node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Bot | |
// class for performing various twitter actions | |
// | |
var Twit = require('../lib/twitter'); | |
var Bot = module.exports = function(config) { | |
this.twit = new Twit(config); | |
}; | |
// | |
// post a tweet | |
// | |
Bot.prototype.tweet = function (status, callback) { | |
if(typeof status !== 'string') { | |
return callback(new Error('tweet must be of type String')); | |
} else if(status.length > 140) { | |
return callback(new Error('tweet is too long: ' + status.length)); | |
} | |
this.twit.post('statuses/update', { status: status }, callback); | |
}; | |
// | |
// choose a random friend of one of your followers, and follow that user | |
// | |
Bot.prototype.mingle = function (callback) { | |
var self = this; | |
this.twit.get('followers/ids', function(err, reply) { | |
if(err) { return callback(err); } | |
var followers = reply.ids | |
, randFollower = randIndex(followers); | |
self.twit.get('friends/ids', { user_id: randFollower }, function(err, reply) { | |
if(err) { return callback(err); } | |
var friends = reply.ids | |
, target = randIndex(friends); | |
self.twit.post('friendships/create', { id: target }, callback); | |
}) | |
}) | |
}; | |
// | |
// prune your followers list; unfollow a friend that hasn't followed you back | |
// | |
Bot.prototype.prune = function (callback) { | |
var self = this; | |
this.twit.get('followers/ids', function(err, reply) { | |
if(err) return callback(err); | |
var followers = reply.ids; | |
self.twit.get('friends/ids', function(err, reply) { | |
if(err) return callback(err); | |
var friends = reply.ids | |
, pruned = false; | |
while(!pruned) { | |
var target = randIndex(friends); | |
if(!~followers.indexOf(target)) { | |
pruned = true; | |
self.twit.post('friendships/destroy', { id: target }, callback); | |
} | |
} | |
}); | |
}); | |
}; | |
// | |
// search follow | |
// | |
Bot.prototype.searchFollow = function (params, callback) { | |
var self = this; | |
self.twit.get('search/tweets', params, function (err, reply) { | |
if(err) return callback(err); | |
var tweets = reply.statuses; | |
var target = randIndex(tweets).user.id_str; | |
self.twit.post('friendships/create', { id: target }, callback); | |
}); | |
}; | |
// | |
// retweet | |
// | |
Bot.prototype.retweet = function (params, callback) { | |
var self = this; | |
self.twit.get('search/tweets', params, function (err, reply) { | |
if(err) return callback(err); | |
var tweets = reply.statuses; | |
var randomTweet = randIndex(tweets); | |
self.twit.post('statuses/retweet/:id', { id: randomTweet.id_str }, callback); | |
}); | |
}; | |
// | |
// favorite a tweet | |
// | |
Bot.prototype.favorite = function (params, callback) { | |
var self = this; | |
self.twit.get('search/tweets', params, function (err, reply) { | |
if(err) return callback(err); | |
var tweets = reply.statuses; | |
var randomTweet = randIndex(tweets); | |
self.twit.post('favorites/create', { id: randomTweet.id_str }, callback); | |
}); | |
}; | |
function randIndex (arr) { | |
var index = Math.floor(arr.length*Math.random()); | |
return arr[index]; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RTD2 - Twitter bot that tweets about the most popular github.com news | |
// Also makes new friends and prunes its followings. | |
// | |
var Bot = require("./bot") | |
, config1 = require("../config1"); | |
var bot = new Bot(config1); | |
console.log('RTD2: Running.'); | |
//get date string for today's date (e.g. 2011-01-01) | |
function datestring () { | |
var d = new Date(Date.now() - 5*60*60*1000); //est timezone | |
return d.getUTCFullYear() + "-" + | |
(d.getUTCMonth() + 1) + "-" + | |
d.getDate(); | |
}; | |
setInterval(function() { | |
bot.twit.get("followers/ids", function(err, reply) { | |
if(err) return handleError(err) | |
console.log("\n# followers:" + reply.ids.length.toString()); | |
}); | |
var rand = Math.random(); | |
if(rand <= .10) { // do a targeted follow | |
var params = { | |
q: "nodejs" | |
, since: datestring() | |
, result_type: "mixed" | |
}; | |
bot.searchFollow(params, function(err, reply) { | |
if(err) return handleError(err); | |
var name = reply.screen_name; | |
console.log("\nSearchFollow: followed @" + name); | |
}); | |
} else if(rand <= .90) { // retweet | |
var params = { | |
q: "nodejs" | |
, since: datestring() | |
, result_type: "mixed" | |
}; | |
bot.retweet(params, function(err, reply) { | |
if(err) return handleError(err); | |
console.log("\nRetweet: retweeted response: " + reply.id); | |
}); | |
} else if(rand <= .95) { // favorite | |
var params = { | |
q: "nodejs" | |
, since: datestring() | |
, result_type: "mixed" | |
}; | |
bot.favorite(params, function(err, reply) { | |
if(err) return handleError(err); | |
console.log("\nFavorite: favorited response: " + reply.id); | |
}); | |
} else { // prune a friend | |
bot.prune(function(err, reply) { | |
if(err) return handleError(err); | |
var name = reply.screen_name | |
console.log("\nPrune: unfollowed @"+ name); | |
}); | |
} | |
}, 12000); | |
function handleError(err) { | |
console.error("response status:", err.statusCode); | |
console.error("data:", err.data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment