CanCeylan
CanCeylan

Reputation: 3010

Get All Followers in Twitter with TweetSharp

I'm trying to get all followers by usin TweetSharp library. In twitter's api, it says we have an opportunity to use cursor variable in order to do pagination so that we would get all followers.

https://dev.twitter.com/docs/api/1.1/get/followers/list

https://dev.twitter.com/docs/misc/cursoring

But, is there any chance to do this operation with TweetSharp ? I'm writing the following code:

var options = new ListFollowersOptions { ScreenName = input };
IEnumerable<TwitterUser> friends = service.ListFollowers(options);

However this only returns the first 20, then I won't be able to keep track of any friends.

That would be great if you can help me.

Thanks.

Upvotes: 1

Views: 3639

Answers (1)

David Wilson
David Wilson

Reputation: 177

I'm using TweetSharp v2.3.0 in a .NET 4.0 WinForms app. Here's what works for me:

// this code assumes that your TwitterService is already properly authenticated

TwitterUser tuSelf = service.GetUserProfile(
    new GetUserProfileOptions() { IncludeEntities = false, SkipStatus = false });

ListFollowersOptions options = new ListFollowersOptions();
options.UserId = tuSelf.Id;
options.ScreenName = tuSelf.ScreenName;
options.IncludeUserEntities = true;
options.SkipStatus = false;
options.Cursor = -1;
List<TwitterUser> lstFollowers = new List<TwitterUser>();

TwitterCursorList<TwitterUser> followers = service.ListFollowers(options);

// if the API call did not succeed
if (followers == null)
{
    // handle the error
    // see service.Response and/or service.Response.Error for details
}
else
{
    while (followers.NextCursor != null)
    {
        //options.Cursor = followers.NextCursor;
        //followers = m_twService.ListFollowers(options);

        // if the API call did not succeed
        if (followers == null)
        {
            // handle the error
            // see service.Response and/or service.Response.Error for details
        }
        else
        {
            foreach (TwitterUser user in followers)
            {
                // do something with the user (I'm adding them to a List)
                lstFollowers.Add(user);
            }
        }

        // if there are more followers
        if (followers.NextCursor != null &&
            followers.NextCursor != 0)
        {
            // then advance the cursor and load the next page of results
            options.Cursor = followers.NextCursor;
            followers = service.ListFollowers(options);
        }
        // otherwise, we're done!
        else
            break;
    }
}

Note: This might be considered a duplicate question. (See here and here.) However, those existing questions seem to relate to a different (older?) version of TweetSharp, as there is no TwitterService.ListFollowersOf() method in version 2.3.0.

Upvotes: 1

Related Questions