= 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 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. 1. 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 `\libraries`, create a sub directory called "`twitter4j`"; * In `\libraries\twitter4j`, create a sub directory called "`library`"; * Download the library from http://twitter4j.org (at the moment with the ` = 3.0.5`) * Copy the file named `twitter4j-core-.jar` from the zip file, to `\libraries\twitter4j\library`; * In `\libraries\twitter4j\library`, rename the file `twitter4j-core-.jar` to `twitter4j.jar` * Check that you have the following hierarchy : `\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 === {{{#!java import twitter4j.*; import twitter4j.api.*; import twitter4j.auth.*; import twitter4j.conf.*; import twitter4j.json.*; import twitter4j.management.*; import twitter4j.util.*; import twitter4j.util.function.*;; import java.util.*; Twitter twitter; String searchString = "pdeng"; List tweets; int currentTweet; 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(); 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 === 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. {{{#!java import twitter4j.*; import twitter4j.api.*; import twitter4j.auth.*; import twitter4j.conf.*; import twitter4j.json.*; import twitter4j.management.*; import twitter4j.util.*; import twitter4j.util.function.*; 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 word clouds from tweets === To generate word clouds, we would also need the wordcram library: http://wordcram.org/ {{attachment:wordcram.png}} {{{#!java import twitter4j.*; import twitter4j.api.*; import twitter4j.auth.*; import twitter4j.conf.*; import twitter4j.json.*; import twitter4j.management.*; import twitter4j.util.*; import twitter4j.util.function.*; import java.util.Date; import wordcram.*; //Build an ArrayList to hold all of the words hat we get from the imported tweets ArrayList 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(); } }}}