如何过滤 ruby 中基于位置的推文?
How to filter location based tweets in ruby?
我正在使用一个简单的机器人,它可以根据关键字来收藏推文。我只想收藏来自特定位置的推文。知道如何在 ruby 中执行此操作吗?如果在 ruby 中不可能,我愿意接受其他语言。谢谢
我的代码:
require 'twitter' #gem install twitter
while true
begin
config = {
consumer_key:
consumer_secret:
access_token:
access_token_secret:
}
rClient = Twitter::REST::Client.new config
sClient = Twitter::Streaming::Client.new(config)
# topics to watch
topics = ['star wars']
sClient.filter(:track => topics.join(',')) do |tweet|
if tweet.is_a?(Twitter::Tweet)
puts tweet.text
rClient.fav tweet
end
end
rescue Exception => e
puts 'error, waiting for 5s: ' + e.class.to_s
sleep 5
end
end
我尝试添加这些行,但它不起作用(对于 NYC lat/long):
@Geo = Twitter::Geo.new(coordinates: [22.2798024,114.149884])
sClient.filter(:track => topics.join(','), :Geo => Tweet.Geo('40.7033127,-73.979681')) do |tweet|
要根据位置进行过滤,您需要散列中的位置参数。
那么,这就是您的过滤器函数调用的样子
sClient.filter(:track => topics.join(','), :locations => "40.7033127,-73.979681") do |tweet|
就是这样,你可以开始了。我在 documentation and in this example.
中找到了它
我正在使用一个简单的机器人,它可以根据关键字来收藏推文。我只想收藏来自特定位置的推文。知道如何在 ruby 中执行此操作吗?如果在 ruby 中不可能,我愿意接受其他语言。谢谢
我的代码:
require 'twitter' #gem install twitter
while true
begin
config = {
consumer_key:
consumer_secret:
access_token:
access_token_secret:
}
rClient = Twitter::REST::Client.new config
sClient = Twitter::Streaming::Client.new(config)
# topics to watch
topics = ['star wars']
sClient.filter(:track => topics.join(',')) do |tweet|
if tweet.is_a?(Twitter::Tweet)
puts tweet.text
rClient.fav tweet
end
end
rescue Exception => e
puts 'error, waiting for 5s: ' + e.class.to_s
sleep 5
end
end
我尝试添加这些行,但它不起作用(对于 NYC lat/long):
@Geo = Twitter::Geo.new(coordinates: [22.2798024,114.149884])
sClient.filter(:track => topics.join(','), :Geo => Tweet.Geo('40.7033127,-73.979681')) do |tweet|
要根据位置进行过滤,您需要散列中的位置参数。
那么,这就是您的过滤器函数调用的样子
sClient.filter(:track => topics.join(','), :locations => "40.7033127,-73.979681") do |tweet|
就是这样,你可以开始了。我在 documentation and in this example.
中找到了它