4

我需要根据地理位置实时提供推文。通过 Twitter API 文档,我了解我们可以根据 geolocaion 进行搜索。

但是是否可以基于 geolocaion 进行流式传输?我使用 spring social twitter 进行 API 集成。

           List<StreamListener> listeners = new ArrayList<StreamListener>();
           StreamListener streamListener = new StreamListener() {               
            @Override
            public void onWarning(StreamWarningEvent warningEvent) {}

            @Override
            public void onTweet(Tweet tweet) {}

            @Override
            public void onLimit(int numberOfLimitedTweets) {}

            @Override
            public void onDelete(StreamDeleteEvent deleteEvent) {}
        };
        listeners.add(streamListener);      
        //to-do geolocation based stream filter
        twitter.streamingOperations().filter("tesla", listeners);
        Thread.sleep(10000);

而且我看到 Twitter 流 API 也有一个请求的位置参数。但是我如何使用 Spring Social Twitter 来实现它。

谢谢

4

1 回答 1

1

我一直在尝试这个,终于让流媒体推文工作了。我继续学习本教程 - https://spring.io/guides/gs/accessing-twitter

访问http://localhost:8080/stream/10_000将在控制台中流式传输来自旧金山地区(截至目前已硬编码)的推文 10 秒,并在 10 秒后在网页中显示这些推文。

在 Controller 类中添加一个 @RequestMapping 方法,如下所示: HelloController.java

@RequestMapping("/stream/{time}")
public String streamTweet(@PathVariable int time, Model model) throws InterruptedException{
    if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
        return "redirect:/connect/twitter";
    }
    Model returnedmodel = streamService.streamApi(model, time);
    model = returnedmodel;

    return "stream";
}

StreamService.java

@Service
public class StreamService {

    private final Logger log = LoggerFactory.getLogger(StreamService.class);

    @Autowired
    private Twitter twitter;

    public Model streamApi(Model model, int time) throws InterruptedException{

        List<Tweet> tweets = new ArrayList<>();

        List<StreamListener> listeners = new ArrayList<StreamListener>();
        StreamListener streamListener = new StreamListener() {

            @Override
            public void onWarning(StreamWarningEvent warningEvent) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTweet(Tweet tweet) {
                System.out.println(tweet.getUser().getName() + " : " + tweet.getText());
                log.info("User '{}', Tweeted : {}", tweet.getUser().getName() , tweet.getText());
                tweets.add(tweet);
                model.addAttribute("tweets", tweets);
            }

            @Override
            public void onLimit(int numberOfLimitedTweets) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDelete(StreamDeleteEvent deleteEvent) {
                // TODO Auto-generated method stub

            }
        };

        listeners.add(streamListener);
        //This sets the GeoCode (-122.75,36.8,-121.75,37.8) of San Francisco(South-West and North-East) region as given in below twitter docs
        //https://dev.twitter.com/streaming/overview/request-parameters#locations
        Float west=-122.75f;
        Float south=36.8f;
        Float east=-121.75f;
        Float north = 37.8f;

        FilterStreamParameters filterStreamParameters = new FilterStreamParameters();
        filterStreamParameters.addLocation(west, south, east, north);

        Stream userStream = twitter.streamingOperations().filter(filterStreamParameters, listeners);
        Thread.sleep(time);
        userStream.close();
        return model;
    }
}

stream.html

<html>
    <head>
        <title>Hello Twitter Stream</title>
    </head>
    <body>                  
        <h4>The Twitter Stream:</h4>`enter code here`
        <ul>
            <li th:each="tweet:${tweets}" 
                th:text="${tweet.text} + ' tweeted by - ' + ${tweet.user.name} 
                    + ' from ' + ${tweet.user.location} 
                    + ' @ ' + ${tweet.createdAt}"> 
                    Tweet
            </li>
        </ul>
    </body>
</html>

PS:我知道你不想做这样的事情(即在等待 X 秒后显示推文,我这样做只是为了让它工作)

顺便说一句,如果您有任何想法,我如何使用此流实时显示推文,(使用或不使用 websockets)请分享。谢谢。

于 2016-01-16T13:29:28.713 回答