Monday, March 02, 2009 9:55 PM Jan Tielens

The Twitter Search API made easy with Linq to XML

My pal Lieven and I are preparing some cool demos to show at the Belgian TechDays SharePoint Preconference next week. Our goal is to build up a nice story that shows the various ways how to use SharePoint 2007 in combination with .NET 3.5. For some those demos we're going to make use of one of today's hypes: Twitter! This evening I tried to query Twitter data using Linq and I discovered there are lots of samples available on the net; there's even a complete Linq to Twitter provider on CodePlex. Too bad the Linq to Twitter provider is focused on the tweets for a specific Twitter user and I wanted to make use of Twitter's Search API. It turned out to be very, very easy (thanks to Robert Horvick's code snippets). Just dump the code at the bottom of this post in your .NET 3.5 project, and you can query the Twitter Search API using 'sharepoint' as the query like this:

foreach (U2U.Demos.TwitterSearch.Tweet t
    in U2U.Demos.TwitterSearch.Query("sharepoint"))
{
    MessageBox.Show(t.Title);
}

Here's the code for the TwitterSearch class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace U2U.Demos
{
    public class TwitterSearch
    {
        private const string urlTemplate = "
http://search.twitter.com/search.atom?q={0}";
        private static XNamespace atomNS = "
http://www.w3.org/2005/Atom";

        public static List<Tweet> Query(string query)
        {
            XDocument xDoc = XDocument.Load(string.Format(urlTemplate, query));

            var tweets =
            (from tweet in xDoc.Descendants(atomNS + "entry")
              select new Tweet
              {
                  Title = (string)tweet.Element(atomNS + "title"),
                  Published =
                    DateTime.Parse((string)tweet.Element(atomNS + "published")),
                  Id = (string)tweet.Element(atomNS + "id"),
                  Link = tweet.Elements(atomNS + "link")
                      .Where(link => (string)link.Attribute("rel") == "alternate")
                      .Select(link => (string)link.Attribute("href"))
                      .First(),
                  Author = (from author in tweet.Descendants(atomNS + "author")
                            select new Author
                            {
                                Name = (string)author.Element(atomNS + "name"),
                                Uri = (string)author.Element(atomNS + "uri"),
                            }).First(),
              });

            return tweets.ToList<Tweet>();
        }

        public class Tweet
        {
            public string Id { get; set; }
            public DateTime Published { get; set; }
            public string Link { get; set; }
            public string Title { get; set; }
            public Author Author { get; set; }
        }
        public class Author
        {
            public string Name { get; set; }
            public string Uri { get; set; }
        }
    }
}

Comments

# re: The Twitter Search API made easy with Linq to XML

Tuesday, March 03, 2009 2:20 AM by Andries den Haan

As always, great stuff

# re: The Twitter Search API made easy with Linq to XML

Tuesday, March 03, 2009 2:22 AM by Gene Vangampelaere

Nice !

I'm looking forward to see the demo on the TechDays!

# The Twitter Search API made easy with Linq to XML - Jan Tielens' Bloggings

Tuesday, March 03, 2009 9:48 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: The Twitter Search API made easy with Linq to XML

Thursday, March 05, 2009 1:00 AM by Matt Jimison

Thanks for the great article! It inspired me to throw together a configurable Twitter Search Webpart. I've got the solution and source code on my blog at:

www.mattjimison.com/.../twitter-search-webpart

Cheers!

# Blog: Jan Tielens&#039; Bloggings | Bscopes Feeds

Sunday, March 22, 2009 12:21 PM by Blog: Jan Tielens' Bloggings | Bscopes Feeds

Pingback from  Blog: Jan Tielens&#039; Bloggings | Bscopes Feeds

# re: The Twitter Search API made easy with Linq to XML

Wednesday, April 29, 2009 6:36 PM by JMayo

Hi Jan,

Thanks for the link to LINQ to Twitter. Just wanted to let you know that I've added Search to the latest Beta, v0.70: linqtotwitter.codeplex.com.

Regards,

Joe

# re: The Twitter Search API made easy with Linq to XML

Thursday, May 28, 2009 6:06 AM by Miklos Kanyo

Very useful, thanks a lot for this!!