Sperick
Sperick

Reputation: 2761

LinqToTwitter: How to return more than 20 followers?

How can I modify this query to return all of a users followers as this only returns 20?

        var friendship = await twitterCtx.Friendship.Where(f => f.Type == FriendshipType.FollowersList)
                                                      .Where(f => f.ScreenName == "JoeBloggs")
                                                      .SingleOrDefaultAsync();

        if (friendship != null && friendship.Users != null)
            friendship.Users.ForEach(friend =>
                Console.WriteLine(
                    "ID: {0} Name: {1}",
                    friend.UserIDResponse, friend.ScreenNameResponse));

(I can see there is a Cursor property which is a long for paging results but I haven't been able to work out how to use it.)

Upvotes: 0

Views: 55

Answers (1)

Sperick
Sperick

Reputation: 2761

From searching around and reading the Twitter API documentation about Rate Limits I have the following which is working fine:

    public async Task<IEnumerable<ulong>> GetAllFollowers(string userID)
    {
        var allFollowerIds = new List<ulong>();
        Friendship followers;
        long cursor = -1;
        do
        {
            try
            {
                followers =
                await
                (from follower in twitterCtx.Friendship
                 where follower.Type == FriendshipType.FollowerIDs &&
                 follower.UserID == userID &&
                 follower.Cursor == cursor &&
                 follower.Count == 5000
                 select follower)
                .SingleOrDefaultAsync();
            }
            catch (TwitterQueryException tqe)
            {
                Console.WriteLine(tqe.ToString());
                break;
            }

            if (followers != null &&
            followers.IDInfo != null &&
            followers.IDInfo.IDs != null)
            {
                cursor = followers.CursorMovement.Next;

                allFollowerIds.AddRange(followers.IDInfo.IDs);
            }

        } while (cursor != 0);

        return allFollowerIds;
    }

This will return a maximum of 75,000 followers for a user authorized application. This is because there's a cap of 15 requests for this particular endpoint. You can make another batch of requests after 15 minutes has passed.

I haven't tried myself but if you need to pull down more followers then that for a particular user you should be able to do so by simply calling this method again with the last cursor value being fed in as the starting value.

Upvotes: 0

Related Questions