Skip to content

Instantly share code, notes, and snippets.

@atomize
Created March 1, 2012 08:10
Show Gist options
  • Save atomize/1948224 to your computer and use it in GitHub Desktop.
Save atomize/1948224 to your computer and use it in GitHub Desktop.
Processing and Twitter API controlP5 for UI - search and see pretty spiraling #tweets
import processing.opengl.*;
import controlP5.*;
import napplet.test.*;
import napplet.*;
import interfascia.*;
// Twitter #trends visualization - random size, color, alpha - x,y within 500x500 frame... to form the core of RT.pjs
// for example code Communicate betweem Processing and Twitter Api !!! thanks to Jungho Yeom 2010-11-06
Twitter twitter;
java.util.List statuses = null;
PFont fontA;
String accessToken = "XXXXXXXXXXXXXXXXX";
String accessTokenSecrect = "XXXXXXXXXXXXXXXXX";
String consumerKey = "XXXXXXXXXXXXXXXXX";
String consumerSecret = "XXXXXXXXXXXXXXXXX";
String updateMessage = "d @github I'm starting to 0wn processing";
int fontsize = 15;
ControlP5 controlP5;
int messageBoxResult = -1;
ControlGroup messageBox;
String messageBoxString = "";
void setup(){
size(640, 480, OPENGL) ;
//background(0);
//smooth();
frameRate(30);
twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret, new AccessToken(accessToken,accessTokenSecrect));
controlP5 = new ControlP5(this);
createMessageBox();
controlP5.Button b = controlP5.addButton("toggleBox",1,20,20,100,20);
b.setLabel("#New #Search");
fontA = createFont("HelveticaNeue-Bold-48.vlw",48,true);
textFont(fontA);
}
float t;
void draw(){
//hint(ENABLE_DEPTH_TEST); // ! remember hint(ENABLE_NATIVE_FONTS)
if(messageBox.isVisible()) {
background(0);
} else {
if (mousePressed == true ) {
fill(random(0,255),random(0,255),random(99,255),random(50,255));
pushMatrix();
float gravity;
gravity = 50;
translate(mouseX,pmouseY,9);
rotateY(t+=0.3*1);
Query();
//background(0);
popMatrix();
//loop();
} else {
redraw();
}
}
}
void Query() {
try {
Query query = new Query(""+ messageBoxString +"");
query.setRpp(4);
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
String lines = "@"+ user+": "+ msg + "\n";
Date d = t.getCreatedAt();
// background(0);
// fill(random(pmouseX, pmouseY),pmouseX ,pmouseY, random(55,200));
//text(user, 25, 25 );
textAlign(CENTER);
// textFont(fontA, fontsize*sin(random(80, i+78)));
textFont(fontA, fontsize);
text(lines, 20+20*i, i+pmouseY, 600, 600 );
textFont(fontA, fontsize+i*2);
fill(random(44,255),random(44,255),random(44,255),random(230,255));
text("@"+user, pmouseX, pmouseY, 600, 600 );
//redraw();
};
//delay(2000);
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
//noLoop();
loop();
};
void callMyTweet() {
try
{
statuses = twitter.getUserTimeline();
}
catch (TwitterException e)
{
println(e.getStatusCode());
}
for (int i = 0; i<statuses.size();i++){
Status status = (Status)statuses.get(i);
println("Friend name: "+status.getUser().getName() +" My tweet: "+status.getText());
};
}
/*
void callHomeTimeline() { //call latest 20 tweets posted from you and your firneds
try
{
statuses = twitter.getHomeTimeline();
}
catch (TwitterException e)
{
println(e.getStatusCode());
}
for (int i = 0; i<statuses.size();i++){
Status status = (Status)statuses.get(i);
text("@"+status.getUser().getScreenName() +": "+status.getText(), 60, (60+i)*2);
}
}
void updateMyTweet(String mes) { //update your tweet
try
{
Status upMes = twitter.updateStatus(mes);
}
catch (TwitterException e)
{
println(e.getStatusCode());
}
}
*/
/*
void actionPerformed(GUIEvent e) {
if (e.getMessage().equals("Completed")) {
draw();
noLoop();
}
}
void mousePressed() {
background(0);
redraw();
}
*/
void toggleBox(int theValue) {
if(messageBox.isVisible()) {
messageBox.hide();
} else {
messageBox.show();
}
}
void createMessageBox() {
// create a group to store the messageBox elements
messageBox = controlP5.addGroup("messageBox",width/2 - 150,25,300);
messageBox.setBackgroundHeight(120);
messageBox.setBackgroundColor(color(0,100));
messageBox.hideBar();
// add a TextLabel to the messageBox.
Textlabel l = controlP5.addTextlabel("messageBoxLabel","Type in keyword or #trend ... push [ENTER] :-p",20,20);
l.moveTo(messageBox);
// add a textfield-controller with named-id inputbox
// this controller will be linked to function inputbox() below.
Textfield f = controlP5.addTextfield("inputbox",20,36,260,20);
f.captionLabel().setVisible(false);
f.moveTo(messageBox);
f.setColorForeground(color(20));
f.setColorBackground(color(20));
f.setColorActive(color(100));
// add the OK button to the messageBox.
// the name of the button corresponds to function buttonOK
// below and will be triggered when pressing the button.
controlP5.Button b1 = controlP5.addButton("buttonOK",0,65,80,80,24);
b1.moveTo(messageBox);
b1.setColorBackground(color(40));
b1.setColorActive(color(20));
// by default setValue would trigger function buttonOK,
// therefore we disable the broadcasting before setting
// the value and enable broadcasting again afterwards.
// same applies to the cancel button below.
b1.setBroadcast(false);
b1.setValue(1);
b1.setBroadcast(true);
b1.setCaptionLabel("OK");
// centering of a label needs to be done manually
// with marginTop and marginLeft
//b1.captionLabel().style().marginTop = -2;
//b1.captionLabel().style().marginLeft = 26;
// add the Cancel button to the messageBox.
// the name of the button corresponds to function buttonCancel
// below and will be triggered when pressing the button.
controlP5.Button b2 = controlP5.addButton("buttonCancel",0,155,80,80,24);
b2.moveTo(messageBox);
b2.setBroadcast(false);
b2.setValue(0);
b2.setBroadcast(true);
b2.setCaptionLabel("Cancel");
b2.setColorBackground(color(40));
b2.setColorActive(color(20));
//b2.captionLabel().toUpperCase(false);
// centering of a label needs to be done manually
// with marginTop and marginLeft
//b2.captionLabel().style().marginTop = -2;
//b2.captionLabel().style().marginLeft = 16;
}
// function buttonOK will be triggered when pressing
// the OK button of the messageBox.
void buttonOK(int theValue) {
println("a button event from button OK.");
messageBoxString = ((Textfield)controlP5.controller("inputbox")).getText();
messageBoxResult = theValue;
messageBox.hide();
}
// function buttonCancel will be triggered when pressing
// the Cancel button of the messageBox.
void buttonCancel(int theValue) {
println("a button event from button Cancel.");
messageBoxResult = theValue;
messageBox.hide();
}
// inputbox is called whenever RETURN has been pressed
// in textfield-controller inputbox
void inputbox(String theString) {
println("got something from the inputbox : "+theString);
messageBoxString = theString;
messageBox.hide();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment