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!

35 thoughts on “UPDATED: Quick Tutorial – Processing & Twitter

    1. 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

  1. 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

  2. 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.

  3. 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

  4. Hi! Thank you so much for this! I will use in my architecture project, if I manage to change some things.

    Is it possible to set up the script to show specific words (lets say words that begin with the letter "T") in a color different from the rest? I have no background in this area and I just watched your lecture on TED and was very inspired, i tried this, and managed to make it work and now I am trying to work out creating random colors for the words as well (maybe this would then lead to what I want for the end product)

    Thank you so much again! your work is amazing and inspiring!

  5. ArrayList words = new ArrayList();

    void setup() {

    That gives me an error: unexpected token void
    Its somehow connected to the ArrayList, can anyone explain me this?
    I’m already stuck at the first few lines ;)
    Thanks in advance.

    1. If your code is as above, it is missing the closing brackets to setup method. This should compile (untested), but does nothing more than initialize an ArrayList; Those are Processing/java code. May help to look at Processig.org at learning tab.

      ArrayList words = new ArrayList();

      void setup() {}

  6. Hi, great tutorial, thank you . I did create a sketch thanks to it, now I'm wiling to get it on line, trough processing's, but i'm kind of lost. How does this works with processingjs? Is processing-twitter.js any way related/dependent to/of twitter4j? For me, i do not know javaScript, where is a good place to go to learn how to use processing-twitter.js?
    thanks in advance

    vk

  7. Hey I dragged the twitter4j-core-3.0.1.jar file to my sketch and tried to run some code but it could not find the class or type named "Tweet."
    I am not sure what I am doing wrong or how to fix this? Suggestions?

  8. Hey I dragged the twitter4j-core-3.0.1.jar file to my sketch and tried to run some code but it could not find the class or type named "Tweet."

  9. Hi,
    I am very new to processing and API's.. I entered the credentials and code but received an error, The function query.setRpp(100); does not exist, would anyone know how to fix this?
    Any response is greatly appreciated.
    Grace

  10. i have succeeded using twitter API, however, i do not know how to collect tweets from specific time(which i purposely setup)
    i used
    query.setSince("2013-01-01")

    but the result still gives me tweets from today..
    can anyone fix this?

    also, does anyone know the limit of amout of tweets i can get from API?
    i have read API limiting ..but i am still confused. ..
    is the limit based on only the amount? or the time? or both?
    can i still collect tweets from past?

    help plz

  11. I tried this and got an error: "Cannot find a type or class named 'Date.'"

    I was able to fix it by adding one line: import java.util.*;

    Apparently newer versions of Processing only use "official" Java libraries. In any case, after adding this the tutorial works like a charm!

  12. Thank you so much !
    I just had to do some research on the new version of twitter4j (did this on the 3.0.3 with Processing 2.0b.7). Some classes or method changed a bit, for those I had to deal with in this tutorial (but I'm sure many others had too) I found that:
    - Tweet became Status.
    - Rpp() became count().
    - getFromUser() became getUser() so you need to add a line which get the name of you user with getName() like this:
    User u=(User) t.getUser();
    String user=u.getName();

    I hope I'm not reposting, and I hope this helped someone because I'm a noob and it's my very first help comment.
    Thanks again, and excuse my english i'm french.

    H.

    1. Agreed. The new version of Twitter4j changes a bunch of stuff. Here is the middle part of the code showing the new necessary changes. (Also had to add 'Import java.util.Date;' at the very top to get the Date line to work)

      //Make the twitter object and prepare the query
      Twitter twitter = new TwitterFactory(cb.build()).getInstance();
      Query query = new Query("art");
      query.setCount(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++) {
      Status t = (Status)tweets.get(i);
      User user = t.getUser();
      String name = user.getName();
      String msg = t.getText();
      Date d = t.getCreatedAt();
      println("Tweet by " + name + " at " + d + ": " + msg);

  13. hey! great tut man!
    i had modified my code to the new twitter4j.
    i added the new twitter4j-3.0.3.jar into the sketch, but an error appears:
    the function setCount(int) does not exist.

    what the problem? oO
    i remaded the tutorial 2x and the samething is happening

    greats from brazil!

  14. Updated code that I got to work:

    Updated code that I got to work:

    import java.util.*;
    ArrayList<String> words = new ArrayList();

    void setup() {
    //Set the size of the stage, and the background to black.
    size(1000,1000);
    background(0);
    smooth();

    //Credentials
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("KrC0VKkQIalAEQz4r28A3A");
    cb.setOAuthConsumerSecret("20FMSCYekyYtupujrrVrU204LHDTku2BkbGmD2fYcwY");
    cb.setOAuthAccessToken("5695062-5bENHK4dS6lLnxzAtB5XOLRbFXvEjCDZOLXVn4");
    cb.setOAuthAccessTokenSecret("v34HNHvrFLYawJ6VKvv5fIxDBDWhDTKXJ3rBk8MfdE");

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>