Filtering latest tweets

edited November 2013 in Library Questions

I am trying to filter the latest tweets with the words sad, okay, joyful.

When a tweet which has either word inside it, I would like it to print that tweet. But I would also like there to be a delay in the tweets which are printed. So about a 10 second delay between each tweet. For example:

If a tweet comes through : @joker im so sad today Then I want that to print to the screen, and to the following message

System.out.println("*********************************************a sad tweet");

Then if a tweet comes through after this: @programmer im joyful Then 10 seconds after the last tweet I want that tweet to come through with the following message drawn to the screen.

System.out.println("*********************************************a joyful tweet");

And so on.

Below, I have made some code which allows you to filter the tweets, but im unsure of how to test and print a separate message for each tweet. I tried storing this in an Arraylist and retrieving each tweet with a message, but this doesn't work. Is there a way to do this? Im using processing 2 and twitter4j 3 Any suggestions? Solutions

    void GetTweetsByKeywords()
    {

       List<String>mood = new ArrayList <String>();
       ConfigurationBuilder cb = new ConfigurationBuilder();
       cb.setOAuthConsumerKey("XXXX");
       cb.setOAuthConsumerSecret("XXX");
       cb.setOAuthAccessToken("XXXX");
       cb.setOAuthAccessTokenSecret("XXXX");

       TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
       StatusListener statusListener = new StatusListener()
       {
          private int count = 0;
          private long originalTweetId = 0;

          @Override
          public void onStatus(Status status)
          {

              System.out.println(status.getUser().getName() + " : " + status.getText());

          } //en of the onStatus()
          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice)
          {
              // should really remove deleted tweets here ...
          }

          public void onTrackLimitationNotice(int numberOfLimitedStatuses)
          {
          }

          public void onScrubGeo(long userId, long upToStatusId) {
          // should really remove deleted location information here ...
          }

          public void onStallWarning(StallWarning stallWarning) {
          // should really do something about stalls here ...
          }
          @Override
          public void onException(Exception ex)
          {
            ex.printStackTrace();
          }

      }; //end of the listener
      String keywords[] = {"sad","okay","joyful"};
      for(int i=0; i<keywords.length; i++)
      {
          FilterQuery fq = new FilterQuery();

          fq.track(keywords);

          twitterStream.addListener(statusListener);
          twitterStream.filter(fq);
          mood.add(//here i want to add the filtered tweets);
          System.out.println("Heres a filter :" + mood.get(i));

          if (mood.get(i).equals("sad"))
          {
             System.out.println("*********************************************a sad tweet");
          }
          else if (mood.get(i).equals("joyful"))
          {
            System.out.println("*********************************************a joyfull tweet");
          }
          else if(mood.get(i).equals("okay"))
          {
            System.out.println("*********************************************okay tweet");
          }

    }
 }
Sign In or Register to comment.