VMh
VMh

Reputation: 1340

Get users that a given user is FOLLOWING (not followers) in LinqToTwitter

How can I get UserId and ScreenName of the users that a given user is FOLLOWING (not followers) in LinqToTwitter?

??

Upvotes: 2

Views: 133

Answers (1)

Joe Mayo
Joe Mayo

Reputation: 7513

The Twitter API uses the terms follower to mean people who follow a user and friends to mean people that a user follows and LINQ to Twitter continues that approach. So, you would use a Friendship/FriendshipType.FriendsList query, like this:

    static async Task FriendsListAsync(TwitterContext twitterCtx)
    {
        Friendship friendship;
        long cursor = -1;
        do
        {
            friendship =
                await
                (from friend in twitterCtx.Friendship
                 where friend.Type == FriendshipType.FriendsList &&
                       friend.ScreenName == "JoeMayo" &&
                       friend.Cursor == cursor &&
                       friend.Count == 200
                 select friend)
                .SingleOrDefaultAsync();

            if (friendship != null && 
                friendship.Users != null && 
                friendship.CursorMovement != null)
            {
                cursor = friendship.CursorMovement.Next;

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

        } while (cursor != 0);
    }

This example pages through the results in a do/while loop. Notice that the cursor is set to -1, which starts off the query without a Twitter API cursor. Each query assigns the cursor, which gets the next page of users. In the if block, the first statement reads the friendship.CursorMovement.Next to the get cursor for the next page of users. When the next cursor is 0, you've read all of the followers.

After the query executes, the Users property has a List<User> where you can get user information. This demo prints each member of the list.

One of the things you might run into with large friend lists is that Twitter will return an error for exceeding the rate limit. You'll be able to catch this, in a try/catch block, by catching TwitterQueryException and examining properties for the Rate Limit Exceeded. To minimize rate limit propblems, set count to 200, the max. Otherwise count defaults to 20.

You can download samples and view documentation for this on the LINQ to Twitter Web site.

Upvotes: 2

Related Questions