Twitter Search Table (literally. a table)
in
Share your Work
•
1 year ago
What's the role of Computational Design in Industrial Design?
A series of experiments in how we might design physical objects with code.
Tell me what you think!
So this one searches twitter for a particular word or phrase, turns the results into blocks and then adds them to the "table". It uses
twitter4j, a java implementation of the twitter api. To use this, you need to get a developer application key from twitter. It's free. I'm working on a web version that will use processing.js that won't need the key.
The search stuff is inspired by
monitter.
- import processing.opengl.*;
- import superCAD.*;
- boolean saveOneFrame = false;
- String startDate = year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second();
- String endDate;
- String frontLeftText = "i am";
- String frontRightText = "i feel";
- String backLeftText = "i think";
- String backRightText = "i know";
- import peasy.*;
- PeasyCam cam;
- ConfigurationBuilder cb = new ConfigurationBuilder();
- long lastFrontLeftId = 0;
- long lastFrontRightId = 0;
- long lastBackLeftId = 0;
- long lastBackRightId = 0;
- float textCol;
- int maxLongLegDiff = 5;
- float sqSize = 160;
- float halfsq = sqSize/2;
- float legSize = 80;
- float halfLeg = legSize/2;
- int sideLength = 8;
- float blockSize = legSize/sideLength;
- Twitter twitter;
- PFont fontsm;
- PFont fontlg;
- ArrayList frontLeftTweets = new ArrayList();
- ArrayList frontRightTweets = new ArrayList();
- ArrayList backLeftTweets = new ArrayList();
- ArrayList backRightTweets = new ArrayList();
- boolean startRec = false;
- boolean isTurning = false;
- ArrayList [][] frontLeftLeg = new ArrayList[sideLength][sideLength];
- ArrayList [][] frontRightLeg = new ArrayList[sideLength][sideLength];
- ArrayList [][] backLeftLeg = new ArrayList[sideLength][sideLength];
- ArrayList [][] backRightLeg = new ArrayList[sideLength][sideLength];
- void setup(){
- size(1200, 600, OPENGL);
- //smooth();
- fontsm = loadFont("Inconsolata-24.vlw");
- fontlg = loadFont("Inconsolata-24.vlw");
- //textMode(MODEL);
- cam = new PeasyCam(this,0,0,-100, 500);
- cam.rotateX(-PI/6);
- //cam.pan(0, 50);
- cb.setOAuthConsumerKey("consumerKey");
- cb.setOAuthConsumerSecret("consumerSecret");
- cb.setOAuthAccessToken("accessToken");
- cb.setOAuthAccessTokenSecret("accessTokenSecret");
- cb.setUser("userName");
- twitter = new TwitterFactory(cb.build()).getInstance();
- for (int i=0; i<sideLength; i++){
- for(int j=0; j<sideLength; j++){
- frontLeftLeg[i][j] = new ArrayList();
- frontRightLeg[i][j] = new ArrayList();
- backLeftLeg[i][j] = new ArrayList();
- backRightLeg[i][j] = new ArrayList();
- }
- }
- setLastId(frontLeftText);
- setLastId(frontRightText);
- setLastId(backLeftText);
- setLastId(backRightText);
- // getTweets(leftTxt);
- // getTweets(rightTxt);
- }
- void draw(){
- background(255);
- //println(cam.getRotations());
- if (isTurning == true){
- cam.rotateY(PI/32);
- }
- getTweets(frontLeftText);
- getTweets(frontRightText);
- getTweets(backLeftText);
- getTweets(backRightText);
- if(saveOneFrame == true) {
- cam.reset();
- endDate = year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second();
- String Dateobj = frontLeftText + frontRightText + backLeftText + backRightText + "-" + startDate + "-" + endDate + ".obj";
- beginRaw("superCAD.ObjFile", Dateobj);
- }
- strokeWeight(1);
- stroke(0);
- noFill();
- pushMatrix();
- translate(0,0,blockSize/2);
- box(sqSize, sqSize, blockSize);
- popMatrix();
- fill(150, 150);
- for (int k=0; k<sideLength; k++){
- for (int i=0; i<sideLength; i++){
- for (int j=0; j<frontLeftLeg[k][i].size(); j++){
- pushMatrix();
- fill(150,0,0, 150);
- translate(-halfsq + (k+0.5)*blockSize, halfsq - legSize + (i+0.5)*blockSize, -(j+0.5)*blockSize);
- box(blockSize);
- popMatrix();
- }
- for (int j=0; j<frontRightLeg[k][i].size(); j++){
- pushMatrix();
- fill(0,150,0, 150);
- translate(halfsq - legSize + (k+0.5)*blockSize, halfsq - legSize + (i+0.5)*blockSize, -(j+0.5)*blockSize);
- box(blockSize);
- popMatrix();
- }
- for (int j=0; j<backLeftLeg[k][i].size(); j++){
- pushMatrix();
- fill(0,0,150, 150);
- translate(-halfsq + (k+0.5)*blockSize, -halfsq + (i+0.5)*blockSize, -(j+0.5)*blockSize);
- box(blockSize);
- popMatrix();
- }
- for (int j=0; j<backRightLeg[k][i].size(); j++){
- pushMatrix();
- fill(150,0,150, 150);
- translate(halfsq - legSize + (k+0.5)*blockSize, -halfsq + (i+0.5)*blockSize, -(j+0.5)*blockSize);
- box(blockSize);
- popMatrix();
- }
- }
- }
- strokeWeight(2);
- stroke(0);
- fill(250);
- pushMatrix();
- translate(0,0,-1);
- rect(-halfsq-270, halfsq, 260, 70); // front left
- rect(halfsq + 10, halfsq, 260, 70); // front right
- rect(-halfsq-270, -halfsq - 70, 260, 70); // back left
- rect(halfsq + 10, -halfsq - 70, 260, 70); // back right
- rect(-60, -125, 120, 40); // time
- popMatrix();
- if(saveOneFrame == true) {
- endRaw();
- saveOneFrame = false;
- }
- fill(0);
- textFont(fontlg, 12);
- textAlign(CENTER);
- text(startDate, 0, -110);
- String currTime = (hour() + ":" + minute() + ":" + second());
- text(currTime, 0, -95);
- textAlign(LEFT);
- textFont(fontlg, 12);
- textAlign(RIGHT);
- fill(150,0,0);
- text(frontLeftTweets.size() + " < " + frontLeftText, -halfsq-250, halfsq + 15, 230, 20, 0);
- textFont(fontlg, 12);
- textAlign(LEFT);
- fill(0,150,0);
- text(frontRightText + " > " + frontRightTweets.size(), halfsq+20, halfsq + 15, 230, 20, 0);
- textFont(fontlg, 12);
- textAlign(RIGHT);
- fill(0,0,150);
- text(backLeftTweets.size() + " < " + backLeftText, -halfsq-250, -halfsq - 60, 230, 20, 0);
- textFont(fontlg, 12);
- textAlign(LEFT);
- fill(150,0,150);
- text(backRightText + " > " + backRightTweets.size(), halfsq+20, -halfsq - 60, 230, 20, 0);
- textAlign(LEFT);
- for(int i=frontLeftTweets.size(); i>0; i--){
- textFont(fontsm, 8);
- String tw = (String) frontLeftTweets.get(i-1);
- tw = "> " + tw;
- textCol = map(i, 0, frontLeftTweets.size(), 50, 150);
- fill(textCol, 0, 0);
- text(tw, -halfsq - 250, halfsq + 30, 230, 52, -(frontLeftTweets.size() - i) * 10);
- }
- textAlign(LEFT);
- for(int i=frontRightTweets.size(); i>0; i--){
- textFont(fontsm, 8);
- String tw = (String) frontRightTweets.get(i-1);
- tw = "> " + tw;
- textCol = map(i, 0, frontRightTweets.size(), 50, 150);
- fill(0, textCol, 0);
- text(tw, halfsq + 20, halfsq + 30, 230, 52, -(frontRightTweets.size() - i) * 10);
- }
- textAlign(LEFT);
- for(int i=backLeftTweets.size(); i>0; i--){
- textFont(fontsm, 8);
- String tw = (String) backLeftTweets.get(i-1);
- tw = "> " + tw;
- textCol = map(i, 0, backLeftTweets.size(), 50, 150);
- fill(0, 0, textCol);
- text(tw, -halfsq - 250, -halfsq - 45, 230, 52, -(backLeftTweets.size() - i) * 10);
- }
- textAlign(LEFT);
- for(int i=backRightTweets.size(); i>0; i--){
- textFont(fontsm, 8);
- String tw = (String) backRightTweets.get(i-1);
- tw = "> " + tw;
- textCol = map(i, 0, backRightTweets.size(), 50, 150);
- fill(textCol, 0, textCol);
- text(tw, halfsq + 20, -halfsq - 45, 230, 52, -(backRightTweets.size() - i) * 10);
- }
- if (frameCount%5==0){
- //println("save!");
- String Date = year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second() + ".tiff";
- save(Date);
- }
- }
- //void keyPressed(){
- // if (key == CODED){
- // int roll = floor(random(0, sideLength));
- // if (keyCode == RIGHT){
- // rightSide[roll].add(new PVector(0,0));
- // }
- // if (keyCode == LEFT){
- // leftSide[roll].add(new PVector(0,0));
- // }
- // }
- // if (key == 's'){
- // saveOneFrame = true;
- // }
- //}
- void getTweets(String qT){
- //println(lastId);
- String queryTxt = qT;
- Query query = new Query(queryTxt);
- query.setResultType("RECENT");
- if (queryTxt == frontLeftText){
- query.setSinceId(lastFrontLeftId);
- } else if (queryTxt == frontRightText){
- query.setSinceId(lastFrontRightId);
- } else if (queryTxt == backLeftText){
- query.setSinceId(lastBackLeftId);
- } else if (queryTxt == backRightText){
- query.setSinceId(lastBackRightId);
- }
- try{
- QueryResult result = twitter.search(query);
- ArrayList tweets = (ArrayList) result.getTweets();
- float power = 1.35;
- float maxPow = pow(1, power);
- float rh, rv, rhPow, rvPow;
- int rollhor, rollver;
- for(int i=tweets.size()-1; i>=0; i--){
- Tweet t = (Tweet) tweets.get(i);
- String user = t.getFromUser();
- String msg = t.getText();
- long id = t.getId();
- Date d = t.getCreatedAt();
- rh = random(1);
- rhPow = pow(rh, power);
- //rollhor = floor(map(rhPow, 0, maxPow, sideLength, 0));
- rv = random(1);
- rvPow = pow(rv, power);
- //rollver = floor(map(rvPow, 0, maxPow, sideLength, 0));
- int frontLeftLegLength = frontLeftLeg[0][sideLength-1].size();
- int frontRightLegLength = frontRightLeg[sideLength-1][sideLength-1].size();
- int backLeftLegLength = backLeftLeg[0][0].size();
- int backRightLegLength = backRightLeg[sideLength-1][0].size();
- int longestLeg = max(frontLeftLegLength, frontRightLegLength, backLeftLegLength);
- longestLeg = max(longestLeg, backRightLegLength);
- //println("longest: " + longestLeg + " of FL:" + frontLeftLegLength + " of FR:" + frontRightLegLength + " of BL:" + backLeftLegLength + " of BR:" + backRightLegLength);
- if(queryTxt == frontLeftText){
- frontLeftTweets.add(msg);
- if (longestLeg - frontLeftLegLength > 4){
- frontLeftLeg[0][sideLength-1].add(new PVector(0,0));
- } else {
- rollhor = floor(map(rhPow, 0, maxPow, 0, sideLength));
- rollver = floor(map(rvPow, 0, maxPow, sideLength, 0));
- frontLeftLeg[rollhor][rollver].add(new PVector(0,0));
- }
- lastFrontLeftId = id;
- } else if (queryTxt == frontRightText){
- frontRightTweets.add(msg);
- if (longestLeg - frontRightLegLength > 4){
- frontRightLeg[sideLength-1][sideLength-1].add(new PVector(0,0));
- } else {
- rollhor = floor(map(rhPow, 0, maxPow, sideLength, 0));
- rollver = floor(map(rvPow, 0, maxPow, sideLength, 0));
- frontRightLeg[rollhor][rollver].add(new PVector(0,0));
- }
- lastFrontRightId = id;
- } else if (queryTxt == backLeftText){
- backLeftTweets.add(msg);
- if (longestLeg - backLeftLegLength > 4){
- backLeftLeg[0][0].add(new PVector(0,0));
- } else {
- rollhor = floor(map(rhPow, 0, maxPow, 0, sideLength));
- rollver = floor(map(rvPow, 0, maxPow, 0, sideLength));
- backLeftLeg[rollhor][rollver].add(new PVector(0,0));
- }
- lastBackLeftId = id;
- } else if (queryTxt == backRightText){
- backRightTweets.add(msg);
- if (longestLeg - backRightLegLength > 4){
- backRightLeg[sideLength-1][0].add(new PVector(0,0));
- } else {
- rollhor = floor(map(rhPow, 0, maxPow, sideLength, 0));
- rollver = floor(map(rvPow, 0, maxPow, 0, sideLength));
- backRightLeg[rollhor][rollver].add(new PVector(0,0));
- }
- lastBackRightId = id;
- }
- }
- }
- catch (TwitterException te){
- println("Couldn't connect: " + te);
- }
- }
- void setLastId(String qT){
- String queryTxt = qT;
- Query query = new Query(queryTxt);
- query.setResultType("RECENT");
- try{
- QueryResult result = twitter.search(query);
- ArrayList tweets = (ArrayList) result.getTweets();
- Tweet t = (Tweet) tweets.get(tweets.size()-1);
- long id = t.getId();
- if(queryTxt == frontLeftText){
- lastFrontLeftId = id;
- } else if(queryTxt == frontRightText){
- lastFrontRightId = id;
- } else if(queryTxt == backLeftText){
- lastBackLeftId = id;
- } else if(queryTxt == backRightText){
- lastBackRightId = id;
- }
- }
- catch (TwitterException te){
- println("Couldn't connect: " + te);
- }
- }
- void keyPressed(){
- if (key == 's'){
- saveOneFrame = true;
- }
- if (key == 't'){
- isTurning = !isTurning;
- }
- if (key == 'r'){
- cam.reset();
- }
- if (key == 'p'){
- cam.reset();
- cam.rotateX(-PI/2);
- }
- }
1