簡體   English   中英

Twitter gem獲取推文的位置

[英]Twitter gem get location of tweet

我一直在使用twitter rails API,但在獲取推文位置時遇到了麻煩。 這似乎總是失敗,什么也不返回。 我也嘗試過做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是如何收到這些查詢的。 我是否缺少另一個位置字段? 我還注意到輸出的數量不等於tweet數組的大小。

https://pastebin.com/eW18Ri2S

這是一個示例輸出

twitter gem文檔中所述,您應該place?用戶place? 還是geo? 方法。

空對象

在版本4中,如果缺少該對象,則您期望返回Twitter對象的方法將返回nil。 這可能導致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中,除了false和nil之外,其他一切都是真實的。 對於這些情況,現在有謂詞方法:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM