簡體   English   中英

Ruby on Rails:如何使用Twitter搜索Api從next_page獲取推文? 什么應該是我的since_id和max_id?

[英]Ruby on Rails: How to get tweets from the next_page using the Twitter search Api? And What should be my since_id and max_id?

這是我的代碼。 我想用@second_results獲取下一頁的結果。 但它似乎只是重復從@first_results收到的任何內容。

search = TwitterManualSearch.new


@first_results = search.first_query(:tag => params[:search]).results

@second_results = search.second_query(:tag => params[:search], :since_id =>  @first_results.results.last.id, :max_id => @first_results.max_id).results

為了您的參考,我寫了一些方法來緩解調用,我在這里使用twitter gem - https://github.com/sferik/twitter

我知道我在做一些根本錯誤的事情,但它是什么?

更新:給你一個簡短,粗略和快速的想法,這是我如何使用代碼返回結果(不包括我的所有方法)

為了獲得第一批結果,我做了

@first_results = Twitter.search("#tag", :count => 30, :result_type => "recent")

@first_results.results.map do |status|
  "#{status.from_user}: #{status.text}"
end

然后再獲得更多結果

    @second_results = search.second_query("#tag", :count => 30, :result_type => "recent", :since_id =>  @first_results.results.last.id, :max_id => @first_results.max_id).results



@second_results.map do |status|
  "#{status.from_user}: #{status.text}"
end

謝謝

如果您在控制器中執行此操作,最終將從Twitter返回“To many API Calls”錯誤。 您需要創建一個rake任務,以不超過其速率限制的方式提取所有需要的推文。

https://dev.twitter.com/docs/rate-limiting/1.1

我猜你所看到的可能是超出限制的結果。

閱讀此https://dev.twitter.com/docs/working-with-timelines 你在做什么並不能區分第一個實例變量和第二個實例變量。 相反,只需確保對第一個變量調用進行計數,並將第二個調用的max_id與第一個實例變量調用的最后一個id匹配。 像這樣的東西

@second_results = search.second_query(:tag => params[:search], :max_id => @first_results.results.last.id).results

而你應該沒事。 雖然注意裁員。

暫無
暫無

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

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