Twitter API Voting System

Posted by Richard Jones on Geeks with Blogs See other posts from Geeks with Blogs or by Richard Jones
Published on Thu, 25 Mar 2010 10:40:17 GMT Indexed on 2010/03/25 10:53 UTC
Read the original article Hit count: 556

Filed under:

So I blatantly got this idea from the MIX 10 event.

At MIX they held a rockband talent competition type thing (I’m not quite sure of all the details).    But the interesting part for me is how they collected votes.

They used Twitter (what else, when you have a few thousand geeks available to you).

The basic idea was that you tweeted your vote with a # tag, i.e

#ROCKBANDVOTE vote Richard

How cool….    So the question is how do you write something to collate and count all the votes?   Time to press the magic Visual Studio new Project button…

Twitter has a really nice API that can be invoked from .NET.  

This is the snippet of code that will search for any given phrase i.e #ROCKBANDVOTE

 

public static XmlDocument GetSearchResults(string searchfor)
        {
            return GetSearchResults(searchfor, "");
        }
 
        public static XmlDocument GetSearchResults(string searchfor, string sinceid)
        {
            XmlDocument retdoc = new XmlDocument();
 
            try
            {
                string url = "http://search.twitter.com/search.atom?&q=" + searchfor;
                if (sinceid.Length > 0)
                    url += "since_id=" + sinceid;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                WebResponse res = request.GetResponse();
                retdoc.Load(res.GetResponseStream());
                res.Close();
 
            }
            catch
            {
            }
            return retdoc;
        }
    }

I’ve got two overloads, that optionally let you pass in the last ID to look for as well as what you want to search for.

Note that Twitter rate limits the amount of requests you can send,  see

http://apiwiki.twitter.com/Rate-limiting

So realistically I wanted my app to run every hour or so and only pull out results that haven’t been received before (hence the overload to pass in the sinceid parameter).

I’ll post the code when finished that parses the returned XML.

© Geeks with Blogs or respective owner