实时听推文 [英] Listen tweets in real-time

查看:100
本文介绍了实时听推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实时听一条推文,这意味着当有人推特时我想看到那条推文。

I want to listen to a tweet in real-time, which means when someone tweets I want to see that tweet.

然而我能够收到来自的推文我的新闻提要使用twitter4j库。
这是代码。

However I was able to get tweets from my news feed using twitter4j library. Here's the code.

 package twitteroperation;

import java.util.List;
import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterOperation {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws TwitterException {


          ConfigurationBuilder cb = new ConfigurationBuilder();

           cb.setDebugEnabled(true)
                .setOAuthConsumerKey("")
                .setOAuthConsumerSecret("")
                .setOAuthAccessToken("")
                .setOAuthAccessTokenSecret("");

     TwitterFactory tf=new TwitterFactory(cb.build());  

     twitter4j.Twitter tw=tf.getInstance();

      List<Status> statuses=  tw.getHomeTimeline();

         for (Status status1 : statuses) {
            System.out.println(status1.getUser().getName() + ":" + status1.getText());

        }   
    }
}

我有发现我必须使用Streaming API实时访问推文。但我在java中找不到任何示例代码来访问teal-time推文。

I have found that I must use Streaming APIs to access tweets real-time. But I couldn't find any sample code in java to access teal-time tweets.

推荐答案

Twitter4j提供示例,其中一个它们是您要找的是什么,但是您需要更改行

Twitter4j provides examples, and one of them is exactly what you are looking for, however you need to change the line

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

with

     ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setDebugEnabled(true).setOAuthConsumerKey("")
              .setOAuthConsumerSecret("")
              .setOAuthAccessToken("")
              .setOAuthAccessTokenSecret("");

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

如果不这样做,则必须配置属性文件。之后你会得到这个代码

If you don't, you will have to configure the properties file. After that you will get this code

import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

public final class PrintSampleStream {

    public static void main(String[] args) throws TwitterException {
         ConfigurationBuilder cb = new ConfigurationBuilder();
          cb.setDebugEnabled(true).setOAuthConsumerKey("")
                  .setOAuthConsumerSecret("")
                  .setOAuthAccessToken("")
                  .setOAuthAccessTokenSecret("");
          TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
                  .getInstance();
        StatusListener listener = new StatusListener() {
            @Override
            public void onStatus(Status status) {
                System.out.println("@" + 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 onTrackLimitationNotice(int numberOfLimitedStatuses) {
                System.out.println("Got 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 onException(Exception ex) {
                ex.printStackTrace();
            }
        };
        twitterStream.addListener(listener);
        twitterStream.sample();
    }
}

有了这个,你就会开始收到样本公开流。如果您想获得特定的推文,则需要使用一些过滤器。例如,如果您想要特定查询的推文,则需要更改此行

With that you'll start to receive the sample public stream. If you want to get specific tweets you will need to use some filters. For example if you want the tweets for an specific query you need to change this line

twitterStream.sample();

带有你想要的单词

FilterQuery filtre = new FilterQuery();
String[] keywordsArray = { "obama" };
filtre.track(keywordsArray);
twitterStream.filter(filtre);

如果您想从特定的配置文件中流式传输推文,则需要使用以下过滤器。行 twitterStream.sample(); 你需要为此更改

If you want to stream tweets from specifics profiles you will need to use the follow filter. The line twitterStream.sample(); you will need to change it for this

          long[] users = new long[]{someid,someotherid,otherid};
          twitterStream.addListener(listener);
          FilterQuery filtre = new FilterQuery();
          filtre.follow(users);
          twitterStream.filter(filtre);

数组的id是Twitter用于每个用户的id。如果您不知道某个用户的ID,您可以通过Twitter4j获取它:

The id's for the array are the id's that Twitter use for every user. If you don't know the id for a certain user you could get it with Twitter4j:

User user = tw.showUser("barackobama"); //tw is your Twitter variable from twitter4j.Twitter tw=tf.getInstance();
long id = user.getId();

有更多方法可以从流中检索推文,您只需要阅读文档或搜索这个网站。祝你好运!

There are more ways to retrieve tweets from the stream, you only need to read the documentation or search on this site. Good luck!

这篇关于实时听推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆