Differences between revisions 20 and 22 (spanning 2 versions)
Revision 20 as of 2017-02-19 21:55:18
Size: 8768
Editor: JunHu
Comment:
Revision 22 as of 2018-02-28 22:37:08
Size: 9206
Editor: JunHu
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
 1. Download and install the Twitter4J Java library (see instructions below)  1. 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.
Line 17: Line 17:
=== 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`
=== Installing the Twitter4J or "Simple Tweet" library ===

 * You can either install Twitter4J:
 
  *
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`

 * or install "Simple Tweet". "Simple Tweet" is built on top of Twitter4J - installing "Simple Tweet" will also install Twitter4J.
  * In Process, go to menu Sketch/Import Library/Add Library. In the "Libraries" tab, Filter "tweet", find and select "Simple Tweet", then "Install".
Line 32: Line 36:
 * Go to http://dev.twitter.com  * Go to http://apps.twitter.com

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

  • You can either install Twitter4J:
    • 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

  • or install "Simple Tweet". "Simple Tweet" is built on top of Twitter4J - installing "Simple Tweet" will also install Twitter4J.
    • In Process, go to menu Sketch/Import Library/Add Library. In the "Libraries" tab, Filter "tweet", find and select "Simple Tweet", then "Install".

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://apps.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

   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.

  • Log in to dev.twitter.com
  • In the top right of the page, hover over your avatar and click on ‘My applications’.
  • Click on your application
  • In the list of menu tabs, go to ‘Settings’
  • Underneath ‘Application Type’, there is an option called ‘Access’. Check that this is set to ‘Read and Write’.
  • Go to the ‘Details’ tab and check that your application is now set to be a ‘Read and Write’ application. NB. You may have to re-create your access token, which can be done at the bottom of the ‘Details’ page.

   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 }

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