3

I'm trying to make a sketch that gets the last 100 tweets from a specific user's twitter timeline. I'm using Twitter4j and the sketch works fine but I've learned that Twitter4J by default limits the timeline results to 20.

I've seen this page to learn about getting more than 100 when using Queries, but it seems to be working differently when trying to get a specific user's timeline. How to retrieve more than 100 results using Twitter4j

Thanks for looking!

Here is my sketch:

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;


ConfigurationBuilder cb = new ConfigurationBuilder();

cb.setOAuthConsumerKey("XXXXXX");
cb.setOAuthConsumerSecret("XXXXXX");
cb.setOAuthAccessToken("XXXXXX");
cb.setOAuthAccessTokenSecret("XXXXXX");

java.util.List statuses = null;

Twitter twitter = new TwitterFactory(cb.build()).getInstance();

String userName ="XXXXXX";
int numTweets = 100;
String[] twArray = new String[numTweets];



  try {
    statuses = twitter.getUserTimeline(userName);
  }
  catch(TwitterException e) {
  }

  for (int i=0; i<statuses.size(); i++) {
    Status status = (Status)statuses.get(i);

    //println(status.getUser().getName() + ": " + status.getText());
    twArray[i] = status.getUser().getName() + ": " + status.getText();

  }


println(twArray);
Community
  • 1
  • 1
phwoomp
  • 69
  • 1
  • 8
  • Did you actually try adapting the other post that you linked or did you just stay at "seems to be working differently"? Specifically did you try using query.setMaxId(); ? – Petros Koutsolampros Oct 07 '13 at 12:38
  • I tried adapting it but I don't understand what to replace in the code to make it about the user timeline i'm trying to request instead of a query. – phwoomp Oct 07 '13 at 12:44
  • Great! Show us your adaptations, your train of thought, things you tried! – Petros Koutsolampros Oct 07 '13 at 12:53
  • I apoligize for not being able to show the entire sketch, there is not enough room. I changed the line from your sketch here:http://stackoverflow.com/questions/18800610/how-to-retrieve-more-than-100-results-using-twitter4j FROM: `QueryResult result = twitter.search(query);` TO: `QueryResult result = twitter.getUserTimeline(userName);` But I understand that Twitter4J treats queries differently from timelines, I just don't know how to properly describe in the sketch that I want the tweets from a timeline instead of a query. – phwoomp Oct 07 '13 at 13:09
  • The console then tells me that: The Method getUserTimeline(String) in the type TimelinesResources is not applicable for the arguments (Query) So I guess the datatype Query comes from the contributed library but the results from the timeline request need to be treated differently than a query for a search term. – phwoomp Oct 07 '13 at 13:17
  • From http://twitter4j.org/javadoc/twitter4j/api/TimelinesResources.html#getUserTimeline() what getUserTimeline() gives you is a list of Statuses. In the other question what we do is we pull a query from twitter(QueryResult result = twitter.search(query);) and pull the tweets from that query(result.getTweets();). Here the tweets are given directly from you so you need to replace result.getTweets(); and completely get rid of the QueryResult – Petros Koutsolampros Oct 07 '13 at 14:05
  • Thanks for the reply. I still just don't understand where to put `getUserTimeline()` and what to put instead of `(result.getTweets();)` Should I remove all the query stuff? Or just the QueryResult part? All I want is to load 100 of the last tweets from a specific user into an array so I can use it in processing. – phwoomp Oct 07 '13 at 14:58

2 Answers2

6

I know the question is old, but the accepted answer is not the best solution for this, below is a more elegant piece of code to solve this problem.

...

Paging p = new Paging();
p.setCount(100);
return twitter.getUserTimeline("screenName",p);
jucajl
  • 1,195
  • 3
  • 12
  • 29
3

How about something like this?

Paging pg = new Paging();
String userName = "uzr";
void setup() {

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("xxxx");
  cb.setOAuthConsumerSecret("xxxx");
  cb.setOAuthAccessToken("xxxx");
  cb.setOAuthAccessTokenSecret("xxxx");

  Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  int numberOfTweets = 100;
  long lastID = Long.MAX_VALUE;
  ArrayList<Status> tweets = new ArrayList<Status>();
  while (tweets.size () < numberOfTweets) {
    try {
      tweets.addAll(twitter.getUserTimeline(userName,pg));
      println("Gathered " + tweets.size() + " tweets");
      for (Status t: tweets) 
        if(t.getId() < lastID) lastID = t.getId();
    }
    catch (TwitterException te) {
      println("Couldn't connect: " + te);
    }; 
    pg.setMaxId(lastID-1);
  }
}
Petros Koutsolampros
  • 2,790
  • 1
  • 14
  • 20
  • Thanks very much for the help Petros. I also found an easy way to achieve my goal here: http://forum.processing.org/two/discussion/comment/369#Comment_369 – phwoomp Oct 07 '13 at 19:31