I'm writing a program using the Twitter4j library and need to create a new object (called a FilterQuery) upon a certain event occuring (which will happen multiple times, creating multiple filterqueries). I need to run a method from this object so I need to know what it is called. If I knew how many times this would happen I would just declare them all beforehand and name them sequentially. But since I don't know how many I am going to need, and creating them all beforehand seems very inefficient, I wondering how i could create new ones on a running tally.
if(eventOccurs == true){
FilterQuery fq# = new FilterQuery;
fq#.follow;
eventOccurs = false;
}
Do I need to make an arraylist to put them all in? How do I add numbers dynamically (the # in the above example)
Thanks
I can be more specific about the code if that would help.
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).
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; }
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 } }
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); }
I am in the process of creating a program that uses the Twitter API (through the twitter4j library) to search for tweets based on certain criteria and visualize them by date/time. I am imagining that I would break the tweets up into specific intervals (by hour, day, etc) and the number of tweets that was sent during that time would create the magnitude of the shape.
So that is the main idea, but I am having trouble sorting and categorizing the data by the date. From twitter, I am creating an arraylist of tweet objects that have a few parameters, one of which is a Date parameter which comes in as "Fri Mar 30 14:27:17 CDT 2012".
My initial thought was to split this Date into separate peices and recombine it into a string or int that I could then sort with a comparator. Using a series of split commands I got the date information into an INT format of "YYmmDDhhMM" that i then sorted with a comparator.
void setup() {
//Credentials ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("###"); cb.setOAuthConsumerSecret("###"); cb.setOAuthAccessToken("##"); cb.setOAuthAccessTokenSecret("###");
//Make the twitter object and prepare the query Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("insert query here"); query.setRpp(100);
ArrayList tweetdata = new ArrayList();
//Try making the query request. try { 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(); Date d = t.getCreatedAt(); String date = d.toString();
//------------how I split the date to convert to an int-------------------
String date_split[] = date.split(" ");
//split and make numbers of the date
if (date_split[1].equals("Jan") == true) { date_split[1] = "01"; } else if (date_split[1].equals("Feb") == true) { date_split[1] = "02"; } else if (date_split[1].equals("Mar") == true) { date_split[1] = "03"; } else if (date_split[1].equals("Apr") == true) { date_split[1] = "04"; } else if (date_split[1].equals("May") == true) { date_split[1] = "05"; } else if (date_split[1].equals("Jun") == true) { date_split[1] = "06"; } else if (date_split[1].equals("Jul") == true) { date_split[1] = "07"; } else if (date_split[1].equals("Aug") == true) { date_split[1] = "08"; } else if (date_split[1].equals("Sep") == true) { date_split[1] = "09"; } else if (date_split[1].equals("Oct") == true) { date_split[1] = "10"; } else if (date_split[1].equals("Nov") == true) { date_split[1] = "11"; } else if (date_split[1].equals("Dec") == true) { date_split[1] = "12"; }
class AComparator implements Comparator { int compare(Object o1, Object o2) { int score1 = ((tweetObj) o1).returnDate(); int score2= ((tweetObj) o2).returnDate(); return (score1<score2) ? -1 : (score1==score2) ? 0 : 1; } }
class tweetObj { String user; String msg; String loc; String src; int hours; int mins; int sec; int date;
tweetObj(String u, String m, int d) { user = u; msg = m; date = d; }
int returnDate(){ return date; }
}
I thought I could then sort this into a series of arraylists based on it's size, but realized (I may be wrong) that I would have to create hundreds of arraylists manually to recreate all the days and hours over the time period of the tweets, especially since a period of time with no tweets is just as significant to the data as a period of time with many tweets.
here is a simple example of what I mean. (not code, more of a diagram of the data)
date/time number of tweets
apr. 17th, btw 9 and 10 am 6
apr. 17th, btw 10 and 11 am 3
apr. 17th, btw 11 and 12 am 0
apr. 17th, btw 12 and 1 pm 5
apr. 17th, btw 1 and 2 pm 12
Ideally I would like to do this directly with the date object without having to do all the conversion to an int or string but havent found anything online showing how (at least that I can understand.) Anyone have any suggestions?
FYI, I am and architecture student and not particularly experienced with processing or any programming languages but I am learning!
Let me know if you need more information or explanation.