get the timeline from a user with twitter4j

edited October 2013 in Library Questions

Hi folks, I'm starting to play with processing and twitter API, which I found amazing, but a bit tricky... I get tweets from a search from a key word , and I can post tweets with my user too... But so far, I can't get the timeline from a user, from my user for example (@urco82). I have been looking for some tutorial or references in this forum, but I didn't find anything that resolve my problem. I found a couple posts about get more than 20 tweets, but I have copied the code and doesn't work for me yet... Some suggestion or guide to take the right way?

Many many thanks in advance. The forum is amazing.

Answers

  • edited October 2013

    Hi _VK! many thanks!! I get the timeline succesfully now! At the beginning I was getting my tweets, no my home timeline tweets... I changed the method twitter.getUserTimeline() for getHomeTimeline() to get the right tweets.

    The another question is about List statuses declaration, and how is used in this bucle " for (Status status : result.getTweets()) {}. I don't understand the line Status status:result.getTweets...:(

    Thanks for your suggestions again!

    import twitter4j.conf.*;
    import twitter4j.*;
    import twitter4j.auth.*;
    import twitter4j.api.*;
    import java.util.*;
    
    
    List<Status>statuses = null;
    
    TwitterFactory twitterFactory;
    Twitter twitter;
    
    void setup() {     
      size(800, 600);    
      background(0); 
    
      connectTwitter(); 
    
    }  
    
    void draw() {     
      background(0);
      getSearchTweets(); 
      //getTimeline();
      delay(250);  
    
    }  
    
    
    
    
    // Initial connection
    void connectTwitter() {  
    
      ConfigurationBuilder cb = new ConfigurationBuilder();  
      cb.setOAuthConsumerKey("xxxxx");
      cb.setOAuthConsumerSecret("xxxxx");
      cb.setOAuthAccessToken("xxxxxxxx");
      cb.setOAuthAccessTokenSecret("xxxxxxx"); 
    
      twitterFactory = new TwitterFactory(cb.build());    
      twitter = twitterFactory.getInstance();  
    
      println("connected");
    } 
    
    // Get your tweets
    void getTimeline() {     
      try {        
        statuses = twitter.getHomeTimeline();
      }   
      catch(TwitterException e) {         
        println("Get timeline: " + e + " Status code: " + e.getStatusCode());
      }     
      for (Status status:statuses) {               
        //println(status.getUser().getName() + ": " + status.getText());
        text(status.getUser().getName() + ": " + status.getText(), random(width), random(height), 300, 200);
      }
    }  
    // Search for tweets
    
    void getSearchTweets() {           
      try {        
        Query query = new Query("lomce");            
        QueryResult result = twitter.search(query);              
        for (Status status : result.getTweets()) {              
          //println("@" + status.getUser().getScreenName() + ":" + status.getText());
          text("@" + status.getUser().getScreenName() + ":" + status.getText());
        }
      }   
      catch (TwitterException e) {            
        println("Search tweets: " + e);
      }
    }
    
  • _vk_vk
    Answer ✓

    This is a for each loop it is used to iterate though a collection just as a regular for, but easier. It works like:

    very non technical explanation!

    for( Type tempName:collection){ doStuff(tempName);}

    each object in the collection will be assigned to tempName. The statements using tempName will be executed, and then the next object will be assigned, until there is no more to use. You can use a regular for instead. Will work same way: for (int i = 0; i < list.size(); i++){ list.get(i).doStuff(); }

  • Great _VK!!! amazing explanation!;)

  • edited October 2013

    So I've tried the code in this thread (and VK's code from the 3.0.3 thread) and I have the same error in both.

    connected
    Get timeline: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    Relevant discussions can be found on the Internet at:
        http://www.google.co.jp/search?q=00df323f or
        http://www.google.co.jp/search?q=64388485
    TwitterException{exceptionCode=[00df323f-64388485 c60d4d36-22d807e1 c60d4d36-22d807e1 c60d4d36-22d807e1], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.5} Status code: -1
    

    OSX 10.9, Processing 2.0.3, Twitter4J 3.0.5 Same exact code as above, any ideas?

    Quick Edit: forgot to mention that I'm getting a "NullPointerException" that highlights the following line from the sketch: for (Status status:statuses) {

  • Did you replaced xxxxx with your oauth data?

  • Yes, I didn't know if it was safe to post those keys it to the forum.

    When I run the sketch on this thread, I now get the following error: "The method text(String, float, float) in the type PApplet is not applicable for the arguments (String)"

  • Twitter won't by happy if you disclose your data. : )

    The text stuff is about line 68 where, indeed, is missing coordinates to text method. Anyway, try this.

    import java.util.*;
    
    List<Status>statuses = null;
    
    TwitterFactory twitterFactory;
    Twitter twitter;
    
    void setup() {     
      size(100, 100);    
      background(0); 
    
      connectTwitter();    
      getTimeline();  
      getSearchTweets();
    }  
    
    void draw() {     
      background(0);
    }  
    
    
    
    
    // Initial connection
    void connectTwitter() {  
      ConfigurationBuilder cb = new ConfigurationBuilder();  
      cb.setOAuthConsumerKey("xxx");
      cb.setOAuthConsumerSecret("xxx");
      cb.setOAuthAccessToken("xxx");
      cb.setOAuthAccessTokenSecret("xxx"); 
    
      twitterFactory = new TwitterFactory(cb.build());    
      twitter = twitterFactory.getInstance();  
    
      println("connected");
    } 
    
    // Get your tweets
    void getTimeline() {     
      try {        
        statuses = twitter.getHomeTimeline();
      }   
      catch(TwitterException e) {         
        println("Get timeline: " + e + " Status code: " + e.getStatusCode());
      }     
      for (Status status:statuses) {               
        println(status.getUser().getName() + ": " + status.getText());
      }
    }  
    // Search for tweets
    
    void getSearchTweets() {           
      try {        
        Query query = new Query("love");            
        QueryResult result = twitter.search(query);              
        for (Status status : result.getTweets()) {              
          println("@" + status.getUser().getScreenName() + ":" + status.getText());
        }
      }   
      catch (TwitterException e) {            
        println("Search tweets: " + e);
      }
    }
    
  • Thanks for the Answer _vk.

    So I have this like you suggested: import twitter4j.conf.*; import twitter4j.*; import twitter4j.auth.*; import twitter4j.api.*; import java.util.*;

    List<Status>statuses = null;
    
    TwitterFactory twitterFactory;
    Twitter twitter;
    
    void setup() {     
      size(100, 100);    
      background(0); 
    
      connectTwitter();    
      getTimeline();  
      getSearchTweets();
    }  
    
    void draw() {     
      background(0);
    }  
    
    
    
    
    // Initial connection
    void connectTwitter() {  
      ConfigurationBuilder cb = new ConfigurationBuilder();  
      cb.setOAuthConsumerKey("mykey");
      cb.setOAuthConsumerSecret("mykey");
      cb.setOAuthAccessToken("mykey");
      cb.setOAuthAccessTokenSecret("mykey");
    
    
      twitterFactory = new TwitterFactory(cb.build());    
      twitter = twitterFactory.getInstance();  
    
      println("connected");
    } 
    
    // Get your tweets
    void getTimeline() {     
      try {        
        statuses = twitter.getHomeTimeline();
      }   
      catch(TwitterException e) {         
        println("Get timeline: " + e + " Status code: " + e.getStatusCode());
      }     
      for (Status status:statuses) {               
        println(status.getUser().getName() + ": " + status.getText());
      }
    }  
    // Search for tweets
    
    void getSearchTweets() {           
      try {        
        Query query = new Query("love");            
        QueryResult result = twitter.search(query);              
        for (Status status : result.getTweets()) {              
          println("@" + status.getUser().getScreenName() + ":" + status.getText());
        }
      }   
      catch (TwitterException e) {            
        println("Search tweets: " + e);
      }
    }
    

    This same null pointer exception error:

    connected
    Get timeline: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    Relevant discussions can be found on the Internet at:
        http://www.google.co.jp/search?q=e5488403 or
        http://www.google.co.jp/search?q=0a254b76
    TwitterException{exceptionCode=[e5488403-0a254b76 6ece33f2-3a841f10 6ece33f2-3a841f10 6ece33f2-3a841f10], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.5} Status code: -1
    

    Did I do it wrong?

  • Installing Java for OSX worked for the error: Get timeline: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

    Thank you very much!

Sign In or Register to comment.