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:TwitterCrawler.PrintSampleStream.java

License:Apache License

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

    System.getProperties().put("http.proxyHost", "127.0.0.1");
    System.getProperties().put("http.proxyPort", "8580");

    //final PrintSampleStream pr = new PrintSampleStream();

    try {//  w w  w  .ja  v  a 2s . c o  m
        pr.LinkMongodb();
    } catch (Exception e) {
        e.printStackTrace();
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("7ZVgfKiOvBDcDFpytRWSA")
            .setOAuthConsumerSecret("JmeJVeym78arzmGthrDUshQyhkq6nWA9tWLUKxc")
            .setOAuthAccessToken("321341780-Zy7LptVYBZBVvAeQ5GFJ4aKFw8sdqhWBnvA3pDuO")
            .setOAuthAccessTokenSecret("foi8FnQCeN0J5cdwad05Q6d7dbytFayQn1ZOvmhF6Qc");
    cb.setJSONStoreEnabled(true);

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

    StatusListener listener = new StatusListener() {

        @Override
        public void onException(Exception ex) {
            // TODO Auto-generated method stub
            ex.printStackTrace();
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            // TODO Auto-generated method stub
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());

        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            // TODO Auto-generated method stub
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStatus(Status status) {
            //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());  
            //System.out.println(status);  
            String str = DataObjectFactory.getRawJSON(status);
            try {
                //JSONObject nnstr = new JSONObject(newstr);  
                DBObject dbObject = (DBObject) JSON.parse(str);
                pr.collection.insert(dbObject);
                System.out.println(dbObject);
                pr.count++;
                /* if(pr.count>300000) {  
                pr.mongo.close();  
                System.exit(0);  
                 }  */
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            // TODO Auto-generated method stub
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);

        }

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

        }

    };
    twitterStream.addListener(listener);

    //String[] Track = {"why Obama look like he about to drop the best album of 2013?"}; 
    String[] trackArray = { "Mac", "App", "Store" };
    //trackArray[1] = ;  

    // Create a FilterQuery object and then set the items to track
    FilterQuery filter = new FilterQuery();

    // Create an array of items to track
    //String[] itemsToTrack = {""}; 
    // Set the items to track using FilterQuerys' track method.
    //filter.track(Track); 
    filter.track(trackArray);

    // Assuming you have already created Twitter/TwitterStream object, 
    // use the filter method to start streaming using the FilterQuery object just created.
    twitterStream.filter(filter);

    /* String[] username = {"katyperry"};
     twitterStream.addListener(userlistener);  
     twitterStream.user();*/
    //pr.mongo.close();
}

From source file:twittermongodbapp.CollectTweets.java

static public void collectTweets(DB db, ConfigurationBuilder cf1, twitter4j.Twitter twitter) {

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

    DBCollection collection = db.getCollection("collection");
    DBCollection collection2 = db.getCollection("collection2");
    BasicDBObject document0 = new BasicDBObject();
    collection.remove(document0);/*from   w w w .  j a v a 2  s . c o  m*/
    collection2.remove(document0);
    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {
            if (collection2.getCount() == 15000)
                System.exit(0);
            String json = DataObjectFactory.getRawJSON(status);
            DBObject doc = (DBObject) JSON.parse(json);
            try {
                collection2.insert(doc);
            } catch (Exception e) {
                System.out.println("MongoDB Connection Error : " + e.getMessage());
            }

            System.out.println("Collected tweets :" + collection2.getCount());

        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();

        }

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

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {

        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0);
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0);
        }
    };
    getTopTrends(twitter, twitterStream, listener);
}

From source file:twitterstreamsample.NewJFrame.java

private void ClickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClickActionPerformed
    // TODO add your handling code here:

    String consKey = "Your key";
    String consSecret = "Your Secret";
    String accToken = "Your token";
    String accSecret = "Your secret";

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            textArea.append(status.getUser().getName() + ":" + status.getText());
        }//from  w w w.ja v  a2  s.c o m

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

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

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

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

    };

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

    twitterStream.setOAuthConsumer(consKey, consSecret);

    twitterStream.setOAuthAccessToken(new AccessToken(accToken, accSecret));

    twitterStream.addListener(listener);

    twitterStream.sample();

}

From source file:twittertweetscollection.TwitterTweetsCollection.java

public List<Status> execute() throws TwitterException {

    final List<Status> statuses = new ArrayList();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("I7WYWBE6eSNKdV4FYSyiuP0uk")
            .setOAuthConsumerSecret("oft7Y6WcUCvxovPU5modCZ3BOlHH2z9Px73UkFMILN4ltBhOni")
            .setOAuthAccessToken("3524247253-o7zXFn3r8PwPb4IUTYqHRQzVabTzfoFn9Ck7FzW")
            .setOAuthAccessTokenSecret("V7zkLiJmF0vsdX8rUdkXo3q1ha452vKqVtB5OoCuQWAgR");

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

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            statuses.add(status);/*from   w  ww.  j  a  v a2s. co m*/
            System.out.println(statuses.size() + ":" + status.getText());
            if (statuses.size() > 100) {
                synchronized (lock) {
                    lock.notify();
                }
                System.out.println("unlocked");
            }
        }

        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 sw) {
            System.out.println(sw.getMessage());

        }
    };

    //FilterQuery fq = new FilterQuery();
    //String keywords[] = { "SuperBowlSunday"};

    //fq.track(keywords);

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

    try {
        synchronized (lock) {
            lock.wait();
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("returning statuses");
    twitterStream.shutdown();
    return statuses;
}

From source file:twittynumnum.FutureListener.java

public void listen(String[] keywords) {

    StatusListener listener = new StatusListener() {
        @Override//from w w  w  . j av a  2 s.co  m
        public void onException(Exception arg0) {

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {

        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {

        }

        @Override
        public void onStatus(Status status) {
            User user = status.getUser();
            String username = user.getScreenName();
            String profileLocation = user.getLocation();
            long tweetId = status.getId();
            String content = status.getText();
            Date tweetDate = status.getCreatedAt();
            out.print(tweetId + "\t" + tweetDate + "\t" + username + "\t" + profileLocation + "\t"
                    + content.replaceAll("\n", "/n") + "\n");
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {

        }

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

    };

    FilterQuery filter = new FilterQuery();

    filter.track(keywords);
    twitterStream.addListener(listener);
    twitterStream.filter(filter);
}

From source file:ws.project.languagebasedlexiconanalisys.TwitterStreamAnalizer.java

void parseStream() throws IOException {
    //Inizializza file JSON
    FileWriter file = new FileWriter("data.json");
    JSONObject obj = new JSONObject();
    final JSONArray data = new JSONArray();
    obj.put("data", data);
    file.write(obj.toJSONString());//from   w ww . j av a 2s.  c om
    file.flush();
    file.close();

    SimpleDateFormat currentDate = new SimpleDateFormat();
    currentDate.applyPattern("dd-MM-yyyy");
    final String currentDateStr = currentDate.format(new Date());

    ConfigurationBuilder cfg = new ConfigurationBuilder();
    cfg.setOAuthAccessToken("3065669171-9Hp3VZbz7f0BCsvWWFfgywgqimSIp1AlT98745S");
    cfg.setOAuthAccessTokenSecret("AUmg0AdhHzMXisnP1WV7Wnsw5amWFQPyIojI5aBG5qV4A");
    cfg.setOAuthConsumerKey("arieQRhL2WwgRFfXFLAJp5Hkw");
    cfg.setOAuthConsumerSecret("NvmWqgN1UKKPUWoh9d9Z2PuQobOah8IR5faqX2WjDGBL053sWE");

    StatusListener listener;
    listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            SimpleDateFormat tweetDate = new SimpleDateFormat();
            tweetDate.applyPattern("dd-MM-yyyy");
            String tweetDateStr = tweetDate.format(status.getCreatedAt());

            try {
                indexer.openWriter(currentDateStr);
            } catch (IOException ex) {
                Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
            }

            //CHIUDO se cambio giorno ma non ho raggiunto l'1%
            if (!tweetDateStr.equals(currentDateStr)) {
                try {
                    indexer.closeWriter();
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                }
                writeOnJson(currentDateStr);
                System.out.println("Giorno successivo, completato senza aver raggiunto 1%");
                System.exit(0);
            }
            tot_count++;

            if (status.getLang().equals("it")) {
                it_count++;
                try {
                    indexer.addTweet(id, status.getText());
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            try {
                indexer.closeWriter();
            } catch (IOException ex) {
                Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice sdn) {
        }

        @Override
        public void onTrackLimitationNotice(int i) {
        }

        @Override
        public void onScrubGeo(long l, long l1) {
        }

        @Override
        public void onStallWarning(StallWarning sw) {
        }

        @Override
        public void onException(Exception excptn) {
            TwitterException exc = (TwitterException) excptn;
            if (exc.exceededRateLimitation()) {
                try {
                    indexer.closeWriter();
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                }
                writeOnJson(currentDateStr);
                System.out.println("1% raccolto, dati raccolti. Amen");
            }
        }

    };

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

    indexer.addIndex(currentDateStr);
    indexer.openWriter(currentDateStr);
    twitterStream.sample();

}

From source file:xdsoft.cloudera.flume.twitter.source.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets.//w  ww  .  j  a  v a2  s  .  co 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
            logger.debug(status.getUser().getScreenName() + ": " + status.getText());

            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();
}