Twitter Stream API to get users tweets

2019-03-21 12:15发布

I am using twitter stream API to get tweets from user. I have list of user Id How would i get list of tweets from the TwitterStream API. I have analyzed the following sample

public class TwitterStreamAPI {

    public static void main(String[] args) {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("xxxx");
        cb.setOAuthConsumerSecret("xxx");
        cb.setOAuthAccessToken("xxx");
        cb.setOAuthAccessTokenSecret("xxx");

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

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

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

            @Override
            public void onDeletionNotice(long directMessageId, long userId) {
               // System.out.println("Got a direct message deletion notice id:" + directMessageId);
            }

            @Override
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
               // System.out.println("Got a 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 onFriendList(long[] friendIds) {
                /*System.out.print("onFriendList");
                for (long friendId : friendIds) {
                    System.out.print(" " + friendId);
                }
                System.out.println();*/
            }

            @Override
            public void onFavorite(User source, User target, Status favoritedStatus) {
               /* System.out.println("onFavorite source:@"
                        + source.getScreenName() + " target:@"
                        + target.getScreenName() + " @"
                        + favoritedStatus.getUser().getScreenName() + " - "
                        + favoritedStatus.getText());*/
            }

            @Override
            public void onUnfavorite(User source, User target, Status unfavoritedStatus) {
               /*System.out.println("onUnFavorite source:@"
                                + source.getScreenName() + " target:@"
                                + target.getScreenName() + " @"
                                + unfavoritedStatus.getUser().getScreenName()
                                + " - " + unfavoritedStatus.getText());*/
            }

            @Override
            public void onFollow(User source, User followedUser) {
                /*System.out.println("onFollow source:@"
                        + source.getScreenName() + " target:@"
                        + followedUser.getScreenName());*/
            }

            @Override
            public void onDirectMessage(DirectMessage directMessage) {
               /* System.out.println("onDirectMessage text:"
                        + directMessage.getText());*/
            }

            @Override
            public void onUserListMemberAddition(User addedMember, User listOwner, UserList list) {
                /*System.out.println("onUserListMemberAddition added member:@"
                        + addedMember.getScreenName()
                        + " listOwner:@" + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserListMemberDeletion(User deletedMember, User listOwner, UserList list) {
               /* System.out.println("onUserListMemberDeleted deleted member:@"
                        + deletedMember.getScreenName()
                        + " listOwner:@" + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserListSubscription(User subscriber, User listOwner, UserList list) {
                /*System.out.println("onUserListSubscribed subscriber:@"
                        + subscriber.getScreenName()
                        + " listOwner:@" + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserListUnsubscription(User subscriber, User listOwner, UserList list) {
                /*System.out.println("onUserListUnsubscribed subscriber:@"
                        + subscriber.getScreenName()
                        + " listOwner:@" + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserListCreation(User listOwner, UserList list) {
                /*System.out.println("onUserListCreated listOwner:@"
                        + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserListUpdate(User listOwner, UserList list) {
                /*System.out.println("onUserListUpdated listOwner:@"
                        + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserListDeletion(User listOwner, UserList list) {
                /*System.out.println("onUserListDestroyed listOwner:@"
                        + listOwner.getScreenName()
                        + " list:" + list.getName());*/
            }

            @Override
            public void onUserProfileUpdate(User updatedUser) {
                //System.out.println("onUserProfileUpdated user:@" + updatedUser.getScreenName());
            }

            @Override
            public void onBlock(User source, User blockedUser) {
                /*System.out.println("onBlock source:@" + source.getScreenName()
                        + " target:@" + blockedUser.getScreenName());*/
            }

            @Override
            public void onUnblock(User source, User unblockedUser) {
                /*System.out.println("onUnblock source:@" + source.getScreenName()
                        + " target:@" + unblockedUser.getScreenName());*/
            }

            @Override
            public void onException(Exception ex) {
               /* ex.printStackTrace();
                System.out.println("onException:" + ex.getMessage());*/
            }
        };

        String useIds[] = {"99901654"};




        twitterStream.addListener(listener);
    twitterStream.user(useIds);
    }
}

https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintUserStream.java

but it does not print status

2条回答
成全新的幸福
2楼-- · 2019-03-21 12:22

I have answered the same in detail here. Please do check it. This will show you how we can read and update twitter using required access tokens.

in brief, These are the steps

  1. Crate a Twitter application on https://apps.twitter.com/app/new and get your consumer key. (Follow the mentioned blog if you struck somewhere.)
  2. Using the Twitter4j API get the authorization URL, hit it and get the PIN. (The code required is mentioned below for your ref)
  3. Input the pin and get access tocken (refer below code)
  4. That's all now we can read or update twitter feeds. (Based on the access level you set while creating twitter application)

Please check here for sample code. https://stackoverflow.com/a/40691881/2405670

查看更多
不美不萌又怎样
3楼-- · 2019-03-21 12:25

The streaming apis provide you with real-time events therefore if none of the users Tweet during the time you are listening you will not see any output.

If you are instead trying to fetch a users historic tweets consider using Twitter#getUserTimeline(screenName) instead, e.g.:

final Twitter twitter = new TwitterFactory().getInstance();
final List<Status> statuses = twitter.getUserTimeline(screenName);

for (Status status : statuses) {
    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}

See this example for further details.

查看更多
登录 后发表回答