Twitter Search via C#

While there are some examples of accessing the Twitter API via C# on the web, I couldn’t really find any good samples of how to do a search.  There were some examples that took the REST route and used raw http requests.  While others explored the JSON formatting capabilities of the twitter API.  In the end, I didn’t really care for a lot of the code I saw, and I wasn’t really concerned with what format I got the results in, just that I got the results.

After a bit of fiddling about, I decided to use the ATOM format, and the SyndicationFeed API that came with WCF.  I came up with this simple command line tool that lets you type a search query, and it’ll list the top 10 results.  The code is quite short and simple to understand.

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("twitter -> ");
            var input = Console.ReadLine();

            while (input != "exit")
            {
                var scrubbed = HttpUtility.UrlEncode(input);
                var reader = XmlReader.Create(
                     string.Format("http://search.twitter.com/search.atom?lang=en&q={0}", scrubbed));
                var feed = SyndicationFeed.Load(reader);

                foreach (SyndicationItem item in feed.Items)
                {
                    Console.WriteLine("\t{0} - {1}", item.Authors[0].Name, item.Title.Text);
                }

                Console.Write("\ntwitter -> ");
                input = Console.ReadLine();
            }
        }
    }

To use it, you just need to add references to System.ServiceModel.dll, and System.Web.dll.

Enjoy!

6 Comments »

  1. George Tsiokos Said,

    April 27, 2009 @ 3:28 pm

    Where is the download? (I’m lazy)

  2. Joel Martinez Said,

    April 27, 2009 @ 3:31 pm

    it’s easy:

    Open Visual Studio
    Go to file – new – project, and select a console app
    paste the code above into the program.cs file

    and Bam! the code has been downloaded ;-)

  3. Trevor Said,

    May 25, 2009 @ 10:25 am

    Hello, I am using .NET 1.1, can your code be adapted to 1.1?

    Trevor

  4. Reg Said,

    August 18, 2009 @ 9:53 am

    Hi,

    Why does the (feed) have 15 items for searches ?
    Is there a way to search and locate the latest 100 tweets ?? or all the tweets starting from a certain date ?? How to extend the search ??

    When using twitter search through http://search.twitter.com , we can browse the older tweets through “older” link at the bottom of the page. How to get all those tweets ?

    Any idea ?

    -Reg

  5. Joel Martinez Said,

    August 18, 2009 @ 11:04 am

    Hi Trevor, unfortunately no, this code uses WCF classes that I believe are only present in the latest framework (3.5) … or at best, 3.0. so it wouldn’t work in 1.1

  6. Joel Martinez Said,

    August 18, 2009 @ 11:05 am

    Hi Reg, I honestly don’t know the answer to those questions :-) I honestly just got where I got by browsing the API documentation (http://apiwiki.twitter.com/) … I’m sure that the answer to your questions and more can be found there.

RSS feed for comments on this post

Leave a Comment