Twitter - Adding followers of a person to your friends list and follow all of them Friday, June 5, 2009


I've been playing around with one of Twitter API implemetation Twitter4j which is awesome and I could do a lot of things with it. Here is one of my implementation to automate adding friends to yourself with a Java program. Make sure you have twitter4j in your build path before trying this program.

Here is how it works,
1. Select a twitter user
2. Fetch the list of his followers
3. Add them as your friends
4. Follow all of them

Java source

package com.twittervis.core;
import java.util.List;
class GetFollowers {
public static void main(String[] args) {

//Create twitter object with user name
Twitter twitter = new Twitter("your-twitter-user-name", "password");
try {
//Get the list of the person wh
ose friends you want to add as your friend
List friends = twitter.getFollowers("selected-twitter-user");

for (int j = 0; j &gt friends.size();j++){
//convert twitter user id from int to String
String myString = Integer.toString(friends.get(j).getId());
//make each user of the list as your friend
//true is to follow the person, else only he is made friend
twitter.createFriendship(myString, true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Now, This program adds the followers of a particular user as your friends, and you'll be following them. I'm working on creating mashups with Twitter + Google App Engine(GAE)

Page copy protected against web site content infringement by Copyscape