简体   繁体   English

Twitter4J:如何返回通过样本/过滤器方法打印的推文?

[英]Twitter4J: How do I return the tweets printed by sample/filter methods?

What I am doing? 我在做什么?
I am trying to build an application with a User Interface. 我正在尝试使用用户界面构建应用程序。 The user will enter the search term and then using a websocket connection, I would start returning the filtered tweets to UI. 用户将输入搜索词,然后使用websocket连接,我将开始将过滤后的推文返回给UI。

What I did? 我做了什么?
I have a TwitterFilter class that looks like 我有一个TwitterFilter类,看起来像

public TwitterFilter(String searchTerm) {
        final ConfigurationBuilder configurationBuilder = getConfigurationBuilder();

        twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
        final StatusListener statusListener = getStatusListener();

        filterQuery = new FilterQuery();
        filterQuery.track(new String[] { searchTerm });
        filterQuery.language(new String[] { "en" });
        twitterStream.addListener(statusListener);

    }

    public void getFilteredTweets() {
        twitterStream.filter(filterQuery);
    }

    private static StatusListener getStatusListener() {
        return new StatusListener() {
            @Override
            public void onStatus(final Status status) {
                System.out.println(status.getText());
            }

            @Override
            public void onDeletionNotice(final StatusDeletionNotice statusDeletionNotice) {

            }

            @Override
            public void onTrackLimitationNotice(final int i) {

            }

            @Override
            public void onScrubGeo(final long userId, final long upToStatusId) {
            }

            @Override
            public void onStallWarning(final StallWarning stallWarning) {

            }

            @Override
            public void onException(final Exception e) {
                e.printStackTrace();
            }
        };
    }

    private static ConfigurationBuilder getConfigurationBuilder() {
        final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setDebugEnabled(true);
        configurationBuilder.setOAuthConsumerKey(CONSUMER_KEY);
        configurationBuilder.setOAuthConsumerSecret(CONSUMER_SECRET);
        configurationBuilder.setOAuthAccessToken(ACCESS_TOKEN);
        configurationBuilder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
        return configurationBuilder;
    }

and a class with websocket code, that needs to return tweets filtered by Twitter, looks as 和一个带有websocket代码的类,需要返回经过Twitter过滤的推文,看起来像

@ServerEndpoint("/tweets")
public class TweetStreamServer {
    @OnMessage
    public void tweets(final String message, final Session client) throws IOException, InterruptedException {
        final TwitterFilter twitterFilter = new TwitterFilter("india");
        for (final Session peer: client.getOpenSessions()) {
            peer.getBasicRemote().sendText(twitterFilter.getFilteredTweets()); // compilation error
        }
    }
}

Problem? 问题?
Since neither filter() or sample() methods return anything( void ), how do I return the tweets? 由于filter()sample()方法都不返回任何内容( void ),我该如何返回推文? ( documentation )? 文档 )?

This program implements streaming api. 该程序实现流式API。 Your use case requires user to enter the search term and you have to return the tweets with that search term. 您的用例要求用户输入搜索词,并且您必须返回带有该搜索词的推文。

Possible Implementations: 可能的实现:

1. Use rest api: 1.使用rest api:

Use oauth and login using user's credentials, then return the tweets you get. 使用oauth并使用用户的凭据登录,然后返回获得的推文。

https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/search/SearchTweets.java https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/search/SearchTweets.java

Rate limitations: https://dev.twitter.com/docs/rate-limiting/1.1/limits You can get only 180 tweets every 15 minutes (per user). 速率限制: https ://dev.twitter.com/docs/rate-limiting/1.1/limits每15分钟(每位用户)您只能获得180条推文。

2. Streaming api: 2.流式API:

This is not a rest api search. 这不是REST API搜索。 When you track particular keyword or follow an user, you get the tweets whenever they are tweeted (live). 当您跟踪特定的关键字或关注用户时,只要发布(实时)推文,就会收到这些推文。 This is different from searching. 这与搜索不同。 You cannot know when a twitter user tweets using the keywords you are tracking. 您无法得知Twitter用户何时使用您正在跟踪的关键字进行鸣叫。 Since this is a live stream. 由于这是直播。

Start the stream with possible keywords. 使用可能的关键字开始流。 Save the results in a database and search the database when you get a request from user. 将结果保存到数据库中,并在收到用户请求时搜索数据库。 When user enters a new keyword, stop the stream, add the new keyword to the list of keywords already tracking and start the stream. 当用户输入新关键字时,停止流,将新关键字添加到已跟踪的关键字列表中并启动流。 [You can only have one connection to streaming api from one IP address]. [您只能从一个IP地址到流式API的一个连接]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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