Read_data.java Source code

Java tutorial

Introduction

Here is the source code for Read_data.java

Source

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author newuser
 */
public class Read_data {
    //Insert your customer key
    private final static String customer_key = "[Insert Customer Key here]";
    //Insert yout customer secret
    private final static String customer_secret = "[Insert Customer Secret here]";
    //Insert your access tokem 
    private final static String access_token = "[Insert Access Token here]";
    //INsert your access token secret
    private final static String access_token_secret = "[Insert Access Token Secret here]";
    private static final Object lock = new Object();
    private static int count = 0;

    /**
     * @param args the command line arguments
     * @throws twitter4j.TwitterException
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws TwitterException, FileNotFoundException, IOException {
        // TODO code application logic here

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey(customer_key).setOAuthConsumerSecret(customer_secret)
                .setOAuthAccessToken(access_token).setOAuthAccessTokenSecret(access_token_secret);

        TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

        StatusListener statusListener = new StatusListener() {

            @Override
            public void onStatus(Status status) {

                //get the place if it is not null
                if (status.getPlace() != null) {

                    try {

                        String place = status.getPlace().getName();

                        System.out.println("Place: " + place + "\t\t\t" + "Tweet: " + status.getText());

                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Read_data.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

            @Override

            public void onDeletionNotice(StatusDeletionNotice sdn) {
                System.out.print("");
            }

            @Override
            public void onTrackLimitationNotice(int i) {
                System.out.print("");
            }

            @Override
            public void onScrubGeo(long l, long l1) {
                System.out.print("");
            }

            @Override
            public void onStallWarning(StallWarning sw) {
                System.out.println(sw);
            }

            @Override
            public void onException(Exception ex) {
                System.out.println(ex);
            }
        };

        FilterQuery fq = new FilterQuery();

        //Stream tweets with these keywords. Replace for your tweets that you are looking for.
        String keywords[] = { "lol", "lls", "lmao", "llf" };

        fq.track(keywords);

        twitterStream.addListener(statusListener);
        twitterStream.filter(fq);

        try {
            synchronized (lock) {
                lock.wait();
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        System.out.println("returning statuses");
        // twitterStream.shutdown();
    }
}