Differences between revisions 6 and 7
Revision 6 as of 2014-02-12 19:50:57
Size: 2835
Editor: JunHu
Comment:
Revision 7 as of 2014-02-12 19:54:04
Size: 4896
Editor: JunHu
Comment:
Deletions are marked like this. Additions are marked like this.
Line 40: Line 40:
  == Examples ==

=== Searching Twitter for Tweets ===

{{{#java

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;

import java.util.*;

Twitter twitter;
String searchString = "pdeng";
List<Status> tweets;

int currentTweet;

void setup()
{
    size(800,600);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("mbAbxTdLPAETGSkP4oM5A");
    cb.setOAuthConsumerSecret("XAtLQmwQ0lmUCUY0Kqj8qSvQ9eYgEIPsTKO6437U");
    cb.setOAuthAccessToken("13220212-BS7HEn5tj14DBOjtNt3IuvhTZIxyv9rWoscUmMRIU");
    cb.setOAuthAccessTokenSecret("v8dZjU9JTADvaemGaMSAr65UYgKXpSCYaxtVyhV2LN181");

    TwitterFactory tf = new TwitterFactory(cb.build());

    twitter = tf.getInstance();

    getNewTweets();

    currentTweet = 0;
    
    thread("refreshTweets");
}

void draw()
{
    fill(0, 40);
    rect(0, 0, width, height);

    currentTweet = currentTweet + 1;

    if (currentTweet >= tweets.size())
    {
        currentTweet = 0;
    }

    Status status = tweets.get(currentTweet);

    fill(200);
    text(status.getText(), random(width), random(height), 300, 200);

    delay(1000);
}

void getNewTweets()
{
    try
    {
        Query query = new Query(searchString);

        QueryResult result = twitter.search(query);

        tweets = result.getTweets();
        
        println(tweets.size());
    }
    catch (TwitterException te)
    {
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}

void refreshTweets()
{
    while (true)
    {
        getNewTweets();

        println("Updated Tweets");

        delay(30000);
    }
}
}}}

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

  • In the "Sketchbook location" of Processing, create a sub directory called "libraries" if it does not exist;
  • In <Sketchbook location>\libraries, create a sub directory called "twitter4j";

  • In <Sketchbook location>\libraries\twitter4j, create a sub directory called "library";

  • Download the library from http://twitter4j.org (at the moment with the <version number> = 3.0.5)

  • Copy the file named twitter4j-core-<version number>.jar from the zip file, to <Sketchbook location>\libraries\twitter4j\library;

  • In <Sketchbook location>\libraries\twitter4j\library, rename the file twitter4j-core-<version number>.jar to twitter4j.jar

  • Check that you have the following hierarchy : <Sketchbook location>\libraries\twitter4j\library\twitter4j.jar

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…

  • Go to http://dev.twitter.com

  • Sign in with your Twitter account
  • Once you are signed in, hover over your avatar in the top right of the screen and choose ‘My Applications’
  • On the resulting page, click the button that says ‘Create a New Application’
  • Give your application a name, description and some relevant website (if you are just testing out how things work, say that in the description. Also, the website could just be your personal website if you don’t have one for the application).
  • Read the terms and conditions and if you agree to them tick the box and proceed with creating your application.
  • You will now be taken to a page that shows you some details about your application. You will find there your Consumer Key and your Consumer Secret. Save them to a text file somewhere secure, you will need them in this tutorial.
  • You will see a button towards the bottom called ‘Create my Access Token’. Click this button and when the page reloads you will find two more keys, the Access Token and the Access Token Secret. Store these two keys also, as they will be needed later.
  • Now we can begin writing our Twitter-enabled Processing sketch.

Examples

Searching Twitter for Tweets

{{{#java

import twitter4j.conf.*; import twitter4j.internal.async.*; import twitter4j.internal.org.json.*; import twitter4j.internal.logging.*; import twitter4j.json.*; import twitter4j.internal.util.*; import twitter4j.management.*; import twitter4j.auth.*; import twitter4j.api.*; import twitter4j.util.*; import twitter4j.internal.http.*; import twitter4j.*; import twitter4j.internal.json.*;

import java.util.*;

Twitter twitter; String searchString = "pdeng"; List<Status> tweets;

int currentTweet;

void setup() {

  • size(800,600);

    ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("mbAbxTdLPAETGSkP4oM5A"); cb.setOAuthConsumerSecret("XAtLQmwQ0lmUCUY0Kqj8qSvQ9eYgEIPsTKO6437U"); cb.setOAuthAccessToken("13220212-BS7HEn5tj14DBOjtNt3IuvhTZIxyv9rWoscUmMRIU"); cb.setOAuthAccessTokenSecret("v8dZjU9JTADvaemGaMSAr65UYgKXpSCYaxtVyhV2LN181");

    TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); getNewTweets(); currentTweet = 0; thread("refreshTweets");

}

void draw() {

  • fill(0, 40); rect(0, 0, width, height); currentTweet = currentTweet + 1;

    if (currentTweet >= tweets.size()) {

    • currentTweet = 0;
    } Status status = tweets.get(currentTweet); fill(200); text(status.getText(), random(width), random(height), 300, 200); delay(1000);

}

void getNewTweets() {

  • try {
    • Query query = new Query(searchString);

      QueryResult result = twitter.search(query); tweets = result.getTweets(); println(tweets.size());

    }

    catch (TwitterException te) {

    • System.out.println("Failed to search tweets: " + te.getMessage()); System.exit(-1);
    }

}

void refreshTweets() {

  • while (true) {
    • getNewTweets(); println("Updated Tweets"); delay(30000);
    }

} }}}

JunHu: TwitterProcessing (last edited 2018-02-28 22:37:08 by JunHu)