9310
Comment:
|
8768
|
Deletions are marked like this. | Additions are marked like this. |
Line 48: | Line 48: |
import twitter4j.*; import twitter4j.api.*; import twitter4j.auth.*; |
|
Line 49: | Line 52: |
import twitter4j.internal.async.*; import twitter4j.internal.org.json.*; import twitter4j.internal.logging.*; |
|
Line 53: | Line 53: |
import twitter4j.internal.util.*; | |
Line 55: | Line 54: |
import twitter4j.auth.*; import twitter4j.api.*; |
|
Line 58: | Line 55: |
import twitter4j.internal.http.*; import twitter4j.*; import twitter4j.internal.json.*; |
import twitter4j.util.function.*;; |
Line 139: | Line 134: |
* 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. |
* 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. |
Line 148: | Line 143: |
import twitter4j.*; import twitter4j.api.*; import twitter4j.auth.*; |
|
Line 149: | Line 147: |
import twitter4j.internal.async.*; import twitter4j.internal.org.json.*; import twitter4j.internal.logging.*; |
|
Line 153: | Line 148: |
import twitter4j.internal.util.*; | |
Line 155: | Line 149: |
import twitter4j.auth.*; import twitter4j.api.*; |
|
Line 158: | Line 150: |
import twitter4j.internal.http.*; import twitter4j.*; import twitter4j.internal.json.*; |
import twitter4j.util.function.*; |
Line 212: | Line 202: |
import twitter4j.*; import twitter4j.api.*; import twitter4j.auth.*; |
|
Line 213: | Line 206: |
import twitter4j.internal.async.*; import twitter4j.internal.org.json.*; import twitter4j.internal.logging.*; |
|
Line 217: | Line 207: |
import twitter4j.internal.util.*; | |
Line 219: | Line 208: |
import twitter4j.auth.*; import twitter4j.api.*; |
|
Line 222: | Line 209: |
import twitter4j.internal.http.*; import twitter4j.*; import twitter4j.internal.json.*; |
import twitter4j.util.function.*; |
Twitter in Processing
Contents
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:
- Download and install Processing
- Download and install the Twitter4J Java library (see instructions below)
- 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.*;
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/
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 }