Strings and Data

icm08-data

This week I was interested in collecting data from Twitter through Processing.

First I had to install the Twitter4J Java library to use its functionality and get an API Key from Twitter to create an application based on its data.

My Processing sketch display words on the screen based upon a Twitter search query for “NYU_ITP”.

There are 4 main sections in the code: Importing Libraries, Authenticating with Twitter, Searching for Tweets and Displaying the Tweets. In the text area it displays the complete tweets, and in the sketch it breaks the tweets into words and throws them at random sizes and positions as an animation over time.

Processing Code:

//Import Libraries import twitter4j.*; import twitter4j.conf.*; import java.util.List; import java.util.Date; ArrayList words = new ArrayList(); void setup() { size(800, 600); background(0); smooth(); //Authenticate with Twitter ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("xxxxxx"); cb.setOAuthConsumerSecret("xxxxxx"); cb.setOAuthAccessToken("xxxxxx"); cb.setOAuthAccessTokenSecret("xxxxxx"); //Search for Tweets Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("itp_nyu"); try { QueryResult result = twitter.search(query); ArrayList tweets = (ArrayList) result.getTweets(); // Displaying the Tweets for (int i = 0; i < tweets.size (); i++) { Status t=(Status) tweets.get(i); User u=(User) t.getUser(); String user=u.getName(); 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() { fill(#006666, 15); rect(0, 0, width, height); int i = (frameCount % words.size()); String word = words.get(i); fill(255, random(50, 150)); textSize(random(20, 40)); text(word, random(width), random(height)); delay(100); }

Leave a Reply