Skip to content

Searching Twitter Using Csharp

August 17, 2012

Somehow through a tweet I ran into WebEssentials 2012 and decided to try it out in my spanking new Visual Studio 2012. It works very nice, but what caught my attention was the Paste Special -> Paste Json as classes. As I wanted to try this I checked out the Twitter search api, which you can ask to return Json results. The Web essentials plugin did indeed work and it came up with this, though I slightly edited it as I didn’t want to use nested classes.

public class Metadata
    {
        public string result_type { get; set; }
    }

    public class Result
    {
        public string created_at { get; set; }
        public string from_user { get; set; }
        public int from_user_id { get; set; }
        public string from_user_id_str { get; set; }
        public string from_user_name { get; set; }
        public object geo { get; set; }
        public object id { get; set; }
        public string id_str { get; set; }
        public string iso_language_code { get; set; }
        public Metadata metadata { get; set; }
        public string profile_image_url { get; set; }
        public string profile_image_url_https { get; set; }
        public string source { get; set; }
        public string text { get; set; }
        public object to_user { get; set; }
        public int to_user_id { get; set; }
        public string to_user_id_str { get; set; }
        public object to_user_name { get; set; }
    }

    public class TwitterResponse
    {
        public double completed_in { get; set; }
        public long max_id { get; set; }
        public string max_id_str { get; set; }
        public string next_page { get; set; }
        public int page { get; set; }
        public string query { get; set; }
        public string refresh_url { get; set; }
        public Result[] results { get; set; }
        public int results_per_page { get; set; }
        public int since_id { get; set; }
        public string since_id_str { get; set; }
    }

Using restsharp I simply make a call to twitter and then parse the result through
Newtonsoft.Json‘s excellent json library. This is a simple Asp.net MVC website. Here is the Controller.

public ActionResult getTweets()
{
//
// retrieve last 10 tweets in #tweetfleet, this is the URL we want
//
string url = "http://search.twitter.com/search.json?q=%23tweetfleet&page=1&rpp=10";

var client = new RestClient("http://search.twitter.com/");
var req = new RestRequest(Method.GET);
req.Resource = "search.json";
req.AddParameter("q", "#tweetfleet");
req.AddParameter("page", 1);
req.AddParameter("rpp", 10);

var response = client.Execute(req);

JsonSerializer serializer = new JsonSerializer();
TwitterResponse tweets = (TwitterResponse)serializer.Deserialize(new JsonTextReader(new StringReader(response.Content)), typeof(TwitterResponse));

return View(tweets);
}

And this is the View:

@model myMvc.Models.TwitterResponse

@{
ViewBag.Title = "getTweets";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Latest Tweets in #tweetfleet</h2>

<p>
<ul>
@foreach (var item in Model.results)
{
<li>@item.from_user says @item.text</li>
}
</ul>
</p>

Needless to say the view could be much more fancy, but it’ll do for this example ! It results in something like this.

All in all pretty easy once you find out how to glue the various pieces together :).

Comments are closed.