Twitter4Jで自動フォロー返しプログラムを作ってみた


Twitter4JTwitterAPIJava使使
import java.util.ArrayList;
import java.util.List;

import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;


public class AutoRefollow {
 protected static Twitter twitter;
 public static void main(String[] args) throws Exception {
  System.out.println("処理開始");
  twitter = new Twitter("yoheiMune", "56909885");
  IDs frends = getFrendIds();
  IDs follower = getFollowerIds();
  List<Integer> notFollowIdList = calcNotFollow(follower, frends);
  doFollow(notFollowIdList);
  System.out.println("処理終了");
 }

 protected static void doFollow(List<Integer> notFollowIdList) throws TwitterException {
  for (Integer userId : notFollowIdList) {
   User user = twitter.createFriendship(userId);
   if(user == null) {
    throw new TwitterException("フォローに失敗しました。対象ID:" + userId);
   }
   System.out.println("フォローしました。対象:" + user.getName());
  }
 }

 protected static List<Integer> calcNotFollow(IDs follower, IDs frends) {
  List<Integer> returnValue = new ArrayList<Integer>();
  for(int id : follower.getIDs()) {
   if(! contains(frends, id)) {
    returnValue.add(Integer.valueOf(id));
   }
  }
  return returnValue;
 }

 private static boolean contains(IDs frends, intid) {
  
  for(int frendId : frends.getIDs()) {
   if(frendId == id) {
    return true;
   }
  }
  return false;
 }

 protected static IDs getFollowerIds() throws TwitterException {
  return twitter.getFollowersIDs();
 }

 protected static IDs getFrendIds() throws TwitterException {
  return twitter.getFriendsIDs();
 }

}


http://twitter4j.org/ja/index.html
http://twitter4j.org/ja/javadoc/twitter4j/Twitter.html