如何使用 Spring 社交与 Twitter 流 API 获取基于位置的推文?
How to get location based tweets using Spring social with Twitter Streaming API?
我需要根据地理位置实时推送推文。通过 Twitter API 文档我知道我们可以根据地理位置进行搜索。
但是可以根据地理位置进行直播吗?我使用 spring 社交推特进行 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 社交 Twitter 实现它。
谢谢
我一直在尝试这个,终于让流式推文工作了。
我继续学习本教程 - https://spring.io/guides/gs/accessing-twitter.
访问 http://localhost:8080/stream/10_000 将在控制台中流式传输来自旧金山地区(目前已硬编码)的推文 10 秒,并在 10 秒后在网页中显示这些推文。
在控制器中 Class 添加一个 @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),请分享。谢谢。
我需要根据地理位置实时推送推文。通过 Twitter API 文档我知道我们可以根据地理位置进行搜索。
但是可以根据地理位置进行直播吗?我使用 spring 社交推特进行 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 社交 Twitter 实现它。
谢谢
我一直在尝试这个,终于让流式推文工作了。 我继续学习本教程 - https://spring.io/guides/gs/accessing-twitter.
访问 http://localhost:8080/stream/10_000 将在控制台中流式传输来自旧金山地区(目前已硬编码)的推文 10 秒,并在 10 秒后在网页中显示这些推文。
在控制器中 Class 添加一个 @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),请分享。谢谢。