71
Comment:
|
8408
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
= Twitter with Processing = | = Twitter in Processing = |
Line 4: | Line 4: |
== Twitter4J == | == 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 1. 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); } } }}} === Sending Tweets from Processing === {{{#!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.*; Twitter twitter; void setup() { size(800,600); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******"); cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************"); cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************"); cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************"); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); } void draw() { } void tweet() { try { Status status = twitter.updateStatus("This is a tweet sent from Processing!"); System.out.println("Status updated to [" + status.getText() + "]."); } catch (TwitterException te) { System.out.println("Error: "+ te.getMessage()); } } void keyPressed() { tweet(); } }}} === Creating Wordcram from 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.Date; import wordcram.*; //Build an ArrayList to hold all of the words hat we get from the imported tweets ArrayList<String> words = new ArrayList(); String s = ""; WordCram w; void setup() { //Set the size of the stage, and the background to black. size(550, 550); background(255); smooth(); //Credentials ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("*****YOUR-KEY-HERE******"); cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************"); cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************"); cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************"); //Make the twitter object and prepare the query Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("#tueindhoven"); query.count(100); //Try making the query request. try { QueryResult result = twitter.search(query); ArrayList tweets = (ArrayList) result.getTweets(); for (int i = 0; i < tweets.size(); i++) { Status t=(Status) tweets.get(i); User u=(User) t.getUser(); String user=u.getName(); String msg = t.getText(); s = s + " " + msg; Date d = t.getCreatedAt(); println("Tweet by " + user + " at " + d + ": " + msg); //Break the tweet into words String[] input = msg.split(" "); for (int j = 0; j < input.length; j++) { //Put each word into the words ArrayList words.add(input[j]); } }; } catch (TwitterException te) { println("Couldn't connect: " + te); }; w = new WordCram(this).fromTextString(s); } void draw() { w.drawAll(); }}} |
Twitter in Processing
Contents
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:
- Download and install Processing
- 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
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("*****YOUR-KEY-HERE******");
31 cb.setOAuthConsumerSecret("*************YOUR-KEY-HERE*************");
32 cb.setOAuthAccessToken("*************YOUR-KEY-HERE***************");
33 cb.setOAuthAccessTokenSecret("**********YOUR-KEY-HERE***************");
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();