How to Search Tweets using Twitter4j Java API


In a recent SEO related project, I used Twitter4J Java API to download 3 million plus tweets. Below is the proof of concept code. There are only three major steps:

1. Download the latest Twitter4J JAR file from http://twitter4j.org/en/ and add into your project library or class path.

2. Create Twitter App by visting https://apps.twitter.com/ Click Create New App button and fill the form by providing app name, URL and app brief description and submit clicking on "Create New Twitter Applicaton" button.





Once the application is created, click the application name on dashboard and go to Key and Access Toekn tab. There would see Consumer Key (API Key), Consumer Secret (API Secret). Click on Generate Access Token and Token secret button, it would generate Access Token and Access Token Secret. These 4 things are what you need when making the API call.

3. Just use below code, as-is, it shall fetch the tweets. Modify the code as per your requirements. Its self explanatory, if you have any questions, feel free to ask under commends. Here is the code sample:

package com.bitspedia.tools;

import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

import java.util.ArrayList;
import java.util.List;

public class TweetsFetcher {
    public static final int TWEETS_COUNT = 20;
    public static final String TWITTER_QUERY_FILTERS = " -filter:retweets AND -filter:replies AND -filter:images AND -filter:videos";
    public static final String NAME = "devtrainings.com"; // TWITTER account
    public static final String CONSUMER_KEY = "write consumer key here ";
    public static final String CONSUMER_SECRET = "write consume secret here";
    public static final String ACCESS_TOKEN = "write access token here";
    public static final String ACCESS_TOKEN_SECRET = "write access token secret here";
    public static final Twitter TWITTER;

    static {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(false)
                .setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET)
                .setOAuthAccessToken(ACCESS_TOKEN)
                .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
        TwitterFactory tf = new TwitterFactory(cb.build());
        TWITTER = tf.getInstance();
    }

    public static void main(String[] args) {
        TweetsFetcher tweetsFetcher = new TweetsFetcher();
        List statusList = tweetsFetcher.downloadTweets(" care ");
        for(Status status : statusList) {
            System.out.println(status.getId() + " | " + status.getUser().getScreenName() + " | " + status.getText());
        }
    }

    public List downloadTweets(String keyword) {
        List statuses = new ArrayList<>();
        try {
            String queryString = keyword + TWITTER_QUERY_FILTERS;
            Query query = new Query(queryString);
            query.lang("en");
            query.setCount(TWEETS_COUNT);
            QueryResult result = TWITTER.search(query);
            statuses = result.getTweets();
            return statuses;
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        return statuses;
    }

}

When I run the code, below is the list of tweets returned by API.

894122656773152770 | thelandee | we don't care https://t.co/ijItD5hoaV
894122646656385024 | benjochannon | I Don't Know How To Explain To You That You Should Care About Other People https://t.co/cczOr8KxpR
894122644609650689 | nabilahazmie | Be who you want to be and not care about what others think 💙 https://t.co/MoJ3CPrebD
894122640264151041 | nydafarah | The less i know the less I'll care.
894122638812913664 | smilesmileposi | You don't have to know exactly how to make people feel better. At least showing them you care will help!
894122630164299777 | peythepotatoooo | only those who care a'right https://t.co/i7BCe5Lcxw
894122629900140546 | AtiliusRegulus | Actually zero people care about your "bi-erasure," Aaron Carter trend. Sickos are sickos.
894122627354296320 | BullMelford | The Bull can take care of your complete old English #wedding day, from the ceremony to the reception. Tweet us today! #LongMelford #Suffolk
894122597524398080 | MsBoss48536280 | LOL trust me fetty do not care about them talking about masika kalysha because that is not his ex that's his sister-in-law
894122589664272384 | HaileyOliver5 | Holy fuck I don't care about ANY of these we fest, ice fest or harbour fest snaps my god u r all annoying
894122583695605760 | liberty201755 | But i would look because anyone calling showing what federalism and lack of care gets you, anti social, is trying to hide something
894122582429114368 | rvmemberme | sans surprise he doesn't care at all
894122573633552384 | glcnigeria | 🎵Hands in the air now, wave it like you don't care...🎵

#TheDovesPraise 
#DaysOfGlory
#Day6
894122572467621888 | andrew4mk | Further signs of abuses in Venezuela. Don't care if you are right or left. This is wrong. All should speak out https://t.co/NqxtmUKtJ1
894122559238807552 | CllrBSilvester | Retweeted Annie Greek UK lover (@annie_ioanna):

I don't care if you are white, brown, yellow, blue or purple..if... https://t.co/fJP7XP5ei2
894122541203300352 | chyato | Anyone care for Yoko's original Taiwanese pork leg stew (豬滷腳)? [偷笑] https://t.co/uTvmyeyrSa
894122525596291072 | MrWoodo | They do it just to make you loudly tell everyone in the room how much "you don't care but..." you gammon-faced old… https://t.co/7H6Mi9cvm7
894122523813720064 | GonzalezMaryssa | If I can't tell you how I really feel about you or about something you said or did... I just don't care about you.
894122518369456128 | ixvs3 | I care too much. That's how I get hurt.

There is lot of other information about tweet that you can get using other getter methods of Status class e.g. user id, date, etc.


Comments

  1. i am such a dummy when it comes to dealing with these kind of programs or systems. this post has helped me a great deal and i am so thankful for it. keep posting

    ReplyDelete
  2. Woah, how hard this code is. I really got astonished to see that this code is quite difficult and hard to understand. Great effort you have put on.

    ReplyDelete

Post a Comment