import java.util.Date; import java.util.Iterator; // http://blog.blprnt.com/blog/blprnt/updated-quick-tutorial-processing-twitter int twitterWaitTime = 10000; int startTime; public class TweetData { public int screenPosX, screenPosY; public int alpha; public String msg; } //Build an ArrayList to hold all of the words that we get from the imported tweets ArrayList words = new ArrayList(); ArrayList onScreenTweets = 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("w9EiFtV7stX3NYuu62zqhkpVo"); cb.setOAuthConsumerSecret("NC3c7RGB3jKDN083e28EXAmiQFdHb84Z1HrECDmzk5IN8UIuxi"); cb.setOAuthAccessToken("273554949-mloI7Qzj74MWivFQ20EFfICTbSY8OiQgRD9XikgF"); cb.setOAuthAccessTokenSecret("lPJE6E0WBHqBEfWyyPnzxYqAqPqYytzA64ujG41IrfBdf"); //Make the twitter object and prepare the query Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("#umdgrad15"); query.count(1); //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); 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); TweetData td = new TweetData(); td.screenPosX = width; td.screenPosY = (int)random(height); td.alpha = 0; td.msg = msg; onScreenTweets.add( td ); //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); }; startTime = millis(); } void draw() { clear(); //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)); Iterator tdIterator = onScreenTweets.iterator(); while (tdIterator.hasNext()) { textSize(20); TweetData td = tdIterator.next(); // returns a reference to the object in the list fill(255, 255, 255, td.alpha); td.alpha += 255/100; if (td.alpha > 255) td.alpha = 255; text(td.msg, td.screenPosX, td.screenPosY); td.screenPosX -= 1; // start lowering alpha once it's within 10% of the width if (td.screenPosX < -(int)(textWidth(td.msg)*0.80)) { td.alpha -= 255/50; } if (td.screenPosX < -(int)textWidth(td.msg)) { println("Removed tweet: " + td.msg); tdIterator.remove(); // remove it from the list... } } // //////////////////// // update tweets every 5-10 seconds or so // //////////////////// // Periodically, like every 5 seconds, check twitter // Calculate how much time has passed int elapsedTime = millis() - startTime; // Has total time in seconds passed? if (elapsedTime > twitterWaitTime) { thread( "requestNewTwitterData" ); // restart the clock startTime = millis(); } } void requestNewTwitterData() { //Credentials ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("w9EiFtV7stX3NYuu62zqhkpVo"); cb.setOAuthConsumerSecret("NC3c7RGB3jKDN083e28EXAmiQFdHb84Z1HrECDmzk5IN8UIuxi"); cb.setOAuthAccessToken("273554949-mloI7Qzj74MWivFQ20EFfICTbSY8OiQgRD9XikgF"); cb.setOAuthAccessTokenSecret("lPJE6E0WBHqBEfWyyPnzxYqAqPqYytzA64ujG41IrfBdf"); //Make the twitter object and prepare the query Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("#FifthHarmony"); query.count(1); //Try making the query request. try { QueryResult result = twitter.search(query); ArrayList tweets = (ArrayList) result.getTweets(); for (int j = 0; j < tweets.size(); j++) { // Tweet t = (Tweet) tweets.get(i); Status t = (Status) tweets.get(j); User u = (User) t.getUser(); String user = u.getName(); String msg = t.getText(); Date d = t.getCreatedAt(); println("Tweet by " + user + " at " + d + ": " + msg); TweetData td = new TweetData(); td.screenPosX = width; td.screenPosY = (int)random(height); td.alpha = 0; td.msg = msg; // Add this message to the tweet list onScreenTweets.add( td ); }; } catch (TwitterException te) { println("Couldn't connect: " + te); }; }