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!

9 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.

  7. Swartskaap Said,

    February 10, 2011 @ 12:40 pm

    This is great but,

    I have been searching like a mad man for the API you used!! Where can i download System.ServiceModel.dll, and System.Web.dll to reference in my application? Would be very cool if you could answer quickly , cause im writing a project for varsity!! Thanks!

    Regards :)

  8. Joel Martinez Said,

    February 25, 2011 @ 12:08 am

    those two assemblies are part of the .net framework.

  9. Apoorv Kumar Upadhyay Said,

    August 17, 2011 @ 1:40 am

    Hi,
    I tried Your Code. I am getting some Weird Error. I work on .net 4.0 Client Profile framework(VS 2010) .I pasted the code included the needed service references(System.Web) and System.ServiceModel and tried to run that code. it gives me two errors
    1)The name “HttpUtility” doesnt exist in the current context
    2)The name ‘SyndicationFeed’ does not exist in the current context

    I edited the .csobj file but the reference is always there of the added references.
    I changed the .net Framework from .net 4.0 Client framework to .net Framework 4 , The HttpUtility issue has been resolved but still the second error remains the same… Unable to run the code.Help me out

RSS feed for comments on this post

Leave a Comment