What is the easiest way to count the number of filtered statuses that come in from a twitter stream? I know I can filter statuses using the FilterQuery like so:
FilterQuery fq = new FilterQuery();
String[] array = { "twitter" };
fq.track(array);
twitterStream.filter(fq);
But how would I be able count the number of statuses that come in containing the word twitter? I have tried numerous different ways which have all but failed and only led to all statuses showing up. I even tried to parse json to filter the "text" part in order to count but it became too confusing and did not work.
As you are already filtering for statuses that contain 'twitter' all you need to do is increment a count
in the StatusListener#onStatus(Status)
method, e.g.:
final AtomicInteger count = new AtomicInteger();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
count.getAndIncrement();
}
// omitted...
}
twitterStream.addListener(listener);
twitterStream.filter(fq);
// wait (to allow statuses to be received) then halt the steam...
System.out.println("received " + count.get() + "statuses in total");
Alternatively you could create a CountingStatusListener
that provided you with the count when you were done processing the stream.
Regarding your comment:
For example I want to run the streamer and have it tell me that 7 tweets or whatever with my filtered word in them have come in since the time I ran the streamer.
You probably already know this but the streaming-api provides a real-time view of statuses flowing through Twitter (albeit a sample) so when you stop processing a stream you will miss any statuses sent between the time you stop until you start processing again.
I hope that helps.