Project 5: Twitter and Processing Prototypes

by samscaife on Friday 27 April 2012

Currently I know that I want my visualisation to be delivered online but I'm not sure what I'm going to build it with. However to understand the processes involved I've decided to mess around with processing and the Twitter api.

There is a Java Libary which I found on a blog post by Jer Thorpe called Twitter4j which will let me access the twitter api. I created my api key by going to the developer section of twitter then messed around with some code I found so that I could understand what each of the sections did.

The first project uses the twitter search api. It was built by going through this Jer Thorpe tutorial then modding the code slightly.


This project searches a given time period for a random sample of X tweets containing Y, in this example X is 10 and Y is "Sleep".
The search api is good but it only gives you access to existing tweets not ones which are coming in live for this I need to use the streaming api.

I found an example by Michael Zick Doherty of using Twitter4j to access the streaming api and display images when they come in on a chosen subject. By looking at this and decompiling the Twitter4j library so I could understand its structure and how the classes worked.

With this knowledge I was able to write a prototype that I could increment a variable when ever a tweet came in. Once I had this I was able to create a display which would show this amount over time. One thing which was really useful while building it was to create a little debug panel which I could look at the amounts in my current variables and spot if there where any errors.


//Search term
String keywords[] = {"breakfast"};

//Global variables
int countDuration = 60000; //how long before the counter moves on (in miliseconds)
int globalCount;
int pGlobalCount; //previous global count value
int cp = 0; //counter position
float time = 0;
TweetCount[] tweetCount = new TweetCount[59]; //amount of bits of data to collect
TwitterStream twitter = new TwitterStreamFactory().getInstance();


// Twitter API variables
static String OAuthConsumerKey = "XXXXXXXXXXXXX";
static String OAuthConsumerSecret = "XXXXXXXXXXXX";
static String AccessToken = "XXXXXXXXXXXXXX";
static String AccessTokenSecret = "XXXXXXXXXXXXXXXXXXXXX";

//////////////////
//////////////////




void setup() {
size(1000, 400);
noStroke();
imageMode(CENTER);
smooth();

connectTwitter();
twitter.addListener(listener);
twitter.filter(new FilterQuery().track(keywords)); // This is where my filters come into the code

for(int i=0; i tweetCount[i] = new TweetCount();
}
}
void draw() {
background(0);
debug();
CountPosition();


for(int i=0; i float xpos = map(i, 0, tweetCount.length, 0, width);
tweetCount[i].render(xpos);
}

tweetCount[cp].counting();

}

// Initial connection
void connectTwitter() {
twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
AccessToken accessToken = loadAccessToken();
twitter.setOAuthAccessToken(accessToken);
}

// Loading up the access token
private static AccessToken loadAccessToken() {
return new AccessToken(AccessToken, AccessTokenSecret);
}

// This listens for new tweet
StatusListener listener = new StatusListener(){

public void onStatus(Status status) {

//println("@" + status.getUser().getScreenName() + " - " + status.getText());
globalCount++;
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
//System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
// System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}

public void onException(Exception ex) {
ex.printStackTrace();
}
};

void CountPosition() {

if(millis()-time >= countDuration) {
time = millis();
cp++;
pGlobalCount = globalCount;
}
if (cp==tweetCount.length) cp = 0; //counter postion reset
}

void debug() {
text("globalCount =" + globalCount, 20, 20);
text("pGlobalCount =" + pGlobalCount, 200, 20);
text("CountPosition =" + cp, 20, 40);
text("time =" + millis()/1000, 20, 60);
}


class TweetCount {

int count;
int countreset;

TweetCount() {

}


void render(float xpos) {

fill(255,100);
ellipse(xpos, height/2,count,count);
fill(255);
text(count, xpos, height/2+70);

}
void counting() {

count = 0;
count = globalCount-pGlobalCount;
//println("count =" + count + " globalCount =" + globalCount + " pGlobalCount =" + pGlobalCount);
}
}

At this stage I am happy with my knowledge of how the code side of it works I just need to look into how I will make this available online

Leave your comment