Twitter in Processing

This wiki is based on a tutorial from http://codasign.com/tutorials/processing-and-twitter/

Get Started with Twitter4J in Processing

Twitter4J is an unofficial Java library for the Twitter API. With Twitter4J, you can easily integrate your Java application with the Twitter service. Twitter4J is an unofficial library.

You can easily use this library in Processing, since Processing is Java. But firstly, make sure you have done the following:

  1. Download and install Processing
  2. Download and install the Twitter4J Java library (see instructions below) or install the "Simple Tweet" library in Processing. /!\ but not both at the same time.

  3. Make sure you have a Twitter account

Installing the Twitter4J or "Simple Tweet" library

Getting an API Key From Twitter / Creating an Application

To obtain the necessary permission to use Processing with Twitter, we need to create an application with an accompanying set of `keys’. To do this…

Examples

Searching Twitter for Tweets

   1 import twitter4j.*;
   2 import twitter4j.api.*;
   3 import twitter4j.auth.*;
   4 import twitter4j.conf.*;
   5 import twitter4j.json.*;
   6 import twitter4j.management.*;
   7 import twitter4j.util.*;
   8 import twitter4j.util.function.*;;
   9 
  10 import java.util.*;
  11 
  12 Twitter twitter;
  13 String searchString = "pdeng";
  14 List<Status> tweets;
  15 
  16 int currentTweet;
  17 
  18 void setup()
  19 {
  20     size(800,600);
  21 
  22     ConfigurationBuilder cb = new ConfigurationBuilder();
  23     cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******");
  24     cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
  25     cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
  26     cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");
  27 
  28     TwitterFactory tf = new TwitterFactory(cb.build());
  29     twitter = tf.getInstance();
  30 
  31     getNewTweets();
  32     currentTweet = 0;
  33     
  34     thread("refreshTweets");
  35 }
  36 
  37 void draw()
  38 {
  39     fill(0, 40);
  40     rect(0, 0, width, height);
  41 
  42     currentTweet = currentTweet + 1;
  43 
  44     if (currentTweet >= tweets.size()) {
  45         currentTweet = 0;
  46     }
  47 
  48     Status status = tweets.get(currentTweet);
  49 
  50     fill(200);
  51     text(status.getText(), random(width), random(height), 300, 200);
  52 
  53     delay(1000);
  54 }
  55 
  56 void getNewTweets()
  57 {
  58     try 
  59     {
  60         Query query = new Query(searchString);
  61         QueryResult result = twitter.search(query);
  62         tweets = result.getTweets();
  63         println(tweets.size());
  64     } 
  65     catch (TwitterException te) 
  66     {
  67         System.out.println("Failed to search tweets: " + te.getMessage());
  68         System.exit(-1);
  69     } 
  70 }
  71 
  72 void refreshTweets()
  73 {
  74     while (true)
  75     {
  76         getNewTweets();
  77         println("Updated Tweets"); 
  78         delay(30000);
  79     }
  80 }

Sending Tweets from Processing

Check that our application is set to read AND write information - We can’t tweet unless our application has the right to send tweets.

   1 import twitter4j.*;
   2 import twitter4j.api.*;
   3 import twitter4j.auth.*;
   4 import twitter4j.conf.*;
   5 import twitter4j.json.*;
   6 import twitter4j.management.*;
   7 import twitter4j.util.*;
   8 import twitter4j.util.function.*;
   9 
  10 Twitter twitter;
  11 
  12 void setup()
  13 {
  14     size(800,600);
  15 
  16     ConfigurationBuilder cb = new ConfigurationBuilder();
  17     cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******");
  18     cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
  19     cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
  20     cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");
  21 
  22     TwitterFactory tf = new TwitterFactory(cb.build());
  23 
  24     twitter = tf.getInstance();
  25 }
  26 
  27 void draw()
  28 {
  29 
  30 }
  31 
  32 void tweet()
  33 {
  34     try 
  35     {
  36         Status status = twitter.updateStatus("This is a tweet sent from Processing!");
  37         System.out.println("Status updated to [" + status.getText() + "].");
  38     }
  39     catch (TwitterException te)
  40     {
  41         System.out.println("Error: "+ te.getMessage()); 
  42     }
  43 }
  44 
  45 void keyPressed()
  46 {
  47     tweet();
  48 }

Creating word clouds from tweets

To generate word clouds, we would also need the wordcram library: http://wordcram.org/

wordcram.png

   1 import twitter4j.*;
   2 import twitter4j.api.*;
   3 import twitter4j.auth.*;
   4 import twitter4j.conf.*;
   5 import twitter4j.json.*;
   6 import twitter4j.management.*;
   7 import twitter4j.util.*;
   8 import twitter4j.util.function.*;
   9 
  10 import java.util.Date;
  11 import wordcram.*;
  12 //Build an ArrayList to hold all of the words hat we get from the imported tweets
  13 ArrayList<String> words = new ArrayList();
  14 String s = "";
  15 WordCram w;
  16 
  17 void setup() {
  18   //Set the size of the stage, and the background to black.
  19   size(550, 550);
  20   background(255);
  21   smooth();
  22 
  23   //Credentials
  24     ConfigurationBuilder cb = new ConfigurationBuilder();
  25     cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******");
  26     cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
  27     cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
  28     cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");
  29 
  30   //Make the twitter object and prepare the query
  31   Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  32   Query query = new Query("#tueindhoven");
  33   query.count(100);
  34 
  35   //Try making the query request.
  36   try {
  37     QueryResult result = twitter.search(query);
  38     ArrayList tweets = (ArrayList) result.getTweets();
  39 
  40     for (int i = 0; i < tweets.size(); i++) {
  41       Status t=(Status) tweets.get(i);
  42       User u=(User) t.getUser();
  43       String user=u.getName();
  44       String msg = t.getText();
  45       s = s + " " + msg;
  46       Date d = t.getCreatedAt();
  47       println("Tweet by " + user + " at " + d + ": " + msg);
  48 
  49       //Break the tweet into words
  50       String[] input = msg.split(" ");
  51       for (int j = 0;  j < input.length; j++) {
  52         //Put each word into the words ArrayList
  53         words.add(input[j]);
  54       }
  55     };
  56   }
  57   catch (TwitterException te) {
  58     println("Couldn't connect: " + te);
  59   };
  60 
  61   w = new WordCram(this).fromTextString(s);
  62 }
  63 
  64 void draw() {
  65   w.drawAll();
  66 }