Week 6 Workshop

Sending and Searching for Tweets from Processing + Arduino

INSPIRATIONS

http://www.creativeapplications.net/webapp/5-twitter-art-projects-webapp/

http://www.creativeapplications.net/webapp/twitter-art-projects-part-2-webapp/

http://www.creativeapplications.net/webapp/5-twitter-art-projects-volume-3-webapp/

STEP 1

1. Create new Twitter Account

Screen Shot 2015-02-16 at 4.08.18 AM

2. In Settings, verify your Mobile number, Twitter doesn’t give developer permissions without a verified mobile number.

Screen Shot 2015-02-16 at 4.09.33 AM

2. Go to apps.twitter.com

Screen Shot 2015-02-16 at 4.06.29 AM

3. Create a new app

Screen Shot 2015-02-16 at 4.07.12 AM

4. Change Permission to Read, Write.

Screen Shot 2015-02-16 at 4.07.32 AM

5. Generate Auth and Access Tokens.

Screen Shot 2015-02-16 at 4.07.54 AM

STEP 2

1. Download Twitter4J library here.

2. Extract ZIP file and Install Library in your Processing > library folder.

Screen Shot 2015-02-16 at 4.14.55 AM

3. Restart Processing.

STEP 3

Code for Sending tweets:

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;

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();
}

STEP 4

Code for Searching tweets:

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;

Twitter twitter;
String searchString = "digital art";
List<Status> 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(250);
}

void getNewTweets()
{
    try 
    {
        Query query = new Query(searchString);

        QueryResult result = twitter.search(query);

        tweets = result.getTweets();
    } 
    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);
    }
}

References
http://codasign.com/tutorials/processing-and-twitter/

http://forum.processing.org/two/discussion/2897/tweeting-a-photo-with-twitter4j-how-solved

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s