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 
  35     twitter = tf.getInstance();
  36 
  37     getNewTweets();
  38 
  39     currentTweet = 0;
  40     
  41     thread("refreshTweets");
  42 }
  43 
  44 void draw()
  45 {
  46     fill(0, 40);
  47     rect(0, 0, width, height);
  48 
  49     currentTweet = currentTweet + 1;
  50 
  51     if (currentTweet >= tweets.size())
  52     {
  53         currentTweet = 0;
  54     }
  55 
  56     Status status = tweets.get(currentTweet);
  57 
  58     fill(200);
  59     text(status.getText(), random(width), random(height), 300, 200);
  60 
  61     delay(1000);
  62 }
  63 
  64 void getNewTweets()
  65 {
  66     try 
  67     {
  68         Query query = new Query(searchString);
  69 
  70         QueryResult result = twitter.search(query);
  71 
  72         tweets = result.getTweets();
  73         
  74         println(tweets.size());
  75     } 
  76     catch (TwitterException te) 
  77     {
  78         System.out.println("Failed to search tweets: " + te.getMessage());
  79         System.exit(-1);
  80     } 
  81 }
  82 
  83 void refreshTweets()
  84 {
  85     while (true)
  86     {
  87         getNewTweets();
  88 
  89         println("Updated Tweets"); 
  90 
  91         delay(30000);
  92     }
  93 }