UPDATED: Quick Tutorial – Processing & Twitter

** Since I first released this tutorial in 2009, it has received thousands of views and has hopefully helped some of you get started with building projects incorporating Twitter with Processing. In late 2010, Twitter changed the way that authorization works, so I’ve updated the tutorial to get it inline with the new Twitter API functionality.

Accessing information from the Twitter API with Processing is (reasonably) easy. A few people have sent me e-mails asking how it all works, so I thought I’d write a very quick tutorial to get everyone up on their feet.

We don’t need to know too much about how the Twitter API functions, because someone has put together a very useful Java library to do all of the dirty work for us. It’s called twitter4j, and you can download it here. We’ll be using this in the first step of the building section of this tutorial.

1. Authentication

Here’s the tricky part. In the olden days, Twitter used to be happy with us sending it a username and password pair for authentication. Now, all of the Twitter API functionality uses an oAuth system, which allows people to authenticate applications through a login at twitter.com (so that developers never see the user’s login information).

That’s pretty good from a security standpoint, but it makes our job a little bit harder. Now, instead of just needing a short username and password, we need FOUR things to authenticate our sketch: A consumer key & secret, and an access token & secret. You can get all 4 of these things from the twitter developer page.

1. Visit https://dev.twitter.com/ and login with your twitter username and password
2. Click on the ‘Create an app’ button at the bottom right
3. Fill out the form that follows – you can use temporary values (like “Jer’s awesome test application”) for the first three fields.
4. Once you’ve agreed to the developer terms, you’ll arrive at a page which shows the first two of the four things that we need: the Consumer key and the Consumer secret. Copy and paste these somewhere so you have them ready to access.

5. To get the other two values that we need, click on the button that says ‘Recreate my access token’. Copy and paste those two values (Access token and Access token secret) so that we have all four in the same place.

2. Building

1. Open up a new Processing sketch.
2. Import the twitter4j library. In your downloaded twitter4j files, you’ll find the core .jar in a directory called ‘lib’. You’re looking for a file called something like ‘twitter4j-core-2.2.5.jar’. We can add this to our sketch by simply dragging the twitter4j-core-2.2.5.jar file onto your sketch window. If you want to check, you should now see this file in your sketch folder, inside of a new ‘code’ directory.
3. The first thing we’ll do in our new file is to store the credentials that we gathered in the first part of this tutorial into a ConfigurationBuilder object (this is a built-in part of the twitter4j library):

** Don’t use the credentials below – these are fake ones that I made up – use the ones that you copy & pasted in steps 4 & 5 above

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");

4. Now we’ll make the main Twitter object that we can use to do pretty much anything you can do on the twitter website – get status updates, run search queries, find follower information, etc. This Twitter object gets built by something called the TwitterFactory, which needs our configuration information that we set above:

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();

5. Now that we have a Twitter object, we want to build a query to search via the Twitter API for a specific term or phrase. This is code that will not always work – sometimes the Twitter API might be down, or our search might not return any results, or we might not be connected to the internet. The Twitter object in twitter4j handles those types of conditions by throwing back an exception to us; we need to have a try/catch structure ready to deal with that if it happens:

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#OWS");

try {
  QueryResult result = twitter.search(query);
}
catch (TwitterException te) {
  println("Couldn't connect: " + te);
};

5. This code is working – but we haven’t done anything with the results. Here, we’ll set the results per page parameter for the query to 100 to get the last 100 results with the term ‘#OWS’ and spit those results into the output panel:


ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#OWS");
query.setRpp(100);

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);
  };
}
catch (TwitterException te) {
  println("Couldn't connect: " + te);
};

It seems stupid to come all of this way without doing something visual, so let’s throw all of the words that we see in the tweets onto the screen, quick and dirty, at random positions. The following code takes our existing instructions and puts them into the standard Processing setup/draw enclosures so that we can do some animation over time:


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

  //Credentials
  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");
  cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
  cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
  cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");

  //Make the twitter object and prepare the query
  Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  Query query = new Query("#OWS");
  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 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));
}

** Extra-coolness: see this example working in the browser with Processing.js!!

That’s it! This example is very simple, but it’s the bare bones of what you need build a project which connects Twitter through Processing.

Shortly I’ll add another post which shows how to use Processing with the Twitter Streaming API, so that we can deal with large volumes of tweets over time.

Good luck!

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

13 Comments

  1. wandering_bear
    Posted October 27, 2011 at 2:59 pm | Permalink

    Great update Jer! Also good choice of hashtag!

  2. That Dude
    Posted October 28, 2011 at 4:06 pm | Permalink

    Will this work on Processing 1.2.1?

  3. Posted November 15, 2011 at 6:18 pm | Permalink

    thank u sir! u are awesome!

  4. Leif
    Posted November 16, 2011 at 7:24 am | Permalink

    How do i write a code to get a users location?

    Any help will be much appriciated!

    Thanks

  5. Posted November 17, 2011 at 11:51 am | Permalink

    Just a quick word of caution; with the latest version of Twitter4J you might see errors related to "Google App Engine". To solve this go to the library and delete the directory twitter4j-appengine and twitter4j-appengine-2.2.4.jar.

    See here for more details:

    http://groups.google.com/group/twitter4j/browse_t…

  6. That Dude
    Posted November 25, 2011 at 11:37 pm | Permalink

    I keep getting an unexpected token error on the line that starts with "cb.setOAuthConsumerKey" I cant get past step 1… running processing 1.2.1 and using twitter4j-2.2.5

    any help/suggestions appreciated

  7. Heather
    Posted November 28, 2011 at 7:47 pm | Permalink

    worked like a charm, thanks for this!

  8. Marcelo
    Posted December 7, 2011 at 8:52 am | Permalink

    Thanks a lot!!
    Very helpfull !!! IT REALLY WORKS!

  9. Posted December 8, 2011 at 4:50 pm | Permalink

    I just dl'ed the twitter4j zip from Github, but there's no .jar file. Is this the right url?
    https://github.com/yusuke/twitter4j/downloads

  10. Posted December 15, 2011 at 2:03 pm | Permalink

    The .zip file from the download link above had no .jar files. I was redirected to:
    http://twitter4j.org/en/index.html#download

    which contained them.

  11. Reemo
    Posted January 8, 2012 at 6:23 am | Permalink

    first of all, thanks a lot for this great tutorial, it was and still is really helpful. But anyhow I have a few questions:
    1st: is it possible to receive tweets only from a particular user? If so, how could I solve this?
    2nd: is it by any chance possible to filter the outcome by one language and without particular symbols?
    … I would appreciate any helpful support, thanks in advance.

    cheers

  12. john
    Posted January 25, 2012 at 1:43 pm | Permalink

    how do you query tweets that contain an exact phrase, where if i use "i will be there". I want to query for the tweets that contains the phrase "i will be there". The results can contain anything else as well, but it has to at least contain those exact words in that order.

  13. Posted February 8, 2012 at 4:40 am | Permalink

    Thanks for your example, it is really helpful!! Recently I am working on a project about extracting twitter feed and display it in another visual form with Kinect camera tracking! Your tutorial give me a great help. thanks

One Trackback

  1. By Processing & Twitter « Another Word For It on November 2, 2011 at 7:25 pm

    [...] Processing & Twitter [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*