简体   繁体   中英

How to follow a account in twitter using twitter4j Stream

I'm working in a project in Java and I need to follow some twitter account to collect data about the traffic. I started using the Twitter API but suddenly i found the limit too low so I changed to the stream API. Here there is no limit but I only know how to search something into the currect stream, not to get only the streams from the accounts I want. Is that posiible with the twitter4J api?

My code is the following one:

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

    StatusListener listener = new StatusListener() {

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

        }

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

        }

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

        }

        @Override
        public void onStatus(Status status) {

           //I process the tweet.

        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub

        }

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

        }

    };
    FilterQuery fq = new FilterQuery();


    String keywords[] = {"a","e","i","o","u"};

    fq.track(keywords);


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

You can specify users you want to follow using the FilterQuery object, eg:

long[] userIds = { ...omitted... };
fq.follow(userIds);

twitterStream.filter(fq);

Be aware that when filtering Tweets only need to match one of the conditions , ie the track or follow , so you may need to do some manual filtering if you intend to use both.

For more information check out Twitter's documentation for the follow parameter and filter stream .

twitter.createFriendship("screenName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM