Example usage for twitter4j IDs getNextCursor

List of usage examples for twitter4j IDs getNextCursor

Introduction

In this page you can find the example usage for twitter4j IDs getNextCursor.

Prototype

@Override
    long getNextCursor();

Source Link

Usage

From source file:GetFriendsIDs.java

License:Apache License

/**
 * Usage: java twitter4j.examples.friendsandfollowers.GetFriendsIDs [screen name]
 *
 * @param args message//from ww  w .j  a v a2s  .  c  o m
 */
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:aic2013.follower.TwitterDataAccess.java

public void forAllFriendIds(TwitterUser twitterUser, Processor<Long> processor) {
    long nextCursor = -1;
    IDs usersResponse = null;

    do {/*from w ww .  ja v  a  2  s. c  om*/
        assert usersResponse == null;

        while (usersResponse == null) {
            if (!open) {
                throw new ClosedException();
            }

            try {
                usersResponse = twitter.getFriendsIDs(twitterUser.getId(), nextCursor);
            } catch (TwitterException ex) {
                if (ex.exceededRateLimitation()) {
                    waitForReset(ex.getRateLimitStatus().getSecondsUntilReset() * 1000);
                } else if (ex.resourceNotFound()) {
                    //                        throw new RuntimeException("The user for which the friends were requested does not exist any more.", ex);
                    return;
                }

                // Retry when other errors like IO-related ones occur
            }
        }

        assert usersResponse != null;
        nextCursor = usersResponse.getNextCursor();

        for (long userId : usersResponse.getIDs()) {
            processor.process(userId);
        }
        usersResponse = null;
    } while (nextCursor > 0);
}

From source file:com.amandine.twitterpostforcoucou.Tweet.java

public void getFollowersInformation(String FollowersFor) {
    try {//from  w w  w.j  a va 2  s.  c o  m
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing followers's ids.");
        do {
            //                 if (0 < args.length) { 
            ids = twitter.getFollowersIDs(URLEncoder.encode(FollowersFor), cursor);
            //                 } else { 
            //                     ids = twitter.getFollowersIDs(cursor); 
            //                 } 
            for (long id : ids.getIDs()) {
                //System.out.println(id + " " + twitter.showUser(id).getScreenName());
                //twitter.showUser(id).getDescription language location name
                //twitter.showUser(id).getScreenName() this is rate limited at 180 every 15 minutes
                writeUsersTwitterIdToUserTable(Long.toString(id), "", FollowersFor);
            }
            try {
                Thread.sleep(1000 * 60);
            } catch (InterruptedException ex) {
                logger.log(Level.INFO, "Woke up", ex);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get followers' ids: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.buddycloud.friendfinder.provider.Twitter.java

License:Apache License

@Override
public ContactProfile getProfile(String accessToken, String accessTokenSecret) throws Exception {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(getProperties().getProperty("twitter.consumerKey"))
            .setOAuthConsumerSecret(getProperties().getProperty("twitter.consumerSecret"))
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret);
    TwitterFactory tf = new TwitterFactory(cb.build());
    twitter4j.Twitter twitter = tf.getInstance();

    Long myId = twitter.getId();/*from   www. ja  v a  2  s.  c o  m*/
    ContactProfile contactProfile = new ContactProfile(HashUtils.encodeSHA256(PROVIDER_NAME, myId.toString()));
    long nextCursor = -1;
    while (true) {
        IDs friendsIDs = twitter.getFollowersIDs(nextCursor);
        for (Long friendId : friendsIDs.getIDs()) {
            contactProfile.addFriendHash(HashUtils.encodeSHA256(PROVIDER_NAME, friendId.toString()));
        }
        if (!friendsIDs.hasNext()) {
            break;
        }
        nextCursor = friendsIDs.getNextCursor();
    }
    return contactProfile;
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.ProfileFragment.java

License:Apache License

public void getFollowers(final User user, final AsyncListView listView) {
    spinner.setVisibility(View.VISIBLE);
    canRefresh = false;/* w  w  w .ja  v a2  s  .c  om*/

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                try {
                    if (followingIds == null && user.getId() == settings.myId) {
                        long currCursor = -1;
                        IDs idObject;
                        int rep = 0;

                        do {
                            // gets 5000 ids at a time
                            idObject = twitter.getFriendsIDs(settings.myId, currCursor);

                            long[] lIds = idObject.getIDs();
                            followingIds = new ArrayList<Long>();
                            for (int i = 0; i < lIds.length; i++) {
                                followingIds.add(lIds[i]);
                            }

                            rep++;
                        } while ((currCursor = idObject.getNextCursor()) != 0 && rep < 3);
                    }
                } catch (Throwable t) {
                    followingIds = null;
                }

                PagableResponseList<User> friendsPaging = twitter.getFollowersList(user.getId(),
                        currentFollowers, 100);

                for (int i = 0; i < friendsPaging.size(); i++) {
                    followers.add(friendsPaging.get(i));
                }

                if (friendsPaging.size() > 17) {
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                currentFollowers = friendsPaging.getNextCursor();

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (followersAdapter == null) {
                            if (followingIds == null) {
                                // we will do a normal array adapter
                                followersAdapter = new PeopleArrayAdapter(context, followers);
                            } else {
                                followersAdapter = new FollowersArrayAdapter(context, followers, followingIds);
                            }
                            listView.setAdapter(followersAdapter);
                        } else {
                            followersAdapter.notifyDataSetChanged();
                        }

                        if (settings.roundContactImages) {
                            ImageUtils.loadSizedCircleImage(context, profilePicture,
                                    thisUser.getOriginalProfileImageURL(), mCache, 96);
                        } else {
                            ImageUtils.loadImage(context, profilePicture, thisUser.getOriginalProfileImageURL(),
                                    mCache);
                        }

                        String url = user.getProfileBannerURL();
                        ImageUtils.loadImage(context, background, url, mCache);

                        canRefresh = true;
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (user != null && user.isProtected()) {
                            Toast.makeText(context, getResources().getString(R.string.protected_account),
                                    Toast.LENGTH_SHORT).show();
                            if (settings.roundContactImages) {
                                ImageUtils.loadSizedCircleImage(context, profilePicture,
                                        thisUser.getOriginalProfileImageURL(), mCache, 96);
                            } else {
                                ImageUtils.loadImage(context, profilePicture, user.getOriginalProfileImageURL(),
                                        mCache);
                            }

                            String url = user.getProfileBannerURL();
                            ImageUtils.loadImage(context, background, url, mCache);
                        } else {
                            Toast.makeText(context, getResources().getString(R.string.error_loading_timeline),
                                    Toast.LENGTH_SHORT).show();
                        }
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;
                        hasMore = false;
                    }
                });
            }
        }
    }).start();
}

From source file:com.dhamacher.sentimentanalysis4tweets.twitterapi.TweetOperator.java

License:Apache License

public LinkedList<Long> getFollowerIds(String user) {
    try {//from   w ww .  ja  v  a 2s  .c  om
        long cursor = -1;
        LinkedList<Long> res = new LinkedList<Long>();
        IDs ids;
        do {
            ids = twitter.getFollowersIDs(user, cursor);
            for (long id : ids.getIDs()) {
                res.add(id);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        return res;

    } catch (TwitterException ex) {
        Logger.getLogger(TweetOperator.class.getName()).log(Level.SEVERE, null, ex);
        return new LinkedList<Long>();
    }
}

From source file:com.dhamacher.sentimentanalysis4tweets.twitterapi.TweetOperator.java

License:Apache License

public LinkedList<Long> getFollowerIds(long userId) {
    try {//  w  ww.jav  a  2  s . co m
        long cursor = -1;
        LinkedList<Long> res = new LinkedList<Long>();
        IDs ids;
        do {
            ids = twitter.getFollowersIDs(userId, cursor);
            for (long id : ids.getIDs()) {
                res.add(id);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        return res;

    } catch (TwitterException ex) {
        Logger.getLogger(TweetOperator.class.getName()).log(Level.SEVERE, null, ex);
        return new LinkedList<Long>();
    }
}

From source file:com.freshdigitable.udonroad.module.twitter.TwitterApi.java

License:Apache License

public Observable<IDs> getAllBlocksIDs() {
    return Observable.create(new Observable.OnSubscribe<IDs>() {
        @Override/* w w w.j  av  a 2 s .  c  o m*/
        public void call(Subscriber<? super IDs> subscriber) {
            try {
                IDs blocksIDs = null;
                while (blocksIDs == null || blocksIDs.hasNext()) {
                    final long cursor = blocksIDs == null ? -1 : blocksIDs.getNextCursor();
                    blocksIDs = twitter.getBlocksIDs(cursor);
                    subscriber.onNext(blocksIDs);
                }
                subscriber.onCompleted();
            } catch (TwitterException e) {
                subscriber.onError(e);
            }
        }
    }).subscribeOn(Schedulers.io());
}

From source file:com.freshdigitable.udonroad.module.twitter.TwitterApi.java

License:Apache License

public Observable<IDs> getAllMutesIDs() {
    return Observable.create(new Observable.OnSubscribe<IDs>() {
        @Override/*from   w  w w.  j ava2s .co  m*/
        public void call(Subscriber<? super IDs> subscriber) {
            try {
                IDs mutesIDs = null;
                while (mutesIDs == null || mutesIDs.hasNext()) {
                    final long cursor = mutesIDs == null ? -1 : mutesIDs.getNextCursor();
                    mutesIDs = twitter.getMutesIDs(cursor);
                    subscriber.onNext(mutesIDs);
                }
                subscriber.onCompleted();
            } catch (TwitterException e) {
                subscriber.onError(e);
            }
        }
    }).subscribeOn(Schedulers.io());
}

From source file:com.freshdigitable.udonroad.TimelineInstTestBase.java

License:Apache License

protected void setupConfig(User loginUser) throws Exception {
    final TwitterAPIConfiguration twitterAPIConfigMock = TwitterResponseMock.createTwitterAPIConfigMock();
    when(twitter.getAPIConfiguration()).thenReturn(twitterAPIConfigMock);

    final long userId = loginUser.getId();
    when(twitter.getId()).thenReturn(userId);
    when(twitter.showUser(userId)).thenReturn(loginUser);
    when(twitter.verifyCredentials()).thenReturn(loginUser);

    final IDs ignoringUserIDsMock = mock(IDs.class);
    when(ignoringUserIDsMock.getIDs()).thenReturn(new long[0]);
    when(ignoringUserIDsMock.getNextCursor()).thenReturn(0L);
    when(ignoringUserIDsMock.getPreviousCursor()).thenReturn(0L);
    when(twitter.getBlocksIDs()).thenReturn(ignoringUserIDsMock);
    when(twitter.getBlocksIDs(anyLong())).thenReturn(ignoringUserIDsMock);
    when(twitter.getMutesIDs(anyLong())).thenReturn(ignoringUserIDsMock);
}