user2008957
user2008957

Reputation: 11

twitter4j not recognizing setRpp or Tweet class

I'm trying to set up a twitter search via processing, using twitter4J. I have the most recent version of processing (2.0) and the most recent version of twitter4j (3.0.3). I'm experiencing an error when I run the code that's pasted below (except I xxx'ed out the twitter specific strings): "The function setRpp(int) does not exist"

I found a similar inquiry in a Processing forum: https://forum.processing.org/topic/twitter4j-for-a-noob

The advice for this thread is: Trying to figure out twitter4J as well, and have encountered similar errors.

You can either switch to an earlier version of twitter4J -- v2.2.5 -- or you'll have to figure out the changes made from twitter4j 2.x.x to 3.x.x.

Try changing "setRpp(1)" to "count(1)".

"Tweet" might not work either -- you can try changing those to "Status".


When I switch setRpp > count and Tweet > Status, processing can't seem to find the function, "getFromUser" Error: The function getFromUser() does not exist

I assume that getFromUser is a function of the Tweet class, and because I changed the class from Tweet to Status, processing can no longer find that function.

Has anyone one else experienced these issues? Please let me know, thank you!

a

My code:

/*

Just a simple Processing and Twitter thingy majiggy

RobotGrrl.com

Code licensed under:
CC-BY

*/

// First step is to register your Twitter application at dev.twitter.com
// Once registered, you will have the info for the OAuth tokens
// You can get the Access token info by clicking on the button on the
// right on your twitter app's page
// Good luck, and have fun!

// This is where you enter your Oauth info
static String OAuthConsumerKey = "xxx";
static String OAuthConsumerSecret = "xxx";

// This is where you enter your Access Token info
static String AccessToken = "xxx";
static String AccessTokenSecret = "xxxx;

// Just some random variables kicking around
String myTimeline;
java.util.List statuses = null;
User[] friends;
Twitter twitter = new TwitterFactory().getInstance();
RequestToken requestToken;
String[] theSearchTweets = new String[11];


void setup() {

  size(100,100);
  background(0);

  connectTwitter();
  sendTweet("Hey from Simple Processing woop woop #RobotGrrl");

}


void draw() {

  background(0);

}


// Initial connection
void connectTwitter() {

  twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
  AccessToken accessToken = loadAccessToken();
  twitter.setOAuthAccessToken(accessToken);

}

// Sending a tweet
void sendTweet(String t) {

  try {
    Status status = twitter.updateStatus(t);
    println("Successfully updated the status to [" + status.getText() + "].");
  } catch(TwitterException e) { 
    println("Send tweet: " + e + " Status code: " + e.getStatusCode());
  }

}


// Loading up the access token
private static AccessToken loadAccessToken(){
  return new AccessToken(AccessToken, AccessTokenSecret);
}


// Get your tweets
void getTimeline() {

  try {
    statuses = twitter.getUserTimeline(); 
  } catch(TwitterException e) { 
    println("Get timeline: " + e + " Status code: " + e.getStatusCode());
  }

  for(int i=0; i<statuses.size(); i++) {
    Status status = (Status)statuses.get(i);
    println(status.getUser().getName() + ": " + status.getText());
  }

}


// Search for tweets
void getSearchTweets() {

  String queryStr = "@RobotGrrl";

  try {
    Query query = new Query(queryStr);    
    query.setRpp(10); // Get 10 of the 100 search results  
    QueryResult result = twitter.search(query);    
    ArrayList tweets = (ArrayList) result.getTweets();    

    for (int i=0; i<tweets.size(); i++) {   
      Tweet t = (Tweet)tweets.get(i);   
      String user = t.getFromUser();
      String msg = t.getText();
      Date d = t.getCreatedAt();    
      theSearchTweets[i] = msg.substring(queryStr.length()+1);

      println(theSearchTweets[i]);
    }

  } catch (TwitterException e) {    
    println("Search tweets: " + e);  
  }

}

Upvotes: 1

Views: 2954

Answers (2)

Graham P Heath
Graham P Heath

Reputation: 7389

It sounds like you have the new twitter4j library. No problem, I'm using the 3.0.3 twitter4j, and it seems things have changed somewhat. That is what I've got working:

TwitterFactory twitterFactory;  

A new public TwitterFactory twitterFactory, and in connectTwitter(), I basically swapped what you had out for this.

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(OAuthConsumerKey);
cb.setOAuthConsumerSecret(OAuthConsumerSecret);
cb.setOAuthAccessToken(AccessToken);
cb.setOAuthAccessTokenSecret(AccessTokenSecret);
twitterFactory = new TwitterFactory(cb.build());
twitter = twitterFactory.getInstance();

To create my Twitter twitter object. I am pretty sure thats all that I've changed, but incase I've missed something, here is a gist https://gist.github.com/loadedsith/5113946

Nice job on your header.

Upvotes: 2

hugohil
hugohil

Reputation: 43

I had this problem too, getFromeUser() is no longer a Status method. Now you need to create a User class, and then get its name with getName(). I did like this:

Status t=(Status) tweets.get(i);
User u=(User) t.getUser();
String user=u.getName();

I hope this helped you.

H.

Upvotes: 1

Related Questions