Search code examples
twitterprocessingtwitter4j

Twitter4j error on processing


Im trying to follow this tutorial:

//Build an ArrayList to hold all of the words that we get from the
 imported tweets 
ArrayList<String> words = new ArrayList();  
 void setup() {   //Set the size of the stage, and the background to black. 
 size(550,550);  
 background(0); 
 smooth();
      //Credentials   ConfigurationBuilder cb = new ConfigurationBuilder();  
 cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");  
 cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
 cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
 cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
     //Make the twitter object and prepare the query  
 Twitter twitter = new  
 TwitterFactory(cb.build()).getInstance();   
 Query query = new Query("#OWS");  
  query.setRpp(100);
     //Try making the query request.   try {
     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();
       println("Tweet by " + user + " at " + d + ": " + msg);

       //Break the tweet into words
       String[] input = msg.split(" ");
       for (int j = 0;  j < input.length; j++) {
        //Put each word into the words ArrayList
        words.add(input[j]);
       }
     };   }   catch (TwitterException te) {
     println("Couldn't connect: " + te);   }; }   void draw() {   //Draw a faint black rectangle over what is currently on the stage so
 it fades over time.   fill(0,1);   rect(0,0,width,height);
       //Draw a word from the list of words that we've built   int i = (frameCount % words.size());   String word = words.get(i);
       //Put it somewhere random on the stage, with a random size and colour   fill(255,random(50,150));   textSize(random(10,30));  
 text(word, random(width), random(height)); }

But i get the following error when i run the code in processing. cannot find class or type named tweet

Ive added the twitter4j libraries by dragging and dropping to the processing IDE.

Im using processing 2.1 and twitter4j3.05

Any suggestions?


Solution

  • This is a basic example using twitter4j 3.0.5.

    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);
      }
    }