We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all, I am working on a project that requires Processing to continuously search for twitter hashtag. I've figured out how to search for multiple hashtags at one time but am still confused with how to modify the code below to meet my need. Anyone can help? Much thanks!
import twitter4j.conf.*;
import twitter4j.api.*;
import twitter4j.*;
import java.util.*;
import ddf.minim.*;
float z = 0;
float a = 0;
PImage bg;
float theta1 = PI/2;
String name = "";
String msg = "";
boolean stopr = true;
PImage img;
ConfigurationBuilder cb;
Query query1;
Query query2;
Query query3;
Query query4;
Twitter twitter;
AudioPlayer player;
Minim minim;
void setup() {
size(508, 512);
img = loadImage("");
imageMode(CENTER);
minim = new Minim(this);
player = minim.loadFile("", 1024);
player.play();
player.loop();
cb = new ConfigurationBuilder(); //Acreditacion
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("J");
cb.setOAuthAccessTokenSecret("");
twitter = new TwitterFactory(cb.build()).getInstance();
queryTwitter();
}
void queryTwitter() {
//Search for four hashtags on Twitter
query1 = new Query("#nyu");
query2 = new Query("#nyc");
query3 = new Query("#acbuilding");
query4 = new Query("#Inthecity");
query1.setCount(1);
query2.setCount(1);
query3.setCount(1);
query4.setCount(1);
//Start searching
try {
QueryResult result1 = twitter.search(query1);
QueryResult result2 = twitter.search(query2);
QueryResult result3 = twitter.search(query3);
QueryResult result4 = twitter.search(query4);
//Get a list of data from Twitter
ArrayList tweets1 = (ArrayList) result1.getTweets();
ArrayList tweets2 = (ArrayList) result2.getTweets();
ArrayList tweets3 = (ArrayList) result3.getTweets();
ArrayList tweets4 = (ArrayList) result4.getTweets();
//Confirm if
for (int i = 0; i < tweets1.size(); i++) {
Status t = (Status) tweets1.get(i);
User user = t.getUser();
name = user.getName();
//boolean a = boolean(name);
msg = t.getText();
//boolean b = boolean(msg);
println(name + ": " + msg);
}
for (int i = 0; i < tweets2.size(); i++) {
Status t = (Status) tweets2.get(i);
User user = t.getUser();
name = user.getName();
//boolean c = boolean(name);
msg = t.getText();
//boolean d = boolean(msg);
println(name + ": " + msg);
};
}
catch (TwitterException te) {
println("Couldn''t connect: " + te);
}
}
void draw(){
z = a + z;
bg = loadImage("clock.jpg");
background(bg);
image(img, width/2 - img.width/2, height - img.height/4);
//strokeWeight(30);
//stroke(255, 0, 0);
pushMatrix();
translate(width/2, height/2);
rotate(z);
image(img, 0, 0);
//line(0, 0, 0, -height/2);
popMatrix();
if (name != "" && msg != "") {
stopr = false;
} else {
stopr = true;
}
if (stopr == false) {
a = 0.01;
}
if (stopr == true) {
a = 0;
}
}
Answers
Can't you just call the queryTwitter() function whenever you want to update? So like, every X seconds or so?
Set apart w/ thread("") a function to deal w/ queries every delay(INTERVAL):
http://forum.processing.org/two/discussion/8467/a-quick-question-about-screenshots
also, see discussion of img4 here: http://forum.processing.org/two/discussion/8087/what-are-setup-and-draw
Maybe this is a job for the streaming API...
https://dev.twitter.com/streaming/overview
I'm currently working on a code that collects how many times a hashtag have or word have been mentioned and ranks those words from 1 to 3 , so i'm asking what kind of adjustment or addition I can make to this code to reach my goal.