Manage your Twitter Friends and Followers with Tweetsharp - Part 1

Posted on Sat 07 November 2009 in General

I've been looking for a better way to keep track of my Twitter friends and followers for a while now. I'm particularly bad at not following back interesting people who follow me because the emails from Twitter don't contain the user's bio information and I'm often too busy or distracted to go and login to the twitter.com to see if the new follower merits a follow-back (i.e. isn't a fembot or some kind of "social media expert" promising to bring me riches, or a billion followers, or a billion rich followers).

To that end, I'm working on a small utility app explicitly for managing your social graph, which I will blog about as I build it, then ultimately make available as a download.

So, for part 1, I'm going to start with finding users who follow me, but who I don't follow back. This is pretty easy to do with a few Tweetsharp calls and a bit of LINQ.

First, I'll get all of my friends (people I follow):

   long? nextCursor = -1; //creates a new cursor the first time we call
GetCursor()
   var friends = new List();
   do
   {
       var twitter = FluentTwitter.CreateRequest()
           .AuthenticateAs(_userName, _password)
           .Users().GetFriends()
           .GetCursor(nextCursor.Value)
           .AsJson();

       var response = twitter.Request();
       var users = response.AsUsers();
       if (users != null)
       {
           friends.AddRange(users);
       }
       nextCursor = response.AsNextCursor();
       } while (nextCursor.HasValue && nextCursor.Value != 0);

The code to get followers is almost identical, except we call 'GetFollowers' instead of 'GetFriends':

    var response = twitter.Request();
    var users = response.AsUsers();
    if (users != null)
    {
        followers.AddRange(users);
    }
    nextCursor = response.AsNextCursor();
    } while (nextCursor.HasValue && nextCursor.Value != 0);

At this point we have two TwitterUser collections, one containing friends and the other containing our followers. We can use LINQ's extension methods to find the objects that exist in only one or the other collection with a single call:

//get people who follow us, but don't follow back (must be spammers)
var spammers = followers.Except(friends);

And that's it. Coming up in Part 2, we'll refactor the code that gets friends and followers to reduce duplication, then we'll start building out some UI so that we can act on the information we have.