Example usage for twitter4j TwitterStream addListener

List of usage examples for twitter4j TwitterStream addListener

Introduction

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

Prototype

TwitterStream addListener(StreamListener listener);

Source Link

Usage

From source file:de.botshield.CaptureFilterStream.java

License:Apache License

public void execute(long[] followArray, String[] trackArray) {
    // try to set up new stream, when the listener fails due to HTTP timeout
    // for example

    this.initializeStreamListener();

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    try {/*from  w  w w . ja v  a  2  s.c  o  m*/
        // filter() method internally creates a thread which manipulates
        // TwitterStream and calls these adequate listener methods
        // continuously.
        twitterStream.filter(new FilterQuery(0, followArray, trackArray));
    } catch (Exception ex) {
        twitterStream.removeListener(listener);
        twitterStream.cleanUp();
    }
}

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

License:Apache License

/**
 * A thread using the streaming API./*from  www  . j a  v  a2  s  .co 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));
        }
    };
}

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

License:Apache License

public TwitterStream streamingTwitter(Collection<String> track, final Queue<JTweet> queue)
        throws TwitterException {
    String[] trackArray = track.toArray(new String[track.size()]);
    TwitterStream stream = new TwitterStreamFactory().getInstance(twitter.getAuthorization());
    stream.addListener(new StatusListener() {

        @Override/*from w  w w  .  ja v  a2  s  .  c  o  m*/
        public void onStatus(Status status) {
            // ugly twitter ...
            if (Helper.isEmpty(status.getUser().getScreenName()))
                return;

            if (!queue.offer(new JTweet(toTweet(status), new JUser(status.getUser()))))
                logger.error("Cannot add tweet as input queue for streaming is full:" + queue.size());
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            logger.error("We do not support onDeletionNotice at the moment! Tweet id: "
                    + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            logger.warn("onTrackLimitationNotice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onException(Exception ex) {
            logger.error("onException", ex);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
        }
    });
    stream.filter(new FilterQuery(0, new long[0], trackArray));
    return stream;
}

From source file:de.twitterlivesearch.api.TwitterLiveSearchFactory.java

License:Apache License

private static TwitterLiveSearch configureTwitter(AbstractConfiguration configuration) {
    TwitterLiveSearch twitter = null;/*from   ww w  . jav  a2 s . c  o m*/
    try {
        // several important variables are initialized here
        twitter = new TwitterLiveSearch();
        TweetHolder tweetHolder = new TweetHolder();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
                AnalyzerMapping.getInstance().ANALYZER_FOR_DELIMITER);
        TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
        Directory directory;

        // confguration part: TwitterLiveSearch is configured here
        // according to the config
        if (configuration.getDirectoryConfig() == DirectoryConfig.RAM) {
            directory = new RAMDirectory();
            log.trace("initialized RAM-Directory");
        } else {
            directory = FSDirectory.open(Paths.get(configuration.getDirectory()));
            log.trace("initialized FS-Directory on path " + configuration.getDirectory());
        }
        IndexWriter iwriter = new IndexWriter(directory, indexWriterConfig);
        Searcher searcher = new Searcher(directory);

        twitterStream.addListener(new TwitterStreamListener(directory, tweetHolder, iwriter, searcher));
        if (configuration.getStreamConfig() == StreamConfig.USER_STREAM) {
            twitterStream.user();
        } else if (configuration.getStreamConfig() == StreamConfig.GARDENHOSE) {
            twitterStream.sample();
        }

        // set everything needed in TwitterLiveSearch
        twitter.setCurrentDirectory(directory);
        twitter.setIndexWriter(iwriter);
        twitter.setTweetHolder(tweetHolder);
        twitter.setTwitterStream(twitterStream); // Referenz auf
        // TwitterLiveSearch
        twitter.setSearcher(searcher);

    } catch (IOException e) {
        e.printStackTrace();

    }

    return twitter;
}

From source file:demo.Investigation.java

License:Apache License

public static void main(String[] args) throws Exception {
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();

    TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance();
    twStream.addListener(new MyStatusListener());
    //String[] track = {"#daihyo"};
    //#JPNvs#COL//from   w  w  w. j ava2s  . c  o  m
    //String[] track = {"#tvasahi,#daihyo,#jfa,#tvasahi_soccer,#JPN,#COL"};//soccer
    //String[] track = {"#nhk,#tvtokyo,#etv,#ntv,#tvasahi,#tbs,#fujitv,#tokyomx"};//TV
    String[] track = { "#nhk" };
    FilterQuery query = new FilterQuery();
    query.track(track);
    twStream.filter(query);
}

From source file:druid.examples.twitter.TwitterSpritzerFirehoseFactory.java

License:Open Source License

@Override
public Firehose connect() throws IOException {
    final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() {
        @Override/*from   w  ww .  j av  a2 s .co m*/
        public void onConnect() {
            log.info("Connected_to_Twitter");
        }

        @Override
        public void onDisconnect() {
            log.info("Disconnect_from_Twitter");
        }

        /**
         * called before thread gets cleaned up
         */
        @Override
        public void onCleanUp() {
            log.info("Cleanup_twitter_stream");
        }
    }; // ConnectionLifeCycleListener

    final TwitterStream twitterStream;
    final StatusListener statusListener;
    final int QUEUE_SIZE = 2000;
    /** This queue is used to move twitter events from the twitter4j thread to the druid ingest thread.   */
    final BlockingQueue<Status> queue = new ArrayBlockingQueue<Status>(QUEUE_SIZE);
    final LinkedList<String> dimensions = new LinkedList<String>();
    final long startMsec = System.currentTimeMillis();

    dimensions.add("htags");
    dimensions.add("lang");
    dimensions.add("utc_offset");

    //
    //   set up Twitter Spritzer
    //
    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addConnectionLifeCycleListener(connectionLifeCycleListener);
    statusListener = new StatusListener() { // This is what really gets called to deliver stuff from twitter4j
        @Override
        public void onStatus(Status status) {
            // time to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }
            try {
                boolean success = queue.offer(status, 15L, TimeUnit.SECONDS);
                if (!success) {
                    log.warn("queue too slow!");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }
        }

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

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            // This notice will be sent each time a limited stream becomes unlimited.
            // If this number is high and or rapidly increasing, it is an indication that your predicate is too broad, and you should consider a predicate with higher selectivity.
            log.warn("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            //log.info("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

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

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

    twitterStream.addListener(statusListener);
    twitterStream.sample(); // creates a generic StatusStream
    log.info("returned from sample()");

    return new Firehose() {

        private final Runnable doNothingRunnable = new Runnable() {
            public void run() {
            }
        };

        private long rowCount = 0L;
        private boolean waitIfmax = (maxEventCount < 0L);
        private final Map<String, Object> theMap = new HashMap<String, Object>(2);
        // DIY json parsing // private final ObjectMapper omapper = new ObjectMapper();

        private boolean maxTimeReached() {
            if (maxRunMinutes <= 0) {
                return false;
            } else {
                return (System.currentTimeMillis() - startMsec) / 60000L >= maxRunMinutes;
            }
        }

        private boolean maxCountReached() {
            return maxEventCount >= 0 && rowCount >= maxEventCount;
        }

        @Override
        public boolean hasMore() {
            if (maxCountReached() || maxTimeReached()) {
                return waitIfmax;
            } else {
                return true;
            }
        }

        @Override
        public InputRow nextRow() {
            // Interrupted to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }

            // all done?
            if (maxCountReached() || maxTimeReached()) {
                if (waitIfmax) {
                    // sleep a long time instead of terminating
                    try {
                        log.info("reached limit, sleeping a long time...");
                        sleep(2000000000L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException("InterruptedException", e);
                    }
                } else {
                    // allow this event through, and the next hasMore() call will be false
                }
            }
            if (++rowCount % 1000 == 0) {
                log.info("nextRow() has returned %,d InputRows", rowCount);
            }

            Status status;
            try {
                status = queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }

            HashtagEntity[] hts = status.getHashtagEntities();
            if (hts != null && hts.length > 0) {
                List<String> hashTags = Lists.newArrayListWithExpectedSize(hts.length);
                for (HashtagEntity ht : hts) {
                    hashTags.add(ht.getText());
                }

                theMap.put("htags", Arrays.asList(hashTags.get(0)));
            }

            long retweetCount = status.getRetweetCount();
            theMap.put("retweet_count", retweetCount);
            User user = status.getUser();
            if (user != null) {
                theMap.put("follower_count", user.getFollowersCount());
                theMap.put("friends_count", user.getFriendsCount());
                theMap.put("lang", user.getLang());
                theMap.put("utc_offset", user.getUtcOffset()); // resolution in seconds, -1 if not available?
                theMap.put("statuses_count", user.getStatusesCount());
            }

            return new MapBasedInputRow(status.getCreatedAt().getTime(), dimensions, theMap);
        }

        @Override
        public Runnable commit() {
            // ephemera in, ephemera out.
            return doNothingRunnable; // reuse the same object each time
        }

        @Override
        public void close() throws IOException {
            log.info("CLOSE twitterstream");
            twitterStream.shutdown(); // invokes twitterStream.cleanUp()
        }
    };
}

From source file:edu.uci.ics.asterix.external.dataset.adapter.PushBasedTwitterFeedClient.java

License:Apache License

public PushBasedTwitterFeedClient(IHyracksTaskContext ctx, ARecordType recordType,
        PushBasedTwitterAdapter adapter) throws AsterixException {
    this.recordType = recordType;
    this.tweetProcessor = new TweetProcessor(recordType);
    this.recordSerDe = new ARecordSerializerDeserializer(recordType);
    this.mutableRecord = tweetProcessor.getMutableRecord();
    this.initialize(adapter.getConfiguration());
    this.inputQ = new LinkedBlockingQueue<Status>();
    TwitterStream twitterStream = TwitterUtil.getTwitterStream(adapter.getConfiguration());
    twitterStream.addListener(new TweetListener(inputQ));
    FilterQuery query = TwitterUtil.getFilterQuery(adapter.getConfiguration());
    if (query != null) {
        twitterStream.filter(query);//  w ww  .j a va  2 s.c  om
    } else {
        twitterStream.sample();
    }
}

From source file:essex.bigessexnew.TwitterStreamHandler.java

@Override
public void run() {
    StatusListener sl = new StatusListener() {
        @Override//from w  ww . j a  v  a  2s.com
        public void onStatus(Status status) {
            /*String s = TwitterObjectFactory.getRawJSON(status);
            System.out.println(s);*/
            BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start();
            documentBuilder.add("content", status.getText());
            collection.insert(documentBuilder.get());

            /*DBObject dbObject = (DBObject) JSON.parse(json);
            collection.insert(dbObject);*/
            //System.out.println(json.toString());

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

        }

        @Override
        public void onScrubGeo(long l, long l2) {

        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {

        }

        @Override
        public void onException(Exception ex) {

        }
    };
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(sl);
    twitterStream.filter(Params.getProperty("hashtag"));
}

From source file:example.justids.java

License:Apache License

/**
* Usage: java twitter4j.examples.search.SearchTweets [query]
*
* @param args//ww w.  ja  v a2  s  .c  o  m
*/
public static void main(String[] args) {

    StatusListener listener = new StatusListener() {
        public Double count = 0d;
        Date started = new Date();
        Date previous = new Date();

        @Override
        public void onStatus(Status status) {

            try {

                File file = new File("Filtered_over1percent_lab_pc_obama_all.txt");
                File file2 = new File("Filtered_over1percent_lab_pc_obama.txt");

                // if file doesnt exists, then create it

                FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
                BufferedWriter bw = new BufferedWriter(fw);
                FileWriter fw2 = new FileWriter(file2.getAbsoluteFile(), true);
                BufferedWriter bw2 = new BufferedWriter(fw2);

                if (this.count % 1000 == 0) {
                    Date finished10k = new Date();

                    System.out.println("\n\n\n\n   AVERAGE  RATE OF TWEETS is "
                            + (this.count * 1000 / (finished10k.getTime() - this.started.getTime())));
                    System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime()));
                    System.out.println(this.count);
                    System.out.println(finished10k.getTime() + " " + this.started.getTime() + " "
                            + (finished10k.getTime() - this.started.getTime()));
                    System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " "
                            + (finished10k.getTime() - this.previous.getTime()));
                    System.out.println(status.getSource());
                    System.out.println("\n\n\n\n");
                    this.previous = finished10k;

                }

                this.count++;
                //  System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count);
                bw.write(status.getId() + "\n");
                bw.close();
                if (status.getText().contains("obama")) {
                    bw2.write(status.getId() + "\n");
                    bw2.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //   System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet");
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println(numberOfLimitedStatuses + " are missing from here");

        }

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

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

        }

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

        }
    };

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    RawStreamListener rawst = new RawStreamListener() {
        public Double count = 0d;
        public Double lengthsum = 0d;
        Date started = new Date();
        Date previous = new Date();

        @Override

        public void onMessage(String message) {
            if (!message.startsWith("{\"delete")) {

                count++;
                lengthsum += message.length();
                if (count % 1000 == 0) {
                    System.out.println(lengthsum / count);
                    //                       lengthsum=0d;
                    //                       count=0d;
                }
            }
            // TODO Auto-generated method stub

        }

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

        }
    };
    //      twitterStream.addListener(rawst);
    String[] searchfor = { "language", "people", "problem", "microsoft", "epidemic", "obama", "zoo" };
    FilterQuery query = new FilterQuery();
    query.track(searchfor);
    twitterStream.addListener(listener);
    twitterStream.filter(query);

    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    //        twitterStream.sample();
}

From source file:example.search.java

License:Apache License

/**
* Usage: java twitter4j.examples.search.SearchTweets [query]
*
* @param args//www.j a  v a2  s  .  c om
*/
public static void main(String[] args) {

    StatusListener listener = new StatusListener() {
        public Double count = 0d;
        Date started = new Date();
        Date previous = new Date();

        @Override
        public void onStatus(Status status) {

            try {

                File file = new File("whythissucks.txt");

                // if file doesnt exists, then create it

                FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
                BufferedWriter bw = new BufferedWriter(fw);

                if (this.count % 1000 == 0) {
                    Date finished10k = new Date();

                    System.out.println("\n\n\n\n   AVERAGE  RATE OF TWEETS is "
                            + (this.count * 1000 / (finished10k.getTime() - this.started.getTime())));
                    System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime()));
                    System.out.println(this.count);
                    System.out.println(finished10k.getTime() + " " + this.started.getTime() + " "
                            + (finished10k.getTime() - this.started.getTime()));
                    System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " "
                            + (finished10k.getTime() - this.previous.getTime()));
                    System.out.println(status.getSource());
                    System.out.println("\n\n\n\n");
                    this.previous = finished10k;

                }

                this.count++;
                //  System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count);

                bw.write(status.getId() + "|" + status.getText() + "\n");

                bw.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //   System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet");
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("limited " + numberOfLimitedStatuses);
        }

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

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

        }

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

        }
    };

    TwitterInstanceCreator tic = new TwitterInstanceCreator();

    RawStreamListener rawst = new RawStreamListener() {
        public Double count = 0d;
        public Double lengthsum = 0d;
        Date started = new Date();
        Date previous = new Date();
        String filename;
        String previousfilename = "";

        @Override

        public void onMessage(String message) {
            if (!message.startsWith("{\"delete")) {

                try {

                    // if file doesnt exists, then create it

                    if (this.count % 1000 == 0) {

                        Date finished10k = new Date();

                        System.out.println("\n\n\n\n   AVERAGE  RATE OF TWEETS is "
                                + (this.count * 1000 / (finished10k.getTime() - this.started.getTime())));
                        System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime()));
                        System.out.println(this.count);
                        System.out.println(finished10k.getTime() + " " + this.started.getTime() + " "
                                + (finished10k.getTime() - this.started.getTime()));
                        System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " "
                                + (finished10k.getTime() - this.previous.getTime()));
                        System.out.println("\n\n\n\n");
                        this.previous = finished10k;

                    }

                    File file = new File("tweetstoimport/" + this.filename);
                    FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
                    BufferedWriter bw = new BufferedWriter(fw);

                    this.count++;
                    //  System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count);
                    bw.write(message + "\n");
                    bw.close();
                    fw.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // TODO Auto-generated method stub

        }

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

        }
    };
    //      twitterStream.addListener(rawst);
    //  String[] searchfor={"flu", "influenza", "fever", "cough", "sore", "throat", "sore throat", "headache"};
    //   FilterQuery query=new FilterQuery();
    //   query.track(searchfor);
    TwitterStream twitterStream = tic.getStream(1);
    twitterStream.addListener(listener);
    // twitterStream.filter(query);

    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.sample();
    //Filters from the stream
    //twitterStream.filter(query);
}