latest working version of twitter4j library

edited November 2015 in Library Questions

Hi all! I am trying to obtain the twitter stream inside processing. I know it is necessary to install the library twitter4j. I tried with this one: http://codasign.com/tutorials/processing-and-twitter/processing-and-twitter-getting-started/ (the one inside the blue link)...following the instructions for the installation.

Than I use the code at this link https://github.com/neufuture/SimpleTwitterStream/

but when I run the processing gives me an error...Apparently it cannot read the class TwitterStream.

Could someone help me to figure out what it is not correct in my procedure? Thank you

Answers

  • _vk_vk
    Answer ✓

    http://twitter4j.org

    Search the Forum for Twitter. There are plenty of examples.

    There are some diferences from that version and the current one. So look for newer code. I have some code at home, if you haven't found any when I get there.

  • Hi! Do you mean there are some differences between the code I am using and a more recent one? It is quite old actually, true! If you have codes to share, they are welcomed! Thank you a lot for your help

  • _vk_vk
    edited November 2015

    tested with twitter4j 4.0.3

    Rest API. tweet/getTimeline/Search:

    import twitter4j.util.*;
    import twitter4j.*;
    import twitter4j.management.*;
    import twitter4j.api.*;
    import twitter4j.conf.*;
    import twitter4j.json.*;
    import twitter4j.auth.*;
    
    
    import java.util.*;
    
    List<Status>statuses = null;
    
    TwitterFactory twitterFactory;
    Twitter twitter;
    
    void setup() {     
      size(100, 100);    
      background(0); 
    
      connectTwitter();    
      getTimeline(); 
      delay(1000); 
      getSearchTweets();
      tweet("A tweet... via @Processing http" + "://processing.org");
    }  
    
    void draw() {     
      background(0);
    }  
    
    
    
    
    // Initial connection
    void connectTwitter() {  
      ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setOAuthConsumerKey("FILL");
      cb.setOAuthConsumerSecret("FILL");
      cb.setOAuthAccessToken("FILL");
      cb.setOAuthAccessTokenSecret("FILL"); 
    
      twitterFactory = new TwitterFactory(cb.build());    
      twitter = twitterFactory.getInstance();  
    
      println("connected");
    } 
    
    
    // Post a tweet
    void tweet(String t) { 
      try {
    
    
    
        Status status = twitter.updateStatus(t);
        System.out.println("Successfully updated the status to [" + status.getText() + "].");
      }
      catch (TwitterException e) {            
        println("Search tweets: " + e);
      }
    }  
    
    
    // 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 {        
        Paging paging = new Paging(1, 100);
        Query query = new Query("processing.org");
        //query.since("2015-05-20");
        //query.until("2015-05-29");    
        QueryResult result = twitter.search(query);              
        for (Status status : result.getTweets ()) {              
          println("@" + status.getUser().getScreenName() + ":" + status.getText() + " _DATE: " + status.getCreatedAt());
        }
      }   
      catch (TwitterException e) {            
        println("Search tweets: " + e);
      }
    }
    

    Stream API. Open with or without a query and save to JSON... NOt sure though if this is the one with save working...

    import twitter4j.util.*;
    import twitter4j.*;
    import twitter4j.management.*;
    import twitter4j.api.*;
    import twitter4j.conf.*;
    import twitter4j.json.*;
    import twitter4j.auth.*;
    
    TwitterStream twitterStream;
    List <Status> allStats = new ArrayList<Status>();
    
    void setup() {     
      size(100, 100);    
      background(0); 
      openTwitterStream();
    }  
    
    
    void draw() {     
      background(0);
    }  
    
    void keyPressed() {
      if (key == 's')
        saveStatus(allStats);
      println("done");
      exit();
    }
    
    
    
    // Stream it
    void openTwitterStream() {  
    
      ConfigurationBuilder cb = new ConfigurationBuilder();  
      cb.setOAuthConsumerKey("FILL");
      cb.setOAuthConsumerSecret("FILL");
      cb.setOAuthAccessToken("FILL");
      cb.setOAuthAccessTokenSecret("FILL"); 
      //cb.setDebugEnabled(true);
      cb.setJSONStoreEnabled(true);
      TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    
      FilterQuery filtered = new FilterQuery();
    
      // if you enter keywords here it will filter, otherwise it will sample
      String keywords[] = {
        "I love you"
      };
    
      filtered.track(keywords);
    
      twitterStream.addListener(listener);
    
      if (keywords.length==0) {
        // sample() method internally creates a thread which manipulates TwitterStream 
        twitterStream.sample(); // and calls these adequate listener methods continuously.
      } else { 
        twitterStream.filter(filtered);
      }
      println("connected");
    } 
    
    
    // Implementing StatusListener interface
    StatusListener listener = new StatusListener() {
    
      //@Override
      public void onStatus(Status status) {
        System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        allStats.add(status);
      }
    
      //@Override
      public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
      }
    
      //@Override
      public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
      }
    
      //@Override
      public void onScrubGeo(long userId, long upToStatusId) {
        System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
      }
    
      //@Override
      public void onStallWarning(StallWarning warning) {
        System.out.println("Got stall warning:" + warning);
      }
    
      //@Override
      public void onException(Exception ex) {
        ex.printStackTrace();
      }
    };
    
    
    import java.io.*;
    import java.util.List;
    
    
    void saveStatus(List <Status> status) {
      List <String> toWrite = new ArrayList<String>();
    
      println("Saving public timeline.");
      for (Status st : status) {
        String rawJSON = DataObjectFactory.getRawJSON(st);
        println("@" + st.getUser().getScreenName() + " - " + st.getText());
        //println("rawJASON= "+ rawJSON);
        String fileName = "statuses/" + st.getId() + ".json";
        toWrite.add(rawJSON + "\n");
    
        saveStrings("saved.txt", toWrite.toArray(new String[0]));
      }
    }//eof saveStatus
    

    Not the cleanest code around...

  • Hey Thank you so much!

Sign In or Register to comment.