processing. twitter + madlib
since moving to munich, my time has been spent filling out paperwork, looking for work, looking for apartments, and realizing how bad my german has become. this of course leaves me no time to actually do stuff. i have some other stuff in the works that i started in san francisco, but it's not ready by any means yet.that said, to close the gap and make myself not feel so lazy. here's a project i completed back in april of this year. i had started it much earlier, but i made myself complete it in time for a presentation i gave when i worked at landor.
basically it's a mashup (is that right?) of twitter + madlibs + color tracking. i used blue light because we had a box full of lovely brinks LED-keychains in the office.
my idea was to create a modular and interact-able mad lib. using space as a medium of control, whenever a person moves in the room the story changes. on top of that using twitter as a means to augment the list of words used.
using a series of hashtags on twitter to represent the word types required for the story, desired words need to be added one hash tag at a time. if you mess up, there isn't a way to correct it, as even deleted entries remain in twitter's search cache for a while.
#s_noun (singular noun)
#p_noun (plural noun)
#s_adj (singular adjective)
#n_number (number)
#s_lat (superlative)
#s_verb (singular verb)
#p_verb (plural verb)
#pt_verb (past-tense verb)
#p_name (proper name)
#p_place (place)


at first i tried to access the twitter api using processing's built in xml library, but that didn't go so well so in the end i used the twitter4j library. however, it's reference/api was cumbersome for me to understand so i ended up writing my own class to handle accessing the data. it's a little sloppy and maybe there's an easier way. make sure you add the twitter4j .jar to your sketch
//-----------------------------------------------------------------------------
//libraries
//-----------------------------------------------------------------------------
import twitter4j.*;
//import processing.core.*;
//import processing.xml.*;
import java.text.SimpleDateFormat;
public interface FTwitterConstants {
//-----------------------------------------------------------------------------
//services
//-----------------------------------------------------------------------------
static final String TIMELINE_URL = "http://twitter.com/statuses/public_timeline.xml";
}
public class FTwitter implements FTwitterConstants {
//-----------------------------------------------------------------------------
//properties
//-----------------------------------------------------------------------------
private PApplet p5;
private Twitter t;
private java.util.List search;
private String name = "";
private String pass = "";
private String term = "";
private String[] entry;
private boolean date;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
//-----------------------------------------------------------------------------
//constructor
//-----------------------------------------------------------------------------
/**
* instantiate FTwitter
*
* @param thePApplet
* PApplet
*/
public FTwitter(PApplet papplet) {
p5 = papplet;
}
/**
* instantiate FTwitter
*
* @param thePApplet
* PApplet
* @param _name
* username
* @param _pass
* password
*/
public FTwitter(PApplet papplet, String _name, String _pass) {
p5 = papplet;
setName( _name );
setPassword( _pass );
}
//-----------------------------------------------------------------------------
//methods
//-----------------------------------------------------------------------------
/**
* search public tweets
*
* @param _term
* the search term
*/
public void search(String _term) {
term = _term;
t = new Twitter(name,pass);
Query q = new Query(term);
q.setRpp(100);
//q.setSinceId(0);
try {
QueryResult result = t.search(q);
search = result.getTweets();
/*
System.out.println("-----------------------------------------------------------------------------");
System.out.println("TwitterCollect.term: " + term );
System.out.println("-----------------------------------------------------------------------------");
System.out.println("TwitterCollect.result:");
System.out.println(result);
*/
} catch( TwitterException e) {
System.out.println("error on twitter status collect");
}
}
//-----------------------------------------------------------------------------
//sets
//-----------------------------------------------------------------------------
/**
* @param _name
* username
*/
public void setName(String _name) {
name = _name;
}
/**
* @param _pass
* password
*/
public void setPassword(String _pass) {
pass = _pass;
}
/**
* @param _date
* XXX
*/
public void setDate(boolean _date) {
date = _date;
}
//-----------------------------------------------------------------------------
//gets
//-----------------------------------------------------------------------------
/**
* @param w
* XXX
*/
public String getWord(int w) {
Tweet tweet = (Tweet) search.get(w);
String word = tweet.getText();
System.out.println("TwitterCollect.tweet: " + word);
String n_word = "";
String[] tweetList = PApplet.split(word, ' ');
for(int i=0; i!=tweetList.length; i++) {
if( !PApplet.trim(tweetList[i]).equals(term) ) {
if(i != tweetList.length-1) n_word += PApplet.trim(tweetList[i]) + " ";
else n_word += PApplet.trim(tweetList[i]);
}
}
return n_word;
}
/**
* return search results from twitter public feed
*
* @return publicText
*/
public String[] getPublicTimeline() {
t = new Twitter();
XMLElement xml = new XMLElement(p5, TIMELINE_URL);
String[] publicText;
if(xml != null) {
int numSites = xml.getChildCount();
publicText = new String[numSites];
for (int i=0; i<numSites; i++) {
XMLElement stat = xml.getChild(i);
XMLElement[] statText = stat.getChildren("text");
publicText[i] = statText[0].getContent();
System.out.println(statText[0].getContent());
}
return publicText;
} else {
return null;
}
}
/**
* return the number of search results
*
* @return search.size()
*/
public int getSearchNum() {
return search.size();
}
/**
* @param w
* index of status to return
*
* return the number of search results
*
* @return entry[w]
*/
public String getStatus(int w) {
return entry[w];
}
}
//libraries
//-----------------------------------------------------------------------------
import twitter4j.*;
//import processing.core.*;
//import processing.xml.*;
import java.text.SimpleDateFormat;
public interface FTwitterConstants {
//-----------------------------------------------------------------------------
//services
//-----------------------------------------------------------------------------
static final String TIMELINE_URL = "http://twitter.com/statuses/public_timeline.xml";
}
public class FTwitter implements FTwitterConstants {
//-----------------------------------------------------------------------------
//properties
//-----------------------------------------------------------------------------
private PApplet p5;
private Twitter t;
private java.util.List search;
private String name = "";
private String pass = "";
private String term = "";
private String[] entry;
private boolean date;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
//-----------------------------------------------------------------------------
//constructor
//-----------------------------------------------------------------------------
/**
* instantiate FTwitter
*
* @param thePApplet
* PApplet
*/
public FTwitter(PApplet papplet) {
p5 = papplet;
}
/**
* instantiate FTwitter
*
* @param thePApplet
* PApplet
* @param _name
* username
* @param _pass
* password
*/
public FTwitter(PApplet papplet, String _name, String _pass) {
p5 = papplet;
setName( _name );
setPassword( _pass );
}
//-----------------------------------------------------------------------------
//methods
//-----------------------------------------------------------------------------
/**
* search public tweets
*
* @param _term
* the search term
*/
public void search(String _term) {
term = _term;
t = new Twitter(name,pass);
Query q = new Query(term);
q.setRpp(100);
//q.setSinceId(0);
try {
QueryResult result = t.search(q);
search = result.getTweets();
/*
System.out.println("-----------------------------------------------------------------------------");
System.out.println("TwitterCollect.term: " + term );
System.out.println("-----------------------------------------------------------------------------");
System.out.println("TwitterCollect.result:");
System.out.println(result);
*/
} catch( TwitterException e) {
System.out.println("error on twitter status collect");
}
}
//-----------------------------------------------------------------------------
//sets
//-----------------------------------------------------------------------------
/**
* @param _name
* username
*/
public void setName(String _name) {
name = _name;
}
/**
* @param _pass
* password
*/
public void setPassword(String _pass) {
pass = _pass;
}
/**
* @param _date
* XXX
*/
public void setDate(boolean _date) {
date = _date;
}
//-----------------------------------------------------------------------------
//gets
//-----------------------------------------------------------------------------
/**
* @param w
* XXX
*/
public String getWord(int w) {
Tweet tweet = (Tweet) search.get(w);
String word = tweet.getText();
System.out.println("TwitterCollect.tweet: " + word);
String n_word = "";
String[] tweetList = PApplet.split(word, ' ');
for(int i=0; i!=tweetList.length; i++) {
if( !PApplet.trim(tweetList[i]).equals(term) ) {
if(i != tweetList.length-1) n_word += PApplet.trim(tweetList[i]) + " ";
else n_word += PApplet.trim(tweetList[i]);
}
}
return n_word;
}
/**
* return search results from twitter public feed
*
* @return publicText
*/
public String[] getPublicTimeline() {
t = new Twitter();
XMLElement xml = new XMLElement(p5, TIMELINE_URL);
String[] publicText;
if(xml != null) {
int numSites = xml.getChildCount();
publicText = new String[numSites];
for (int i=0; i<numSites; i++) {
XMLElement stat = xml.getChild(i);
XMLElement[] statText = stat.getChildren("text");
publicText[i] = statText[0].getContent();
System.out.println(statText[0].getContent());
}
return publicText;
} else {
return null;
}
}
/**
* return the number of search results
*
* @return search.size()
*/
public int getSearchNum() {
return search.size();
}
/**
* @param w
* index of status to return
*
* return the number of search results
*
* @return entry[w]
*/
public String getStatus(int w) {
return entry[w];
}
}
g = toggle sensor grid
m = toggle mouse or LED control
t = manually query twitter for updates
v = toggle video display (for debugging of LED control)
still to do:
- mirror coordinates (webcam)
and maybe one day:
- changing of LED detection color dynamically
-
-
Labels: processing