Example usage for twitter4j UserStreamAdapter UserStreamAdapter

List of usage examples for twitter4j UserStreamAdapter UserStreamAdapter

Introduction

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

Prototype

UserStreamAdapter

Source Link

Usage

From source file:com.pulzitinc.flume.source.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets./*from  ww w.j a  v  a 2s  .c om*/
 */
@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.
    UserStreamListener listener = new UserStreamAdapter() {
        // 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() + " - "
                    + TwitterObjectFactory.getRawJSON(status));

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

            channel.processEvent(event);
        }

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

    logger.debug("Setting up Twitter stream using consumer key {} and access token {}",
            new String[] { consumerKey, accessToken });

    // Set up the stream's listener (defined above),
    twitterStream.addListener(listener);

    logger.debug("Starting up Twitter consuming...");

    twitterStream.user();

    super.start();
}

From source file:ikemen.Main.java

License:Apache License

public static void main(String[] args) {
    final Twitter twitter = TwitterFactory.getSingleton();
    final TwitterStream stream = TwitterStreamFactory.getSingleton();
    stream.addListener(new UserStreamAdapter() {
        public void onStatus(Status status) {
            if (yonda(status.getText())) {
                try {
                    twitter.createFavorite(status.getId());
                } catch (TwitterException ignore) {
                }//from   w w  w .  j  a v  a 2s .com
                try {
                    twitter.sendDirectMessage("yusuke", "http://twitter.com/yusuke/status/" + status.getId());
                } catch (TwitterException ignore) {
                }
            }
        }
    });
    stream.user();
}

From source file:me.timothy.twittertoreddit.TwitterToReddit.java

License:Open Source License

public TwitterToReddit(HttpRestClient restClient, com.github.jreddit.user.User redditUser, long twId,
        String sub) {/*from w  ww  . ja  va  2  s  .co m*/
    this.restClient = restClient;
    this.redditUser = redditUser;
    this.twitterId = twId;
    this.subreddit = sub;

    listener = new UserStreamAdapter() {
        @Override
        public void onStatus(final Status status) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    postTweet(status);
                }

            }).start();
        }

    };
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onBlock(Event2<User, User> b) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override/*from   w w  w . java 2  s.c  om*/
        public void onBlock(User source, User blockedUser) {
            b.onEvent(source, blockedUser);
        }
    });
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onUnBlock(Event2<User, User> arg) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override/*from w ww . j  a v a 2 s.  c  om*/
        public void onUnblock(User source, User unblockedUser) {
            arg.onEvent(source, unblockedUser);
        }
    });
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onFavorite(Event3<User, User, Status> arg) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override/*from   w ww . ja  va2 s  . c o m*/
        public void onFavorite(User source, User target, Status favoritedStatus) {
            arg.onEvent(source, target, favoritedStatus);
        }
    });
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onUnfavorite(Event3<User, User, Status> arg) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override//from w ww  . j av  a 2 s.  co m
        public void onUnfavorite(User source, User target, Status unfavoritedStatus) {
            arg.onEvent(source, target, unfavoritedStatus);
        }
    });
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onFollow(Event2<User, User> arg) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override/*from   w w w. ja  va 2 s. co  m*/
        public void onFollow(User source, User followedUser) {
            arg.onEvent(source, followedUser);
        }
    });
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onDirectMessage(Event<DirectMessage> arg) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override//from www .  j av a2  s.c o  m
        public void onDirectMessage(DirectMessage directMessage) {
            arg.onEvent(directMessage);
        }
    });
}

From source file:net.nokok.twitduke.core.twitter.LambdaTwitterStream.java

License:Open Source License

public void onUserListMenberAddition(Event3<User, User, UserList> arg) {
    this.twitterStream.addListener(new UserStreamAdapter() {
        @Override//  w  w w. j  a v a  2s  .  c om
        public void onUserListMemberAddition(User addedMember, User listOwner, UserList list) {
            arg.onEvent(addedMember, listOwner, list);
        }
    });
}