Node.js Twitter API cursors

隐身守侯 提交于 2019-12-04 07:49:25

The issue you are having is due to Node.js being asynchronous.

T.get('followers/ids', { screen_name: 'twitter' },  function getData(err, data, response) {

  // Do stuff here to write data to a file

  if(data['next_cursor'] > 0) T.get('followers/ids', { screen_name: 'twitter', next_cursor: data['next_cursor'] }, getData);

  })

}

Please note:

  1. I gave a name to the internal callback function. That is so that we can recursively call it from the inside.
  2. The loop is replaced with a recursive callback.
  3. If there is a next_cursor data, then we call T.get using the same function getData.

Be aware that Do stuff here code will be executed many times (as many as there are next cursors). Since it is recursive callback - the order is guaranteed.


If you do not like the idea of recursive callbacks, you can avoid it by:

  1. Finding out beforehand all the next_cursor's if possible, and generate requests using for loop.
  2. Alternatively, use asynchronous-helper modules like Async (though for learning purposes, I would avoid modules unless you are fluent in the concept already).

Consider testing with some 5K+ account.

    const T = new Twit(tokens)

    function getFollowers (screenName, followers = [], cur = -1) {
      return new Promise((resolve, reject) => {
        T.get('followers/ids', { screen_name: screenName, cursor: cur, count: 5000 }, (err, data, response) => {
          if (err) {
            cur = -1
            reject(err)
          } else {
            cur = data.next_cursor
            followers.push(data.ids)
            if (cur > 0) {
              return resolve(getFollowers(screenName, followers, cur))
            } else {
              return resolve([].concat(...followers))
            }
          }
        })
      })
    }

    async function getXaqron () {
      let result = await getFollowers('xaqron')
      return result
    }

 console.log(getXaqron().catch((err) => {
  console.log(err) // Rate limit exceeded
}))

Struggled with this one.. Everything seemed to work, but data['next_cursor'] didn't change, EVER!

Code should be like this:

T.get('followers/ids', { screen_name: 'twitter' },  function getData(err, data, response) {

  // Do stuff here to write data to a file

  if(data['next_cursor'] > 0) T.get('followers/ids', { screen_name: 'twitter', cursor: data['next_cursor'] }, getData);

  })

}

Parameter for Twit isn't "next_cursor", it's just "cursor" ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!