I'm trying to get processing to display the most recent tweet containing my keyword. I'm stuck. I'm not sure how I would check to see if it's updating. I know that it's able to pull the most recent tweet, but as of now, the only way I can view the most recent tweet is by restarting my program. Sorry it's messy.
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
Twitter twitter; // creating the twitter object
String searchString = "newborn"; // twitter search query
List<Status> tweets;// List of Status object to hold tweets
List<Status> tweets2;
int currentTweet;//
int nextTweet;
void setup ()
{
size(800,600);
ConfigurationBuilder cb = new ConfigurationBuilder(); // New config obj, auth.
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
TwitterFactory tf = new TwitterFactory(cb.build()); // init. twit obj by retr inst from twit fact
twitter = tf.getInstance(); //here, twit gets isnt is the inst
getTweets2();
getNewTweets(); // func to get the tweets
currentTweet = 0;
nextTweet = 1;
//Status status = tweets.get(currentTweet);
thread("refreshTweets");
}
void draw()
{
background(0);
//currentTweet = currentTweet + 1;
//nextTweet = nextTweet + 1;
if (currentTweet >= tweets.size())
{
currentTweet = 0;
}
if (nextTweet >= tweets.size())
{
nextTweet = 1;
}
Status status = tweets.get(currentTweet); // retrieve current tweet from list of tweets
Status status2 = tweets.get(nextTweet);
Status status3 = tweets2.get(0);
Status status4 = status;
fill(200);
if(tweets.get(0).getCreatedAt() != tweets2.get(0).getCreatedAt()) {
//text(status.getText(),random(width),random(height), 300, 200);
text("current tweet:" + status.getCreatedAt() + ".....if" ,250,250, 300, 200);
//text("next tweet:" + status2.getCreatedAt(),250,350, 300, 200);
//text("10th tweet:" + status3.getCreatedAt(),250,450, 300, 200);
}else {
text("current tweet:" + status2.getCreatedAt() + ".....else",250,250, 300, 200);
}
delay(2500);
getNewTweets();
//System.out.println(status2.getText());
}
void getNewTweets()
{
try
{
//we try to get the tweetuses!
Query query = new Query(searchString);
QueryResult result = twitter.search(query);
tweets = result.getTweets();
}
catch (TwitterException te)
{
//tweetus aborted?
System.out.println("failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
void getTweets2() //this neat thing gets those tweets
{
try
{
//we try to get the tweetuses!
Query query = new Query(searchString);
QueryResult result = twitter.search(query);
tweets2 = result.getTweets();
}
catch (TwitterException te)
{
//tweetus aborted?
System.out.println("failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
void refreshTweets()
{
while(true)
{
getNewTweets();
println("Updated Tweets");
delay(5000);
}
}
Note: You've posted your secret tokens publicly to the internet, so you should change them as soon as possible! Just editing your question to not include them won't be enough, since they'll still be in the revision history.
You've got a lot of extra stuff going on in this program. You can simplify this a great deal. All you need to do is this:
Step 1: Write a function that gets a tweet (or an ArrayList of tweets). Keep track of that tweet in a sketch-level variable. You've already done this with the getNewTweets()
function, but you can get rid of the other functions.
Step 2: Call that function at the beginning of the sketch. This is so you have something to show. Alternatively, you could just show some dummy text like "waiting to fetch tweets" until the next step.
Step 3: Every X seconds, call that function. You can do this by using the frameCount
variable from the draw()
function. Make sure you limit this so you don't go over twitter's rate limit.
Step 4: Use the sketch-level tweet variable to draw the current tweet however you want.
Putting it all together, it looks like this:
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
Twitter twitter;
String searchString = "cat";
Status currentTweet;
void setup ()
{
size(800, 600);
ConfigurationBuilder cb = new ConfigurationBuilder(); // New config obj, auth.
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
getNewTweets();
}
void draw()
{
//get new tweet once every 10 seconds
if (frameCount % (60*10) == 0) {
getNewTweets();
}
background(0);
fill(200);
text("current tweet:" + currentTweet.getText(), 250, 250, 300, 200);
}
void getNewTweets()
{
println("Getting new tweet...");
try
{
Query query = new Query(searchString);
QueryResult result = twitter.search(query);
currentTweet = result.getTweets().get(0);
}
catch (TwitterException te)
{
te.printStackTrace();
}
}