Twitter宝石获得了推文的位置



我一直在用Twitter Rails API弄乱,我很难获得Tweet的位置。这似乎总是失败的,没有返回任何东西。我也尝试过Tweet.coordinates,但似乎不起作用。

tweets = client.user_timeline("gems")
puts "size : #{tweets.size}"  
tweets.each do |tweet|
    if(tweet.place)
       puts tweet.place.attributes
    end
end

我正在使用以下Twitter宝石。https://github.com/sferik/twitter

编辑:

tweets = client.search("a", geocode: "40.7128,-74.0059,50mi").take(100)
tweets.each do |tweet|
    if tweet.place?
        puts tweet.place.full_name
     elsif tweet.geo?
        puts "geofound"
    elsif tweet.user.location?
        puts tweet.user.location
     end

因此,我尝试了上述代码寻找具有地理编码的推文,但似乎没有一个地方或地理字段,并且总是返回Tweet.user.location,但这不是很准确。该输出只有很多纽约,但也有其他城市的一堆,所以当其他城市不存在/远离纽约时,我真的不知道Twitter如何获得这些疑问。我想念另一个位置字段吗?我还注意到,输出数不等于推文数组的大小。

https://pastebin.com/ew18ri2s

这是一个示例输出

twitter GEM文档中所述,您应用户place?geo?方法。

null对象

在版本4中,您期望返回Twitter对象的方法 如果缺少该对象,将返回无。这可能导致 NoMethodError。为了防止此类错误,您可能已经介绍了 检查响应的真实性,例如:

status = client.status(55709764298092545)
if status.place   
  # Do something with the Twitter::Place object
elsif status.geo   
  # Do something with the Twitter::Geo object
end

在版本5中,所有这些 方法将返回Twitter::NullObject而不是nil。这应该 防止nomethoderror,但如果您 进行真实检查,因为Ruby的一切都是真实的 除了错误和零。对于这些情况,现在有谓词 方法:

 status = client.status(55709764298092545)
 if status.place?
   # Do something with the Twitter::Place object
 elsif status.geo?
   # Do something with the Twitter::Geo object
 end

相关内容

最新更新