Grab timelines from several users at once?

android
twitter4j

#1

I’m currently using Twitter4J to grab the timeline from a single user:

    AsyncTwitterFactory factory = new AsyncTwitterFactory(cb.build());
    AsyncTwitter asyncTwitter = factory.getInstance();
    asyncTwitter.addListener(new TwitterAdapter() {

        @Override
        public void gotUserTimeline(ResponseList<Status> statuses) {
            for (Status status : statuses) {
                Log.d("twitter", "twitter: " + status.getText() + status.getUser().getName());
            }
            super.gotUserTimeline(statuses);
        }

        @Override
        public void onException(TwitterException te, TwitterMethod method) {
            te.printStackTrace();
            super.onException(te, method);
        }
    });

    asyncTwitter.getUserTimeline("twitterusernamehere");

What I need to do is grab the timelines from several users at once. I could run the previous call several times for different users, but that seems like bad practice and im hoping theres another way. I’m currently using Twitter4J, but am open to switching to the Fabric kit or something else if its easier to do it with that.


#2

I’ve tried this before and ran into some problems with rate limits: It’s relatively straight forward to create a thread pool and send off concurrent requests to getUserTimeline()… but that’s a bad way to do it because you’re making lots of concurrent calls the same endpoint, and the rate limit becomes very unpredictable.

What worked for me for a while was:
PriorityQueues: Making 1 call to the API endpoint at a time, but request 1 “page” for every user at a time, only request the next page after you’ve got the first page for everyone. (since_id & max_id parameters really help - Twitter4J sets those with the Paging object)

An even better way to do it: Make a list of the users, and use https://dev.twitter.com/rest/reference/get/lists/statuses eg: https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/list/GetUserListStatuses.java
Works great if the set of users you’re interested in won’t drastically change.

If that fails, the streaming API with follow parameters is definitely the way to go: https://dev.twitter.com/streaming/reference/post/statuses/filter


#3

Thanks, both the Twitter lists and streaming api solutions sound like they would work. Looking at Twitter4J, it looks like it has a filter() method that corresponds to the part of the streaming api you linked. The list of users doesnt really need to change, though i guess it would be a cool feature to have selectable users. Ill give these solutions a try.