Search code examples
javaprocessingtwitter4j

getting latest tweet and storing it twitter4j


Im new to twitter4j and processing.

I want to get the latest searched word from a list of words. I want this to be stored in the variable which will be queried. so:

PossibleWords[] ={"sad","happy","joyful"}

i would then want a string variable searchString to hold the latest tweeted tweet containing the word from the array. For example, if sad is tweeted just now, i want to get that tweet. So I need to test if any of the words from the array is in the latest tweeted words.

conceptually, I understand this, but any one have any suggestions on programming this? This in processing using twitter4j 3


Solution

  • You should use the Twitter Streaming API, look at the code block that I pasted below:

         private void GetTweetsByKeywords()
         {
    
            TwitterStream twitterStream = new TwitterStreamFactory(config).getInstance();
    
                    StatusListener statusListener = new StatusListener() {
                        private int count = 0;
                        private long originalTweetId = 0;
    
                        @Override
                            public void onStatus(Status status) {
    
                            // Here do whatever you want with the status object that is the       
                             //  tweet you got
    
                            } //end of the onStatus()
             }; //end of the listener
    
             FilterQuery fq = new FilterQuery();
    
             String keywords[] = {"sad","happy","joyful"};
    
             fq.track(keywords);
    
    
             twitterStream.addListener(statusListener);
             twitterStream.filter(fq);
        }