Google Analytics Alternative

Search This Blog

Thursday, September 9, 2010

Using ASP.NET MVC 2 to Query Twitter's Public API - FIFA 2010

Using ASP.NET MVC 2 to Query Twitter's Public API - FIFA 2010

If you've been living under a rock for the last few years, you might not have heard of Twitter, but for the rest of us, it is a great communication tool. I have made countless friends with it including the authors of DotNetCurry! Thankfully Twitter has a public Application Programming Interface (API) which you can use and incorporate into your site, if you'd like to add Twitter feeds to it. This article demonstrates how to do it using ASP.NET MVC.
To see this in action, I'm going to create a small ASP.NET MVC 2 website. If you haven't got Microsoft Visual Studio 2010, you can download the Express edition here.
To begin with, let's take a look at the Twitter's API. The documentation is here, but for a brief overview, here's what you can do:
- search
- trends
- trends/current
- trends/daily
- trends/weekly
I'm going to be using the search API to search Twitter. Search returns tweets in two formats, json and atom. I'll be using the atom format for my code. Let's get started!
The tweet returned from Twitter will be in atom format, but I don't want to mess with that! I want a POCO object to play with, so I've created a class calledTwitterViewData to store the tweets. Here's the class:

public class TwitterViewData
public string AuthorName { get; set; }
public string AuthorUri { get; set; }

public string Content { get; set; }
public string Updated { get; set; }
public string Link { get; set; }
VB.NET (10.0)
Public Class TwitterViewData
Public Property AuthorName() As String
Public Property AuthorUri() As String
Public Property Content() As String
Public Property Updated() As String
Public Property Link() As String
End Class

That being done I'm next going to tackle the view. I need to make a web request to search for tweets. Thankfully making a web request in ASP.NET is simple. Once the data is returned, I'll use a LINQ query to transform the data to my TwitterViewData class and pass that to the view. Here's the action code below:
public ActionResult Index()
var request = WebRequest.Create("") as HttpWebRequest;
var twitterViewData = new List<TwitterViewData>();
if (request != null)
using (var response = request.GetResponse() as HttpWebResponse)
using (var reader = new StreamReader(response.GetResponseStream()))
var document = XDocument.Parse(reader.ReadToEnd());
XNamespace xmlns = "";
twitterViewData = (from entry in document.Descendants(xmlns + "entry")
select new TwitterViewData{
Content = entry.Element(xmlns + "content").Value,
Updated = entry.Element(xmlns + "updated").Value,
AuthorName = entry.Element(xmlns + "author").Element(xmlns + "name").Value,
AuthorUri = entry.Element(xmlns + "author").Element(xmlns + "uri").Value,
Link = (from o in entry.Descendants(xmlns + "link")
where o.Attribute("rel").Value == "image"
select new { Val = o.Attribute("href").Value }).First().Val
return View(twitterViewData);

Public Function Index() As ActionResult
Dim request = TryCast(WebRequest.Create(""), HttpWebRequest)
Dim twitterViewData = New List(Of TwitterViewData)()
If request IsNot Nothing Then
Using response = TryCast(request.GetResponse(), HttpWebResponse)
Using reader = New StreamReader(response.GetResponseStream())
Dim document = XDocument.Parse(reader.ReadToEnd())
Dim xmlns As XNamespace = ""
twitterViewData = (From entry In document.Descendants(xmlns + "entry")
Select New TwitterViewData With {.Content = entry.Element(xmlns + "content").Value, .Updated = entry.Element(xmlns + "updated").Value, .AuthorName = entry.Element(xmlns + "author").Element(xmlns + "name").Value, .AuthorUri = entry.Element(xmlns + "author").Element(xmlns + "uri").Value, .Link = (From o In entry.Descendants(xmlns + "link")Where o.Attribute("rel").Value = "image"
Select New With {Key .Val = o.Attribute("href").Value}).First().Val}).ToList()
End Using
End Using
End If
Return View(twitterViewData)
End Function

The code above is pretty self explanatory. I'm making a web request to the API via the following line of code:
var request = WebRequest.Create("") as HttpWebRequest;
From there I parse the atom string into an XDocument type so it's super easy to query.
var document = XDocument.Parse(reader.ReadToEnd());
Next I'm using LINQ to XML to query the data and return the query as a list of TwitterViewData types. That list is passed as the model to the view.
The view as always is quite simple. Here it is:
If you run the application, you'll get the latest searches on the 2010 FIFA world cup from Twitter. This is a cool way of interacting with Twitter. In an upcoming article I'll show you how to do this without ASP.NET.

No comments: