【问题标题】: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?Ruby on Rails:如何使用 Twitter 搜索 API 从 next_page 获取推文?我的since_id 和max_id 应该是什么?
【发布时间】:2013-03-21 23:45:06
【问题描述】:

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

谢谢

【问题讨论】:

  • 您能否提供完整的上下文,说明您在哪里调用它以及您为减轻调用而编写的方法是什么?
  • 我已经更新了这个问题。如果我删除我的方法/类,更新代码就是它所做的。

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


【解决方案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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 我通过 ajax、一个控制器和整个单独的类来做到这一点。我正在处理限制。另外,我需要知道如何进入下一页。 rake 在这里不是一个选择:)。而且我很确定显示两次的相同内容不会超过速率限制。我在处理最大值和自 id 时做错了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2016-04-29
    • 2011-09-18
    • 1970-01-01
    • 2015-12-07
    • 2012-06-03
    相关资源
    最近更新 更多