【问题标题】:node-ntwitter and twitter API Limitesnode-twitter 和 twitter API 限制
【发布时间】:2012-10-08 05:26:54
【问题描述】:

与 API 限制相关的问题,这可能是我遗漏的东西,但不确定:

如果我这样做:

 twit.showUser(ids, function(error, response) {
   console.log(response)
 }

其中{ids}是一个数组,长度

当我这样做并且 ID 大于 100 时,它会失败。

这是基于: https://dev.twitter.com/docs/api/1.1/get/users/lookup

特别是:

每个请求最多 100 个用户

这是在 ntwitter 模块中以某种方式进行管理,还是我需要在外部进行管理?如果是这样,有什么关于如何管理它的建议吗?

或者,在 node-ntwitter 模块之外,如果我想从 showUser() 发回所有 responses 的复合 json,你会建议如何以干净的方式解决这个问题?

【问题讨论】:

    标签: javascript node.js asynchronous twitter


    【解决方案1】:

    每个请求最多 100 个用户

    意味着您一次只能请求 100 个 ID。你可以运行一个增加100的for循环:

    for(var i = 0; i < ids.length; i + 100) {
      requestIds = ids.slice(i, i+99)
      twit.showUser(requestIds, function(error, response) {
       console.log(response)
     }
    }
    

    (未经测试)

    由于它是 node.js,您可能希望它是异步的。查看forEachLimitforEachSeries

    【讨论】:

    • showUser() 是异步的,这意味着循环将在很久以前从 twitter api 调用返回响应。
    • 取决于你想在回调中做什么,但这就是为什么我添加了 forEachLimit 和 forEachSeries 的链接...
    • 谢谢。我想要的基本上是聚合响应对象并将它们发送回客户端。
    • 这绝对是异步库的东西。
    猜你喜欢
    • 2010-11-20
    • 2017-12-20
    • 2016-04-29
    • 2017-10-23
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多