Twitter Processing Library
Tutorial and library to setup processing 2.0 and twitter using twitter4j 4.0.1 First you need to download Processing version 2.0 or forward
- Register for account in Twitter API
We need to obtain access token and costumers keys form the twitter website. First you need to register for an account in the developers page of twitter. https://dev.twitter.com/ Once creating a account go into “My applications” here can create a new application for twitter.
The API from twitter uses a authentication system called OAuth. This lets you log into your twitter account from a custom made application, like a simple processing sketch. With this authentication method lets you write and read twitters from your account and from twitter.com You only need four keys to accomplish login in to your twitter account from another application.
- Consumer Key
- Consumer secret
- Access token
- Access token secret
- Processing Library Twitter4j
For us to use the Twitter API with java we need the open source library Twitter4J the latest version can be downloaded here. In this examples we used the version 4.0.1. If you want to include some other version of the Twitter4j other than the 4.0.1, you need to do the following.
- Download Twitter4J
- Find twitter4j-core-4.0.1.jar in the lib folder.
- Change the name twitter4j-core-4.0.1.jar to twitter4j401.jar
- Move it to a new folder named library
- Move the new folder to a new one named twitter4j401
- Copy this folder in libraries folder of processing
You can download the file from github here. Once you installed the library for processing and have all the necessaries accreditation you can include the following libraries.
import twitter4j.conf.*;
import twitter4j.api.*;
import twitter4j.*;
import java.util.*;
ConfigurationBuilder cb;
Query query;
Twitter twitter;
void setup() {
cb = new ConfigurationBuilder(); //Acreditacion
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
twitter = new TwitterFactory(cb.build()).getInstance();
queryTwitter();
}
The next function is a search query of the 10 latest twitt of the hastag #processing.
void queryTwitter() {
query = new Query("#processing");
query.setCount(10);
try {
QueryResult result = twitter.search(query);
List tweets = result.getTweets();
println("New Tweet : ");
for (Status tw : tweets) {
String msg = tw.getText();
println("tweet : " + msg);
}
}
catch (TwitterException te) {
println("Couldn''t connect: " + te);
}
}
The complete code can be downloaded from gihub This function obtains the latest twitts of the user CGenerativo
<code class="language-java"> void timeline() { try { words.clear(); User user = twitter.showUser("CGenerativo"); println(user.getName()); if (user.getStatus() != null) { List statusess = twitter.getUserTimeline(user.getName()); screenName = user.getScreenName(); for (Status status3 : statusess) { println(status3.getText()); words.add(status3.getText()); } } } catch(TwitterException te) { println("Couldn''t connect: " + te); } }
Obtain the 10 latest twitt of your timeline.
void timeline() {
try {
println("10 Twitter timeline");
Paging paging = new Paging(1, 10);
statuses = twitter.getHomeTimeline(paging);
for (Status status : statuses) {
println(status.getUser().getName() + ":" + status.getText());
}
}
catch(TwitterException te) {
println("Couldn''t connect: " + te);
}
}