Example usage for twitter4j Twitter getFriendsIDs

List of usage examples for twitter4j Twitter getFriendsIDs

Introduction

In this page you can find the example usage for twitter4j Twitter getFriendsIDs.

Prototype

IDs getFriendsIDs(long cursor) throws TwitterException;

Source Link

Document

Returns an array of numeric IDs for every user the authenticating user is following.

Usage

From source file:GetFriendsIDs.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendsandfollowers.GetFriendsIDs [screen name]
 *
 * @param args message/*from w w w . j a  v a  2s  . com*/
 */
public ArrayList<Long> getFriendsId() {
    ArrayList<Long> friendsid = new ArrayList<Long>();
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing following ids.");

        do {

            ids = twitter.getFriendsIDs(cursor);

            for (long id : ids.getIDs()) {

                friendsid.add(id);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        System.out.println("done");
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get friends' ids: " + te.getMessage());

    }

    return friendsid;
}

From source file:com.appspot.bitlyminous.standalone.TwitterExample.java

License:Apache License

/**
 * The main method./*www . j  a  va 2s .  co m*/
 * 
 * @param args the arguments
 * 
 * @throws Exception the exception
 */
public static void main(String[] args) throws Exception {
    // The factory instance is re-useable and thread safe.
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer("671O1zUicFBCYHHYVp0XA", "cP7WvfAtxXGSaf1Sz0ksZtWGcENosFcRr46WR7a9jU");
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Enter the PIN(if available) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try {
            if (pin.length() > 0) {
                accessToken = twitter.getOAuthAccessToken(requestToken, pin);
            } else {
                accessToken = twitter.getOAuthAccessToken();
            }
        } catch (TwitterException te) {
            if (401 == te.getStatusCode()) {
                System.out.println("Unable to get the access token.");
            } else {
                te.printStackTrace();
            }
        }
    }
    // persist to the accessToken for future reference.
    System.out.println("Screen name:" + accessToken.getScreenName());
    System.out.println("Token:" + accessToken.getToken());
    System.out.println("Secret:" + accessToken.getTokenSecret());

    IDs friendsIDs = twitter.getFriendsIDs("gapinginfinity");
    System.out.println(Arrays.toString(friendsIDs.getIDs()));

    ResponseList<Status> favorites = twitter.getFavorites("gapinginfinity");
    System.out.println(favorites.size());
    System.exit(0);
}

From source file:com.eventattend.portal.bl.TwitterBL.java

License:Open Source License

private List getFriendsProfile(Twitter twitterClient, String screenName, TwitterDTO twitterDTO)
        throws BaseAppException {
    //ProfileDTO profileDTO = null;
    User profile = null;//  w  w  w  .ja va  2 s. co m
    List friendsList = null;

    try {
        friendsList = new ArrayList();

        IDs friendsIds = twitterClient.getFriendsIDs(screenName);
        int i = 0;
        if (friendsIds != null) {
            for (int userId : friendsIds.getIDs()) {
                //if(i<5){
                profile = twitterClient.showUser(userId);

                twitterDTO = fetchProfile(profile, twitterDTO);
                friendsList.add(twitterDTO);
            }
            //i++;

            //}
        }

    } catch (TwitterException e) {

        processTwitterException(e);
    }

    return friendsList;
}

From source file:com.gmail.altakey.joanne.service.TwitterAuthService.java

License:Apache License

public static void updateRelations(final Context context, final AccessToken token) {
    final SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, MODE_PRIVATE);
    try {//from ww w.  ja va2  s. co  m
        final Twitter twitter = TwitterFactory.getSingleton();
        twitter.setOAuthAccessToken(token);

        final Set<Long> friends = new HashSet<Long>();
        for (IDs ids = twitter.getFriendsIDs(-1);; ids = twitter.getFriendsIDs(ids.getNextCursor())) {
            for (Long id : ids.getIDs()) {
                friends.add(id);
            }
            if (!ids.hasNext()) {
                break;
            }
        }

        final Set<Long> followers = new HashSet<Long>();
        for (IDs ids = twitter.getFollowersIDs(-1);; ids = twitter.getFollowersIDs(ids.getNextCursor())) {
            for (Long id : ids.getIDs()) {
                followers.add(id);
            }
            if (!ids.hasNext()) {
                break;
            }
        }

        prefs.edit().putString(KEY_FRIENDS, new IdListCoder().encode(friends))
                .putString(KEY_FOLLOWERS, new IdListCoder().encode(followers)).commit();
        UserRelation.notifyRelationsChanged();
        Log.d(TAG, String.format("got %d friends", friends.size()));
        Log.d(TAG, String.format("got %d followers", followers.size()));
    } catch (TwitterException e) {
        Log.w(TAG, "cannot get follower list", e);
    }
}

From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationGetFriendsIDs.java

License:Open Source License

@Override
public void execute(SocialAdapterAccount account) throws SocialAdapterException {
    try {//from   w w  w  .  j  a v  a  2  s .  c om
        Twitter twitter = (Twitter) account.getProxyObject();
        ids = twitter.getFriendsIDs(Long.parseLong(cursor));
    } catch (NumberFormatException exc) {
        logger.error("Call to TwitterOperationGetFriendsIDs failed. Check cursor[" + cursor + "] format.", exc);
        throw new SocialAdapterException(
                "Call to TwitterOperationGetFriendsIDs failed. Check cursor[" + cursor + "] format.", exc);
    } catch (TwitterException exc) {
        logger.error("Call to TwitterOperationGetFriendsIDs cursor[" + cursor + "] failed.", exc);
        throw new SocialAdapterException("Call to TwitterOperationGetFriendsIDs cursor[" + cursor + "] failed.",
                exc);
    }
}

From source file:twitterlab.TwitterLab.java

/**
 * @param args the command line arguments
 *///w  w w  . ja va2  s  .c  om
public static void main(String[] args) {
    // TODO code application logic here
    Users = new ArrayList();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("yBSAPlE6wiAQU6CyIiXHSapwN");
    cb.setOAuthConsumerSecret("3CjbEkYyIZvNZ07KYpC55v9vpqxoNmMs1IkC48Wqr5tKXjUnZk");
    cb.setOAuthAccessToken("858371720-0RASIMJeaMbWr2YPmCPyh5OgCP6Kesgz0WvdsLz7");
    cb.setOAuthAccessTokenSecret("cfi5JDGLTUOKHNj7yFIdivg0Hr1bCml3VlQXj5XCOyoFr");

    try {
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter tw = tf.getInstance();
        IDs ids = tw.getFriendsIDs(-1);
        long[] ID;
        if (ids.getIDs().length > 10) {
            ID = new long[10];
            System.arraycopy(ids.getIDs(), 0, ID, 0, 10);
        } else {
            ID = ids.getIDs();
        }
        ResponseList<User> Friends = tw.lookupUsers(ID);
        User[] A = new User[10];
        if (Friends.toArray().length > 0) {
            Users.add(new ArrayList(Arrays.asList(Friends.toArray(A))));
        }
        for (int i = 0; i < ID.length; i++) {
            IDs idsFriend = tw.getFriendsIDs(ID[i], -1);
            long[] IDfriend;
            if (idsFriend.getIDs().length > 10) {
                IDfriend = new long[10];
                System.arraycopy(idsFriend.getIDs(), 0, IDfriend, 0, 10);
            } else {
                IDfriend = idsFriend.getIDs();
            }
            ResponseList<User> FriendsFriend = tw.lookupUsers(IDfriend);
            A = new User[10];
            if (FriendsFriend.toArray().length > 0) {
                Users.add(new ArrayList(Arrays.asList(FriendsFriend.toArray(A))));
            }
            //System.out.println("Sleep?");
            TimeUnit.SECONDS.sleep(60);
        }

        for (int i = 0; i < Users.size(); i++) {
            for (int j = 0; j < Users.get(0).size(); j++) {
                System.out.println(Users.get(i).get(j).getScreenName());
            }
        }

    } catch (Exception e) {
        // Thread.currentThread().interrupt();
        e.printStackTrace();
    }

}