Example usage for twitter4j StatusListener StatusListener

List of usage examples for twitter4j StatusListener StatusListener

Introduction

In this page you can find the example usage for twitter4j StatusListener StatusListener.

Prototype

StatusListener

Source Link

Usage

From source file:crawling.FoundUsersByStreamHashtag.java

License:Apache License

public static void main(String[] args) throws TwitterException {

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            // my shutdown code here
            // Record the user status
            idOut.println("Final status -------------------:");
            for (Long id : discoveredUsers.keySet()) {
                idOut.println(id + "," + discoveredUsers.get(id));
            }//from  ww w  .ja v  a 2s .  c o  m
            idOut.close();
        }
    });

    if (args.length != 1) {
        System.out.println("Usage: java twitter4j.examples.PrintFilterStreamHashtag hashtag");
        System.exit(-1);
    }
    /*if(args.length == 2){
       // Preload the collected user IDs
       preloadID(args[1]);
    }*/

    //buildStartTime();

    File file = new File(fileName);
    InputStream is = null;

    try {
        if (file.exists()) {
            is = new FileInputStream(file);
            prop.load(is);
        } else {
            System.out.println(fileName + " doesn't exist!");
            System.exit(-1);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(-1);
    }

    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            CheckUser(status);

            /*storeATweet(status);
            tweets += 1;
            if (tweets % 1000 == 0){
               System.out.println("We now have tweets: " + tweets);
            }*/
        }

        private void CheckUser(Status status) {
            // TODO Auto-generated method stub
            Long id = status.getUser().getId();
            String username = status.getUser().getScreenName();
            String realname = status.getUser().getName();
            String text = status.getText();
            Date date = status.getCreatedAt();
            if (discoveredUsers.containsKey(id)) {
                //System.out.println("Already found this user: " + id);
                long num = discoveredUsers.get(id);
                discoveredUsers.put(id, num + 1);
            } else {
                discoveredUsers.put(id, (long) 1);
                storeUserID(status);
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

        }
    };

    try {
        FileWriter outFile = new FileWriter("discoveredUser" + args[0] + ".txt", true);
        idOut = new PrintWriter(outFile);
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    TwitterStream twitterStream = getOAuthTwitterStream();
    twitterStream.addListener(listener);

    FilterQuery query = new FilterQuery();
    String[] track = { args[0] };
    //String[] track = {"#BaltimoreRiots"};
    query.track(track);
    twitterStream.filter(query);
}

From source file:crawling.PrintFilterStreamByFile.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.PrintFilterStreamByFile ID_File ");
        System.exit(-1);//from   ww w  . j  a  va  2  s  . c o m
    }

    idFile = args[0];
    tweetFile = idFile.split("\\.")[0] + "-tweets.txt";

    try {
        FileWriter outFile = new FileWriter(tweetFile, true);
        out = new PrintWriter(outFile);
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            storeTweet(status.getUser().getId() + "::" + status.getText());
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

        }
    };

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);

    // String users = "700896487,701053075,636744543";
    // String[] split = users.split(",");

    buildFollowList();

    long[] followArray = new long[follow.size()];
    for (int i = 0; i < follow.size(); i++) {
        followArray[i] = follow.get(i);
    }
    String[] trackArray = track.toArray(new String[track.size()]);

    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.filter(new FilterQuery(0, followArray, trackArray));
}

From source file:crawling.PrintFilterStreamGeo.java

License:Apache License

public static void main(String[] args) throws TwitterException {

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            /* my shutdown code here */
            // Record the user status
            idOut.println("Final status -------------------:");
            for (Long id : discoveredUsers.keySet()) {
                idOut.println(id + "," + discoveredUsers.get(id));
            }//  w  ww .ja va 2s.  co m
            idOut.close();
        }
    });

    if (args.length < 5) {
        System.out.println("Usage: java twitter4j.examples.PrintFilterStreamGeo long1 lati1 long2 lati2 CITY");
        System.exit(-1);
    }
    if (args.length == 6) {
        // Preload the collected user IDs
        preloadID(args[5]);
    }

    File file = new File(fileName);
    InputStream is = null;

    try {
        if (file.exists()) {
            is = new FileInputStream(file);
            prop.load(is);
        } else {
            System.out.println(fileName + " doesn't exist!");
            System.exit(-1);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(-1);
    }

    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            CheckUser(status);
        }

        private void CheckUser(Status status) {
            // TODO Auto-generated method stub
            Long id = status.getUser().getId();
            /*String username = status.getUser().getScreenName();
            String realname = status.getUser().getName();
            String text = status.getText();
            Date date = status.getCreatedAt();*/
            if (discoveredUsers.containsKey(id)) {
                //System.out.println("Already found this user: " + id);
                long num = discoveredUsers.get(id);
                discoveredUsers.put(id, num + 1);
            } else {
                discoveredUsers.put(id, (long) 1);
                storeUserID(status);
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

        }
    };

    try {
        FileWriter outFile = new FileWriter("discoveredUser" + args[4] + ".txt", true);
        idOut = new PrintWriter(outFile);
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    TwitterStream twitterStream = getOAuthTwitterStream();
    twitterStream.addListener(listener);
    /*ArrayList<Long> follow = new ArrayList<Long>();
    ArrayList<String> track = new ArrayList<String>();
            
    long[] followArray = new long[follow.size()];
    for (int i = 0; i < follow.size(); i++) {
    followArray[i] = follow.get(i);
    }
    String[] trackArray = track.toArray(new String[track.size()]);*/

    // Geographic location
    double[][] location = new double[2][2];
    //location[0] = new double[4];
    //location[1] = new double[4];
    location[0][0] = Double.parseDouble(args[0]);
    location[0][1] = Double.parseDouble(args[1]);
    location[1][0] = Double.parseDouble(args[2]);
    location[1][1] = Double.parseDouble(args[3]);
    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    //twitterStream.filter(new FilterQuery(0, followArray, trackArray, location));
    FilterQuery query = new FilterQuery();
    query.locations(location);
    twitterStream.filter(query);
}

From source file:crawling.PrintSampleStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    //TwitterStream twitterStream = new TwitterStreamFactory().getInstance();

    TwitterStream twitterStream = getOAuthTwitterStream();

    try {/*  ww w .  jav  a2 s .c  o m*/
        FileWriter outFile = new FileWriter("sampledUsers" + ".txt", true);
        idOut = new PrintWriter(outFile);
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    StatusListener listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            ++tweetsCount;
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            Long id = status.getUser().getId();
            /*String username = status.getUser().getScreenName();
            String realname = status.getUser().getName();
            String text = status.getText();
            Date date = status.getCreatedAt();*/
            if (discoveredUsers.containsKey(id)) {
                //System.out.println("Already found this user: " + id);
                long num = discoveredUsers.get(id);
                discoveredUsers.put(id, num + 1);
            } else {
                discoveredUsers.put(id, (long) 1);
                storeUserID(status);
            }

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.sample();
}

From source file:crosstreams.twitter.TwitterStreamFileWriter.java

License:Mozilla Public License

/**
 * Start crawling tweets// w w  w.  j a v  a2s. c om
 * @param args
 * @throws TwitterException
 */
public static void main(String[] args) throws TwitterException {

    System.err.println("### Twitter Stream Writer ###");
    System.err.println("Saves tweets from the Spritzer/Gardenhose Stream to a series of files");
    System.err.println(
            "Command: crosstreams.twitter.TwitterStreamFileWriter <saveFolder> <twitterusername> <twitterpassword> <numberoftweetstostoreperfile>(optional)");
    System.err.println("   saveFolder: Where the tweets will be downloaded to");
    System.err.println("   twitterusername: The username of the twitter account to use for downloading tweets");
    System.err.println("   twitterpassword: The password of the twitter account to use for downloading tweets");
    System.err.println(
            "   numberoftweetstostoreperfile: The total number of tweets to write to a file before closing that file and opening a new one (Integer) (defaults=1000000)");
    System.err.println("Optional System Properties (-D):");
    System.err.println("   http.proxyhost: The proxy host to use if needed");
    System.err.println("   http.proxyport: The proxy port to use if needed");
    System.err.println("   email: An email address to send alerts to if an error is encountered");
    System.err.println("   emailconf: An file containing the javax.mail configuration");
    System.err.println(
            "   emailonvalidate: true/false - should I send an email when a file is correctly validated rather than only when it fails? (default=false)");

    if (args.length <= 1 || args.length >= 5) {
        System.err.println("Example:");
        System.err.println(
                "java -Demail=\"MYEMAIL@HOST.COM\" -Demailconf=\"./javamail.conf\" -Demailonvalidate=\"true\" -jar TwitterStreamFileCrawler.jar ./ MYUSERNAME MYPASSWORD 100000");
        System.err.println("Don't forget to modify ./javamail.conf to contain your email server host");
        System.exit(0);
    }

    // user inputs
    String saveFolder = args[0];
    String username = args[1];
    String password = args[2];
    final int numberOfTweetsToStorePerFile;
    if (args.length > 2)
        numberOfTweetsToStorePerFile = Integer.parseInt(args[3]);
    else
        numberOfTweetsToStorePerFile = 1000000;
    String proxyhost = System.getProperty("http.proxyhost");
    String proxyport = System.getProperty("http.proxyport");
    final String email = System.getProperty("email");
    final String emailconf = System.getProperty("emailconf");

    // define the user account in use and proxy settings if needed
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    if (proxyhost != null && proxyport != null) {
        cb.setHttpProxyHost(proxyhost);
        cb.setHttpProxyPort(Integer.parseInt(proxyport));
    }
    cb.setUser(username);
    cb.setPassword(password);

    if (!saveFolder.endsWith("/") && !saveFolder.endsWith("\\")) {
        saveFolder = saveFolder + System.getProperty("file.separator");
    }
    final String finalSaveFolder = saveFolder;

    // Twitter4J Stream - the type of stream is set automatically, i.e. Gardenhose if you have it, Spritzer otherwise.
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    // The status listener is the important bit, this fires when a new tweet arrives.
    StatusListener listener = new StatusListener() {

        /** The status listener holds a writer to save content to **/
        BufferedWriter statusWriter = null; // the tweets go here
        BufferedWriter logWriter = null; // we write any delete requests or error messages here

        /** We store a fixed number of Tweets in each file **/
        int numberInThisFile = numberOfTweetsToStorePerFile;
        int numberPerFile = numberOfTweetsToStorePerFile;

        String currentFilename;
        int numerrors = 0;

        /**
         * A new tweet has arrived
         */
        public void onStatus(Status status) {
            if (numberInThisFile >= numberPerFile) {
                // closing and opening of new files
                try {
                    if (statusWriter != null) {

                        statusWriter.close();
                        logWriter.close();
                        validateJSONFile(currentFilename, numberPerFile);
                    }
                    Long currentTime = System.currentTimeMillis();

                    currentFilename = finalSaveFolder + currentTime.toString() + ".json.gz";
                    statusWriter = new BufferedWriter(new OutputStreamWriter(
                            new GZIPOutputStream(new FileOutputStream(currentFilename)), "UTF-8"));
                    logWriter = new BufferedWriter(new OutputStreamWriter(
                            new GZIPOutputStream(
                                    new FileOutputStream(finalSaveFolder + currentTime.toString() + ".log.gz")),
                            "UTF-8"));
                    numberInThisFile = 0;
                    numerrors = 0;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            numberInThisFile++;
            // write the JSON - note that I added the getJSON() method to the Twitter4J status object
            // this is why the Twitter4j sources are included rather than importing the jar.
            try {
                Object s = status.getJSON();
                statusWriter.write(status.getJSON().toString() + '\n');
                statusWriter.flush();
            } catch (Exception e) {
                e.printStackTrace();
                numerrors++;
                if (emailconf != null && email != null && numerrors < 5)
                    Mail.mail(emailconf, email, email, "Twitter Stream Writer Alert - Write Failed",
                            "An IOException was thrown when calling statusWriter.write()." + '\n'
                                    + e.getMessage() + '\n'
                                    + "The current file will be closed and a new file will be created.");
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            try {
                logWriter.write("DEL: " + statusDeletionNotice.getStatusId() + " "
                        + statusDeletionNotice.getUserId() + '\n');
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            try {
                logWriter.write("LIMIT: " + numberOfLimitedStatuses + '\n');
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void onScrubGeo(long userId, long upToStatusId) {
            try {
                logWriter.write("SCRUBGEO: " + userId + " " + upToStatusId + '\n');
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void onException(Exception ex) {
            if (logWriter == null)
                return;
            try {
                logWriter.write("ERR: " + ex.getLocalizedMessage() + '\n');
                logWriter.flush();
                if (statusWriter != null) {
                    statusWriter.close();
                    statusWriter = null;
                    logWriter.close();
                    validateJSONFile(currentFilename, numberPerFile);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            //ex.printStackTrace();
        }
    };
    if (emailconf != null && email != null)
        Mail.mail(emailconf, email, email, "Twitter Stream Writer Info - Writer has started",
                "The Gardenhose Writer has begun crawling the stream (this email indicates that you will recieve alerts if something goes wrong.");
    twitterStream.addListener(listener);
    twitterStream.sample();
}

From source file:datasite.DataSite.java

public static void main(String[] args) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);//from  w  w w . j av  a  2 s. com
    cb.setOAuthConsumerKey("ConsumerKey");
    cb.setOAuthConsumerSecret("ConsumerSecret");
    cb.setOAuthAccessToken("AccessToken");
    cb.setOAuthAccessTokenSecret("AccessTokenSecret");

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

    StatusListener listener;
    listener = new StatusListener() {

        @Override
        public void onException(Exception arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatus(Status status) {
            User user = status.getUser();

            // gets Username
            String username = status.getUser().getScreenName();

            try (PrintWriter out = new PrintWriter(
                    new BufferedWriter(new FileWriter("tweet_input\\tweets.txt", true))))

            {
                //out.println(username);

                String content = status.getText();
                out.println(content + "\n");
                //System.out.append( content +"\n");

            } catch (IOException ex) {
                Logger.getLogger(DataSite.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStallWarning(StallWarning sw) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    };

    FilterQuery fq = new FilterQuery();

    String keywords[] = { "africa" };

    fq.track(keywords);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);
    //twitterStream.();
}

From source file:datasite.SortedUnique.java

public static void main(String[] args) {
    ConfigurationBuilder confb = new ConfigurationBuilder();
    confb.setDebugEnabled(true);/*w  w w. j a  v  a  2s .  com*/
    confb.setOAuthConsumerKey("Consumer Key");
    confb.setOAuthConsumerSecret("Consumer Secret");
    confb.setOAuthAccessToken("Access Token");
    confb.setOAuthAccessTokenSecret("Access Token Secret");

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

    StatusListener listener;
    listener = new StatusListener() {

        @Override
        public void onException(Exception arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatus(Status status) {

            PrintStream out = null;
            try {
                User user = status.getUser();
                // gets Username

                String username = status.getUser().getScreenName();
                String content = status.getText();
                Set<String> userWrds = new HashSet<String>(); // HashSet implements Set
                Scanner sc = new Scanner(content + user);
                while (sc.hasNext()) {
                    String word = sc.next();
                    userWrds.add(word);

                }
                out = new PrintStream(new FileOutputStream("tweet_output\\unique.txt", true));
                Iterator<String> iter = userWrds.iterator();
                for (int i = 1; i <= 1 && iter.hasNext(); i++)
                    //System.out.println(iter.next());
                    out.println(userWrds.size());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(SortedUnique.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                out.close();
            }

        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStallWarning(StallWarning sw) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    };
    FilterQuery fq = new FilterQuery();

    String keywords[] = { "africa" };

    fq.track(keywords);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);
    //twitterStream.();
}

From source file:de.botshield.CaptureFilterStream.java

License:Apache License

public void initializeStreamListener() {

    listener = new StatusListener() {
        @Override/*  w  w w  .j  a  v  a2  s .  c  om*/
        public void onStatus(Status status) {
            if (isBlnWritetoDB()) {
                System.out.println("*********************** Writing tweet into db! *********************** ");
                dbConn.insertStatus(status);
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.err.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.err.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            System.err.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            System.err.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            // TODO: sauberer Abbau der DB Connection bei Programmende
            // hier kein closeConnection, da jede SQLException sonst alle
            // zukuenftigen Schreibprozess unmoeglich macht
            // dbConn.closeConnection();
            ex.printStackTrace();
        }
    };

}

From source file:de.fhm.bigdata.projekt.dataimport.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets./*from   w  w  w.  j a v  a2s .  c  o  m*/
 */
@Override
public void start() {
    // The channel is the piece of Flume that sits between the Source and Sink,
    // and is used to process events.
    final ChannelProcessor channel = getChannelProcessor();

    final Map<String, String> headers = new HashMap<String, String>();

    // The StatusListener is a twitter4j API, which can be added to a Twitter
    // stream, and will execute methods every time a message comes in through
    // the stream.
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes in.
        public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the headers and
            // the raw JSON of a tweet
            // shouldn't log possibly sensitive customer data
            logger.debug("tweet arrived");

            headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime()));
            Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers);

            channel.processEvent(event);
        }

        // This listener will ignore everything except for new tweets
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}",
            new String[] { consumerKey, accessToken });
    // Set up the stream's listener (defined above),
    twitterStream.addListener(listener);

    // Set up a filter to pull out industry-relevant tweets
    if (keywords.length == 0) {
        logger.debug("Starting up Twitter sampling...");
        twitterStream.sample();
    } else {
        logger.debug("Starting up Twitter filtering...");

        FilterQuery query = new FilterQuery().track(keywords);
        twitterStream.filter(query);
    }
    super.start();
}

From source file:de.jetwick.tw.NewClass.java

License:Apache License

/**
 * A thread using the streaming API./*w  ww.  j av a2 s.  c  o  m*/
 * Maximum tweets/sec == 50 !! we'll lose even infrequent tweets for a lot keywords!
 */
public Thread streaming(final String token, final String tokenSecret) {
    return new Thread() {

        StatusListener listener = new StatusListener() {

            int counter = 0;

            public void onStatus(Status status) {
                if (++counter % 20 == 0)
                    log("async counter=" + counter);
                asyncMap.put(status.getId(), status.getText());
            }

            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                log("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                log("Got track limitation notice:" + numberOfLimitedStatuses);
            }

            public void onScrubGeo(int userId, long upToStatusId) {
                log("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
            }

            public void onException(Exception ex) {
                ex.printStackTrace();
            }
        };

        public void run() {
            TwitterStream twitterStream = new TwitterStreamFactory()
                    .getInstance(new AccessToken(token, tokenSecret));
            twitterStream.addListener(listener);
            twitterStream.filter(new FilterQuery(0, null, new String[] { queryTerms }, null));
        }
    };
}