Auto-Filter your tweets with Tweet# and LINQ

Posted on Mon 01 June 2009 in General

Every so often on twitter a topic becomes so popular as to become annoying. If you follow a lot of people this happens pretty much on a daily basis.

This also gives rise to another twitter phenomenon - people announcing that they're filtering out #trendyTopic because of the noise it generates, thus contributing further to the noise.

This gave me an idea in passing that I immediately posted to twitter. "What if my client could be set to auto-filter out trending topics." I initially thought it was a silly idea, but then as I let it roll around in my noggin a bit I decided it could actually have some promise, if implemented properly.

So...if you're developing a twitter client (admit it, you are, everyone's doing it) and you're using TweetSharp to do it, here's some code to get you started. It's easy!

First, we need the code to get the current trends.

/// Gets current trends as a string enumeration
public static IEnumerable GetTrendingTopics()
    var search = FluentTwitter.CreateRequest()
    var response = search.Request();
    var trends = response.AsTrends();
    foreach ( var trend in trends.Trends )
       yield return trend.Name;

This code will get us today's trending topics.

Next, we'll get our friends' timeline

public static IEnumerable GetFriendsTimeline()
   var twitter = FluentTwitter.CreateRequest()
       .AuthenticateAs( UserName, Password )

   var response = twitter.Request();
   return response.AsStatuses();

Now that you have trends, and tweets, it's just a matter of writing a bit of LINQ code to filter out any tweets that have the trending string in the text.

   public static IEnumerable FilterTweets( IEnumerabletweets, IEnumerable
filterTexts )
      var filteredTweets = from t in tweets
      from f in filterTexts
          where t.Text.Contains(f)
          select t;
      return filteredTweets;

For the purposes of this example we'll just dump the tweets out to the console.

public static void WriteFilteredTweets( )
   var tweets = GetFriendsTimeline();
   var trends = GetTrendingTopics();
   var filtered = FilterTweets(tweets, trends);
   foreach ( var status in filtered )
       Console.WriteLine("{0} - {1}", status.User.ScreenName, status.Text);

This is obviously a fairly trivial example, and represents the nuclear option of tweet filtering. If I were to implement this fully I would probably want to show the user a list of trending topics and have a one-click filter option (i.e. one button or check box per trend) that automatically expired when the trending topic dropped off the list of current trends.

Other ways this could be better

  • Apply it except when you are @replied to in the tweet
  • Rather than simply hiding the tweets, move them to a less prominent area of your client
  • Adapt the code and use it to highlight trending tweets instead of hiding them