开发者

Understanding Twitter API's "Cursor" parameter

开发者 https://www.devze.com 2022-12-12 02:22 出处:网络
I don\'t really get how to use the Curesor parameter in Twitter\'s API, for an exmaple - here. Am I supposed to make a new API call for each 100 followers?

I don't really get how to use the Curesor parameter in Twitter's API, for an exmaple - here. Am I supposed to make a new API call for each 100 followers?

I'd love it if someone could provide a PHP example for getti开发者_StackOverflow社区ng a full list of followers assuming I have more than 100...

Thanks in advance!


You need to pass the cursor value back to the API to get the next "chunk" of followers. Then you take the cursor parameter from that chunk and pass it back to get the next chunk. It is like a "get the next page" mechanism.


Since this question was asked, Twitter API has changed in many ways.

Cursor is used to paginate API responses with many results. For example, a single API call to obtain followers will retrieve a maximum of 5000 ids.

If you want to obtain all the followers of a user you will have to make a new API call, but this time you have to indicate the "next_cursor" number that was in your first response.

If it's useful, the following python code will retrieve followers from a given user.

It will retrieve a maximum of pages indicated by a constant.

Be careful not to be banned (i.e.: don't make more than 150 api calls/hour in anonymous calls)

import requests
import json
import sys


screen_name = sys.argv[1]
max_pages = 5
next_cursor = -1

followers_ids = []

for i in range(0,max_pages):
    url = 'https://api.twitter.com/1/followers/ids.json?screen_name=%s&cursor=%s' % (screen_name, next_cursor)
    content = requests.get(url).content
    data = json.loads(content)
    next_cursor = data['next_cursor']

    followers_ids.extend(data['ids'])

print "%s have %s followers!" % (screen_name, str(len(followers_ids)))


Despite you asked it some time ago, I hope this will be the more accurated answer.

  1. Twitter does not offer info about what next_cursor means. Only how cursor works.
  2. It is only an easy way for Twitter to manage pagination.
  3. «The function is intended to be used linearly, one cursor set at a time per user.» Source

«It's potentially less efficient for you but much more efficient for us.» Twitter Staff

BUT... Someone ask before in a broken link «are cursors persistents? seem that the answer is "yes"»

This means you can save your last cursor before 0 and continue with it next time.


Check out http://code.google.com/p/twitter-boot/source/browse/trunk/twitter-bot.php

foreach ($this->twitter->getFollowers(,0 ) as $follower)//the 0 is the page
    {
      if ($this->twitter->existsFriendship($this->user, $follower['screen_name'])) //If You  Follow this user
          continue;    //no need to follow now;
      try
      {
        $this->twitter->createFriendship($follower['screen_name'], true); // If you dont Follow Followit now
        $this->logger->debug('Following new follower: '.$follower['screen_name']);
      }
      catch (Exception $e)
      {
        $this->logger->debug("Skipping:".$follower['screen_name']." ".$e->getMessage());
      }

    }

  }
0

精彩评论

暂无评论...
验证码 换一张
取 消