Ruby on Rails:如何使用Twitter搜索Api从next_page获取推文?什么应该是我的since_id和max_id?

时间:2013-03-31 20:45:38

标签: ruby-on-rails ruby ruby-on-rails-3 twitter

这是我的代码。我正在尝试使用@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

由于

2 个答案:

答案 0 :(得分:1)

如果您在控制器中执行此操作,最终将从Twitter返回“To many API Calls”错误。您将需要创建一个rake任务,以不超过其速率限制的方式提取您所需的所有推文。

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

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

答案 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

你应该没事。虽然注意裁员。