twitterrest.Followersids.java Source code

Java tutorial

Introduction

Here is the source code for twitterrest.Followersids.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 twitterrest;

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;

import java.util.List;
import java.util.ArrayList;

/*????*/
public class Followersids {
    //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";

    //ID?
    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 ?????????
                // () 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 {

        String ScreenName = "anondroid5";//??

        // ?
        Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
                .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
        Twitter twitter = new TwitterFactory(configuration).getInstance();

        List<Long> followersList = followers(twitter, ScreenName);//?
        System.out.println(ScreenName + ":FolloweID List");
        for (int i = 0; i < followersList.size(); i++) {
            System.out.println("[" + (i + 1) + "]" + followersList.get(i));
            //?
            User user = twitter.showUser(followersList.get(i));
            System.out.println("User ID : " + user.getId());//UserID
            System.out.println("ScreenName : " + user.getScreenName());//ScreenName
            System.out.println("User's Name : " + user.getName());//UserName
            System.out.println("Number of Followers : " + user.getFollowersCount());//Number of Followers
            System.out.println("Number of Friends : " + user.getFriendsCount());//Number of Friends
            System.out.println("Language : " + user.getLang());//Language
        }
    }
}