Hi
I'm writing a program that uses the twitter4j library to read tweets from the public twitter stream. Most of that is going pretty well. I've gotten it to filter for certain phrases and create a json file of all the tweets it collects.
What I want to do now is to follow a certain user and anytime they tweet something, I add that tweet message to my filter array (keywords[] as seen in the code below). I'm having trouble passing the information I collect in the StatusListener from the tweets back into the global variables in order to influence the filterQuery below it (I want to add terms to the query when posts come in from a specific user).
*fixed* I'm thinking that I'm going to need an arraylist to put the new query keywords into and then convert that to a string[] array so that the filterQuery can read it ( http://twitter4j.org/ja/javadoc/twitter4j/FilterQuery.html).
I dont know if anything I am saying really makes sense (but I hope it does!). I'm new to all of this so it has been a learning experience. If this is horribly unclear, let me know so I can try to explain the problems better.
Thanks!
*edit* some clarification here. (code updated)
I got the arraylist working and changed into an array to then update the QueryFilter. I have now moved the filterQuery and TwitterStreamFactory functions into draw as well, with a boolean that only runs them when a new tweet by the followed user comes up. This seems to generally be working, but the track function doesn't seem to change (tweeting new things to search for doesnt expand the filter).
I'm writing a program that uses the twitter4j library to read tweets from the public twitter stream. Most of that is going pretty well. I've gotten it to filter for certain phrases and create a json file of all the tweets it collects.
What I want to do now is to follow a certain user and anytime they tweet something, I add that tweet message to my filter array (keywords[] as seen in the code below). I'm having trouble passing the information I collect in the StatusListener from the tweets back into the global variables in order to influence the filterQuery below it (I want to add terms to the query when posts come in from a specific user).
*fixed* I'm thinking that I'm going to need an arraylist to put the new query keywords into and then convert that to a string[] array so that the filterQuery can read it ( http://twitter4j.org/ja/javadoc/twitter4j/FilterQuery.html).
I dont know if anything I am saying really makes sense (but I hope it does!). I'm new to all of this so it has been a learning experience. If this is horribly unclear, let me know so I can try to explain the problems better.
Thanks!
*edit* some clarification here. (code updated)
I got the arraylist working and changed into an array to then update the QueryFilter. I have now moved the filterQuery and TwitterStreamFactory functions into draw as well, with a boolean that only runs them when a new tweet by the followed user comes up. This seems to generally be working, but the track function doesn't seem to change (tweeting new things to search for doesnt expand the filter).
- int pcount = 0;
int secFromStart = millis() / 1000;
int minFromStart = 0;
long me = 517856571;
long obama = 813286;
long friedman = 59157393;
String keyword1 = "party";
ArrayList<String> keywords = new ArrayList<String>();
boolean newStrand = false;
long users[] = {
me, obama
};
ConfigurationBuilder cb = new ConfigurationBuilder();
TwitterStream ts;
FilterQuery fq = new FilterQuery(); //what twitterstream filters for
void setup() {
keywords.add (keyword1);
size (600, 600);
//sets up OAuth Authentication
cb.setOAuthConsumerKey("ofkb7JrFeEwqyTXvC9IXA");
cb.setOAuthConsumerSecret("vVw8Sz7aNpsHGfnqckmQPYY1eXlLhn1Qhbo8fOVlrs");
cb.setOAuthAccessToken("517856571-kat3vVvQuh3Sgl6oEnjzrZs5aR6OEY9QoSknpMMm");
cb.setOAuthAccessTokenSecret("WOpNE21ohWCyeMfLHT8DMWWEsudKcFiV6kylZyzus");
ts = new TwitterStreamFactory(cb.build()).getInstance(); //created new twitterstream object
StatusListener listener = new StatusListener() { //configures listenener
ArrayList<tweetObj> filteredTweets = new ArrayList<tweetObj>();
String title = "Obama"; //title of output file
void export() {
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy-HH-MM");
Date date = new Date();
//SAVE OUT JSON
String[] ss = new String[filteredTweets.size()];
for (int i = 0; i < filteredTweets.size(); i ++) {
tweetObj t = filteredTweets.get(i);
ss[i] = t.toJSON();
};
saveStrings("out/" + title + dateFormat.format(date) + ".txt", ss);
}
public void onStatus(Status status) {
boolean validTweet = false;
int termIsFoundIndex = -1;
long thisUser= status.getUser().getId(); //get username and message of filtered tweet
String thisStatusText = status.getText();
thisStatusText.toLowerCase();
if (thisUser == me) { // if I tweet print message and change
validTweet = true;
keywords.add(status.getText());
newStrand = true;
}
if (thisUser == obama) {
validTweet = true;
keywords.add(status.getText());
newStrand = true;
}
ArrayList<String> searchTerms = new ArrayList<String>();
searchTerms.add(keyword1);
for (int i = 0; i < searchTerms.size(); i++)
{
termIsFoundIndex = thisStatusText.indexOf(searchTerms.get(i));
if (termIsFoundIndex != -1)
{
validTweet = true;
i = searchTerms.size(); //this means just get out of the loop
}
}
if (validTweet)
{
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
System.out.println("Retweet Count : " + status.getRetweetCount());
println (keywords);
pcount++;
filteredTweets.add(new tweetObj(status));
export();
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
};
String[] keywordsArray = keywords.toArray(new String[0]);
fq.track(keywordsArray); //filters these words
fq.follow(users); //collects tweets by these users
ts.addListener(listener);
ts.filter(fq);
}
void draw() { //just keeping track
if (newStrand == true) {
String[] keywordsArray = keywords.toArray(new String[0]); - fq = new FilterQuery();
fq.track(keywordsArray); //filters these words - fq.follow(users);
ts.filter(fq);
newStrand = false;
}
background(0);
// textFont(tag);
textSize(24);
fill(255);
text("#" + pcount, 5, 50);
int secFromStart = millis() / 1000;
int minFromStart = 0;
if (secFromStart > 59) {
minFromStart++;
secFromStart = 0;
}
text(minFromStart + " : " + secFromStart + " : " + millis()/10, 5, 150); //not working right but good enough for now
text(keyword1, 5, 250);
}
- class tweetObj{ //implements Comparable{
String user;
String msg;
String loc;
String src;
int hours;
int mins;
int sec;
Date thisDate;
int hrsSinceFirst;
long retweets;
tweetObj(String u, String m, Date d) {
user = u;
msg = m;
thisDate = d;
}
tweetObj(Status thisStatus) {
user = thisStatus.getUser().getScreenName();
retweets = thisStatus.getRetweetCount();
msg = thisStatus.getText();
thisDate = thisStatus.getCreatedAt();
}
String printRetweet()
{
return "Retweet count: " + retweets;
}
long returnDate(){
return thisDate.getTime();
}
//FUNCTION TO EXPORT JSON
String toJSON() {
String myString = "";
//MAKE MAIN JSON OBJECT
JSONObject jo = new JSONObject();
try {
jo.put("msg", msg);
jo.put("rtwt", retweets);
jo.put("user", user);
jo.put("date", thisDate);
}
catch (JSONException e) {
println("TO JSON ERROR");
}
myString = jo.toString();
return(myString);
};
}
1