Example usage for twitter4j TwitterStreamFactory TwitterStreamFactory

List of usage examples for twitter4j TwitterStreamFactory TwitterStreamFactory

Introduction

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

Prototype

public TwitterStreamFactory() 

Source Link

Document

Creates a TwitterStreamFactory with the root configuration.

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 {/*  w  ww . ja va  2 s .  com*/
        // 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.//w ww . j av a  2s.  c om
 * 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 a2s  .co 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 w  w w .j ava2  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:druid.examples.twitter.TwitterSpritzerFirehoseFactory.java

License:Open Source License

@Override
public Firehose connect() throws IOException {
    final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() {
        @Override/*www  . j  a v a 2s .c  o 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:essex.bigessexnew.TwitterStreamHandler.java

@Override
public void run() {
    StatusListener sl = new StatusListener() {
        @Override/*from   ww  w  . j a  v a 2  s .  c  om*/
        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  .  com
*/
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:examples.PrintUserStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    // user() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.user();/*from   w w w  . j  a va 2s  .c o m*/
}

From source file:foo.bar.twitter.sample01.Sample01Activity.java

License:Apache License

/**
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */// w  ww .  j  a  v  a2  s.c  om
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //buttons
    bt_start = (Button) findViewById(R.id.stratButton);
    bt_cancel = (Button) findViewById(R.id.cancelButton);
    bt_start.setEnabled(true); //start button enabled
    bt_cancel.setEnabled(false); //cancel button disabled

    //get OAuth token
    SharedPreferences pref = getSharedPreferences(ConstantValue.PREFERENCE_NAME, MODE_PRIVATE);
    token = pref.getString(ConstantValue.PREF_KEY_TOKEN, "");
    tokenSecret = pref.getString(ConstantValue.PREF_KEY_SECRET, "");

    isAuthorized = (token.length() > 0 && tokenSecret.length() > 0);
    twitterStream = new TwitterStreamFactory().getInstance();
}

From source file:foo.bar.twitter.sample01.Sample01Activity.java

License:Apache License

/**
 * start button clicked//from  ww  w .j a  v  a  2s.  c om
 * @param v
 */
public void onClickStartButton(View v) {
    Log.d("DEBUG", "Start button clicked!");

    if (!isAuthorized) {
        Log.d("DEBUG", "not authorized");
        return;
    }

    bt_start.setEnabled(false); //start button disabled
    bt_cancel.setEnabled(true); //cancel button enabled

    if (isAuthorized) {
        twitterStream = new TwitterStreamFactory().getInstance();
        twitterStream.setOAuthConsumer(ConstantValue.CONSUMER_KEY, ConstantValue.CONSUMER_SECRET);
        twitterStream.setOAuthAccessToken(new AccessToken(token, tokenSecret));
        StatusListener listener = new StatusListener() {
            @Override
            public void onException(Exception arg0) {
                ;//no process
            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice arg0) {
                ;//no process
            }

            @Override
            public void onScrubGeo(long arg0, long arg1) {
                ;//no process
            }

            @Override
            public void onStatus(Status status) {
                String tweet = status.getText();
                Log.d("DEBUG", "tweet: " + tweet);
            }

            @Override
            public void onTrackLimitationNotice(int arg0) {
                ;//no process
            }
        };
        twitterStream.addListener(listener);
        String[] trackArray = { ConstantValue.HASH_TAG };
        twitterStream.filter(new FilterQuery(0, null, trackArray));
    }
}