Processing and Twitter

Screen shot 2013-03-06 at 1.06.42 AM

Recently I’ve been looking into creating some design that is heavily reliant on code and coding. I’m absolutely amazed by all the people out there willing to post the entire code of their sketches, or post regularly to help forums.. I don’t think I’d be able to get through an inch of the world of code without all these amazing and helpful people. Which got me to thinking that perhaps in return when I stumble on things that work I should also share my learning with people so they can gain from it too.

So, this week I was interested in collecting data off of twitter through processing. I found a really straight forward tutorial by Jer Thorp and through tinkering for a few hours I was able to get the sketch to function and collect tweets #Tasmeem.

This is the link to the original post, where you can find all the steps. I am only going to be writing about the things I needed to tweak to get it to run for me, and where I found those tweaks online:

http://blog.blprnt.com/blog/blprnt/updated-quick-tutorial-processing-twitter

Regarding the twitter oAuth process there are a few steps that are different, but its not something you can’t just messing around and figure out. Don’t forget you need all four codes for this to run.

To import the twitter4j library you can’t just simply drag and drop the file into the sketch as is it mentioned on Jer’s blog. You need to manually install the library following the steps listed here:

http://wiki.processing.org/w/How_to_Install_a_Contributed_Library

Here is an image, all the highlighted text had to be edited after I unzipped the twitter4j file, and placed into my sketch folder.

Screen shot 2013-03-07 at 12.16.51 AM

Over and over again I got the error “Cannot find a class or type named Tweet”. I found two really helpfully links:

https://forum.processing.org/topic/twitter4j-error-cannot-find-a-class-or-type-named-configuration-builder

http://stackoverflow.com/questions/14510641/twitter4j-not-recognizing-setrpp-or-tweet-class

Which made me add the following code:

import twitter4j.*;

import twitter4j.conf.*;

And switch out the lines of code about Tweet t = (tweet).. to:

Status t=(Status) tweets.get(i);
User u=(User) t.getUser();
String user=u.getName();

[If you are getting an error about setRpp the last link above had a solution to that also, I didn't]

Here is the edited code:

import twitter4j.*;

import twitter4j.conf.*;

import java.util.List;

//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(1300,800);
  background(0);
  smooth();

  //Credentials
  ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("XXXXXXXXXXXXXXXXXXX");
cb.setOAuthConsumerSecret("XXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessToken("XXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXX");

  //Make the twitter object and prepare the query
  Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  Query query = new Query("#tasmeem");

  //Try making the query request.
  try {
    QueryResult result = twitter.search(query);
    ArrayList tweets = (ArrayList) result.getTweets();

    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() {
  //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));
}

Advertisements

Design Crossing

 

Clearly I haven’t kept up with my 60 days sketch project; I got really sucked into reading on my topic.. Oh well..

Our semester started yesterday, and I can tell that with all the work ahead its going to fly by (thankfully). We started our first design project today (during orientation!), which is a group proposal for the Tasmeem design conference. Working in groups is extremely difficult, but it seemed so much easier with the guidance of our new associate director. We brainstormed for a while, put all our ideas on a board and then as a group we clustered those ideas into meaningful proposals. I was the only one to break out and put my own cluster together, and it was accepted. So tomorrow we are taking our four new directions and “exploding them” as Paolo likes to say. I’m supper excited because that means I have a one in four chance of having my idea being chosen by the group as our proposal.. But even if it doesnt get chosen I’ll be just as happy to exhibit work in the conference within the scope of the other proposals since they are all looking really interesting at the moment.