Bulk Follow Back Twitter - Java Social Media

Java examples for Social Media:Twitter

Description

Bulk Follow Back Twitter

Demo Code

import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

public class BulkFollowBack {
    public static void main(String[] args) throws TwitterException {
        Twitter twitter = new TwitterFactory().getInstance();
        String consumerKey = "";
        String consumerSecret = "";
        String accessToken = "";
        String accessTokenSecret = "";

        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        twitter.setOAuthAccessToken(new AccessToken(accessToken,
                accessTokenSecret));//from   w w w . ja v a 2s.c  o  m

        IDs followers = twitter.getFollowersIDs(-1);
        long[] followersIDs = followers.getIDs();

        IDs friends = twitter.getFriendsIDs(-1);
        long[] friendsIDs = friends.getIDs();

        for (long follower : followersIDs) {
            boolean isFollowing = false;
            for (long friend : friendsIDs) {
                if (friend == follower) {
                    isFollowing = true;
                    break;
                }
            }
            if (isFollowing) {
                System.out.println("id:" + follower+ "");
            } else {
                IDs pendingFollowRequests = twitter
                        .getOutgoingFriendships(-1);
                long[] pendingFollowRequestIDs = pendingFollowRequests
                        .getIDs();
                boolean followPending = false;
                for (long pendingFollow : pendingFollowRequestIDs) {
                    if (pendingFollow == follower) {
                        followPending = true;
                    }
                }
                if (followPending) {
                    System.out.println("id:" + follower + "");
                } else {
                    System.out.println("id:" + follower+ "");
                    twitter.createFriendship(follower);
                }
            }
        }
    }
}

Related Tutorials