Skip to content

Examples: Twitter API

francisli edited this page Oct 23, 2011 · 6 revisions

Fetching the Public Timeline

This example demonstrates basic usage of the GET method to request data from a server.

import com.francisli.processing.http.*;

HttpClient client;

void setup() {
  client = new HttpClient(this, "api.twitter.com");
  client.GET("/1/statuses/public_timeline.json");
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.getContentAsString());
}

void draw() {
  
}

Parsing data from the Public Timeline

This example demonstrates using the JSONObject to extract data from a JSON response.

import com.francisli.processing.http.*;

HttpClient client;

void setup() {
  client = new HttpClient(this, "api.twitter.com");
  client.GET("/1/statuses/public_timeline.json");
}

void responseReceived(HttpRequest request, HttpResponse response) {
  //// check for HTTP 200 success code
  if (response.statusCode == 200) {
    JSONObject results = response.getContentAsJSONObject();
    //// we know Twitter returns a JSON array, so use size() and get() to access elements
    for (int i = 0; i < results.size(); i++) {
      //// get the next element in the array
      JSONObject tweet = results.get(i);
      
      //// each element in the JSON array is an object/dictionary of metadata, 
      //// so we use get() with String keys to fetch data
      
      //// we know text is a String value
      String text = tweet.get("text").stringValue();
      
      //// we know user is another JSON object/dictionary
      JSONObject user = tweet.get("user");
      //// we know screen_name is a String value in the user object
      String screen_name = user.get("screen_name").stringValue();
      
      println(screen_name + ": " + text);
    }
  } else {
    //// output the entire response
    println(response.getContentAsString());
  }
}

void draw() {
  
}

Searching for Tweets

This example demonstrates passing query parameters to a GET request.

import com.francisli.processing.http.*;

HttpClient client;

void setup() {
  client = new HttpClient(this, "search.twitter.com");
  //// instantiate a new HashMap
  HashMap params = new HashMap();
  //// put key/value pairs that you want to send in the request
  params.put("q", "processing");
  client.GET("/search.json", params);
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.getContentAsString());
}

void draw() {
  
}

Posting a Status Update

This example demonstrates setting up OAuth authorization and using the POST method to send data. First, you will need to set up an application with Twitter and generate a user token for your account (see Setting up a new app with Twitter).

Important: Do not share your consumer secret and user token secret, or they can be used by anyone to post and abuse the system with your account! In this example, I’ve put placeholder String values for you to insert your own authorization data, but if you plan to share your code you should consider storing them in a separate file.

import com.francisli.processing.http.*;

HttpClient client;

void setup() {
  client = new HttpClient(this, "api.twitter.com");
  //// using SSL is optional, but recommended
  client.useSSL = true;
  //// turn on OAuth request signing
  client.useOAuth = true;
  //// set up OAuth signing parameters
  client.oauthConsumerKey = "<insert your app key here>";
  client.oauthConsumerSecret = "<insert your app secret here>";
  client.oauthAccessToken = "<insert your user token here>";
  client.oauthAccessTokenSecret = "<insert your user token secrent>";
  //// set up POST params
  HashMap params = new HashMap();
  params.put("status", "This is a tweet from a Processing sketch!");
  client.POST("/1/statuses/update.json", params);
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.statusCode + ": " + response.statusMessage);
  println(response.getContentAsString());
}

void draw() {
  
}

Posting a Status Update with an Image

This example demonstrates sending the contents of a file with a POST request.

import com.francisli.processing.http.*;

HttpClient client;

void setup() {
  size(640, 480);
  
  //// draw a pretty picture
  background(0);
  noStroke();
  float r = random(255);
  float g = random(255);
  float b = random(255);
  for (int diameter = 640; diameter > 0; diameter -= 40) {
    fill(r, g, b, 30);
    ellipse(width/2, height/2, diameter, diameter);
  }
  
  //// save current contents of frame to a file
  save("sketch.png");
  
  client = new HttpClient(this, "upload.twitter.com");
  //// using SSL is optional, but recommended
  client.useSSL = true;
  //// turn on OAuth request signing
  client.useOAuth = true;
  //// set up OAuth signing parameters
  client.oauthConsumerKey = "<insert your app key here>";
  client.oauthConsumerSecret = "<insert your app secret here>";
  client.oauthAccessToken = "<insert your user token here>";
  client.oauthAccessTokenSecret = "<insert your user token secrent>";
  //// set up POST params
  HashMap params = new HashMap();
  params.put("status", "Posting from Processing with an image!");
  HashMap files = new HashMap();
  files.put("media[]", "sketch.png");
  client.POST("/1/statuses/update_with_media.json", params, files);
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.statusCode + ": " + response.statusMessage);
  println(response.getContentAsString());
}

void draw() {
  
}

Clone this wiki locally