Ruby on Rails:如何使用Twitter搜索Api从next_page获取tweet ?我的since_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

谢谢

阅读此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

你应该会没事的。不过要小心冗余。

如果你在控制器中这样做,你最终会从twitter返回一个" to many API Calls"错误。您将需要创建一个rake任务,以不超过其速率限制的方式拉入所有所需的tweet。

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

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

最新更新