demo.UserFollowers.java Source code

Java tutorial

Introduction

Here is the source code for demo.UserFollowers.java

Source

/*
 * Copyright 2007 Yusuke Yamamoto
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.IDs;
import twitter4j.RateLimitStatus;
import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

/*Followersids???*/
public class UserFollowers {
    //OAUTH??
    final static String CONSUMER_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String CONSUMER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String ACCESS_TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    //?
    static List<Long> followers(Twitter twitter, String screenName) {
        List<Long> m_FollowersList = new ArrayList<Long>();

        long cursor = -1;
        //int count = 0;
        while (true) {
            IDs ids = null;
            try {
                //IDs ids = twitter.getFollowersIDs(user.getId(), cursor);
                ids = twitter.getFollowersIDs(screenName, cursor);
            } catch (TwitterException twitterException) {
                // Rate Limit ?????????
                // (memo) status code ?????????

                RateLimitStatus rateLimit = twitterException.getRateLimitStatus();
                int secondsUntilReset = rateLimit.getSecondsUntilReset();
                System.err.println("please wait for " + secondsUntilReset + " seconds");
                System.err.println("Reset Time : " + rateLimit.getResetTimeInSeconds());

                //() 120?getSecondsUntilReset() ?????????????
                // long waitTime = (long)(secondsUntilReset + 120) * 1000;
                long waitTime = (long) (300 * 1000); // 300
                try {
                    Thread.sleep(waitTime);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                continue;
            } catch (Exception e) {
                e.printStackTrace();
            }

            long[] idArray = ids.getIDs();
            for (int i = 0; i < idArray.length; i++) {
                //System.out.println("["+(++count)+"]" + idArray[i]);
                m_FollowersList.add(new Long(idArray[i]));
            }

            if (ids.hasNext()) {
                cursor = ids.getNextCursor();
            } else {
                break;
            }
        }
        return m_FollowersList;
    }

    public static void main(String[] args) throws TwitterException, IOException {
        //?
        Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
                .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
        Twitter tw = new TwitterFactory(configuration).getInstance();
        //String screenName = "masason";
        String screenName = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(
                "??ScreenName???????!! ex)masason : ");
        screenName = br.readLine();

        //?
        List<Long> followersList = followers(tw, screenName);
        for (int i = 0; i < followersList.size(); i++) {
            //???idscreenName?
            User user = tw.showUser(followersList.get(i));
            System.out.println("[" + (i + 1) + "]" + "ScreenName : " + "[" + user.getScreenName() + "]  "
                    + "UserIds : " + followersList.get(i));
        }
    }
}