Twitter in Processing: leading up to the ICM final

I am going to work with twitter for my final. This is a first attempt to deal with twitter data. I’m started by using the Twitter4j library and following the example on Jer Thorpe’s page. From there I needed to have more control of the words so I have moved the words into a class. The sketch below shows the words animating backwards in Z space. Due to complications with the processing for wordpress script, the words are not being displayed. Instead you see nulls where the words would be.

The code is below:

//Info: http://processingjs.org/reference
//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList words = new ArrayList();

ArrayList wordsPicked = new ArrayList();

void setup() {
//Set the size of the stage, and the background to black.
size(640, 480, P3D);
background(245);
smooth();
frameRate(10);

//Creditials
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(“YIrkYN0ci6CpwmKaAOZL2w”);
cb.setOAuthConsumerSecret(“w7HVBFWPtHBm7hDsgZ8a74S6HJcw4tCy0TcV2M3K2g”);
cb.setOAuthAccessToken(“401756997-ZEBizWu10ei2rIHLsTermvy0FDamHswFhMT9rEEP”);
cb.setOAuthAccessTokenSecret(“sDtucOyTt3oeK4hiJabhxXM0KzqvtUsEJXuCgUgAiE”);

//Make the twitter object and prepare the query
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query(“#Obama”);
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 background
background(255);

//Draw a word from the list of words that we’ve built
int i = (frameCount % words.size());
String word = words.get(i);
wordsPicked.add(new Word(word, random(width), random(height), 100, random(10, 30)));

for (int w = 0; w < wordsPicked.size(); w++) {
Word thisWord = (Word) wordsPicked.get(w);
thisWord.display();
thisWord.update();
}
}

class Word {
String word;
float x, y, z, size;

Word (String word_, float x_, float y_, float z_, float size_) {
word = word_;
x = x_;
y = y_;
z = z_;
size = size_;
}

void display() {
//Put it somewhere random on the stage, with a random size and colour
fill(0, 0, 220, random(50, 150));
textSize(size);
text(word, x, y, z);
}

void update() {
z–;
}

void kill() {
}
}

Leave a Reply