Twitter in Processing

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) 1 Make sure you have a Twitter account

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

Sending Tweets from Processing

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

Creating Wordcram from tweets

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