Twitter Live Ticker
Easy to set up Twitter News Ticker


2011
The »onformative lab« is not only a place for tutorials and to showcase experimental projects but also a place to publish and share some code snippets and sketches that have been useful for us in the past and might be useful for others too.

While setting up the projection mapping visuals for the ZERO Event at Baalsaal in Hamburg we were asked if it would be possible to quickly set up some kind of twitter news ticker people could send live tweets to. We quickly hacked something together using Twitter4J, an unofficial Java library for the Twitter API. With Twitter4J, it is quite easy to integrate Twitter services into your Java and therefore processing projects. All you have to do to make use of the Twitter4J functions and classes is to add the twitter4j-core-x.x.x.jar to your processing sketch code folder. As our twitter ticker might not work with some future versions of the Twitter4J core jar, we packed and uploaded a working version of the sketch for you to download.


Download Source Code
Source Code

The following example is a basic twitter news ticker that searches for the recent tweets including a certain keyword which could be a hashtag, a username or any other word. The text then simply scrolls from right to left as seen on the image above. The important line 57 is where you actually query your tweets and can define your search criteria more explicit. You can do that for example by adding the following line to search only for tweets in a certain radius around a location defined by latitude and longitude, in this case 52/13 for Berlin and a radius of 200km.

query.setGeoCode(new GeoLocation(52.0, 13.0), 200.0, Query.KILOMETERS);

You can also limit the number of results by adding

query.setRpp(10);

but you can even add more filters to your search by simply adding some of the following search operators to your search query:
Operators

from:cedrickiefer
to:onformative
@p5berlin .
near:Berlin within:200km
since:2011-04-27
until:2011-04-27
filter:links
include:retweets
source:twitterfeed
Results

sent by person »cedrickiefer«.
sent to person »onformative«.
referencing person »p5berlin«
sent within 200km of »Berlin«.
sent since date »2011-04-27″ (year-month-day).
sent up to date »2011-04-27″.
only tweets linking to URLs.
sent via operator (web, tweetdeck, twitter4j etc)

processing code

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
PFont font;
PImage img;
String twitterMsgData;

Twitter myTwitter;
ArrayList tweets;
float x = 800;
int offsetX;

void setup() {
size(1800, 200);
colorMode(HSB, 360, 100, 100, 100);
img = loadImage("trans.gif");
font = createFont("V5PRC___.TTF", 30 );
textFont(font);
frameRate(30);
smooth();
noStroke();

myTwitter = new Twitter("USER", "PW");
loadTweets();
}

void draw() {
background( 0);
randomSeed(0);
int y = height/2;

if (frameCount%(30*60*5)==0)loadTweets(); //refresh every 5 min

for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
msg = msg.replaceAll("r|n", "");

Date d = t.getCreatedAt();

float textHeight = textAscent()+textDescent();

fill(random(255), 100, 100);
text(user+" : "+msg, x+offsetX, y);
offsetX += textWidth(msg)+textWidth(user)+textWidth(" : ");
}
image(img, -30, 0, 220, height);
pushMatrix();
rotate( PI);
image(img, -width-30, -height, 220, height);
popMatrix();
x-=5;
if (x<-offsetX-10) x = width+10;
offsetX = 0;
}

void loadTweets() {
try {
Query query = new Query("CedricKiefer");
query.setGeoCode(new GeoLocation(52.0, 13.0), 200.0, Query.KILOMETERS);
query.setRpp(3);
QueryResult result = myTwitter.search(query);

tweets = (ArrayList) result.getTweets();
}
catch (TwitterException te) {
println("Couldn’t connect: " + te);
}
}