Displaying Tweets in ASP.NET

Background

The truth is that all these Social Networks have APIs that allow developers to integrate our applications with them, and they are great. There are also open source libraries that we could use (like "twitterizer"), and it works pretty well also. But the reality is that they have too many options and documentation to read... and (for me at least), it was not worth it to spend that time when my requirement was as simple as showing my latest tweets and videos in the company web page. I found a guy wondering in a forum how he couldn't find a simple example on how to do this same thing with these open source libraries or with the APIs, though I insist, to integrate whole applications, libraries and APIs are great!

What We'll Do

We'll see Twitter this time, and we'll use Twitter's feed to get a user's timeline (the tweets he or she sent). Twitter returns the information in several formats (rss, xml, json...) and we'll talk about XML in this post.

Using the Code

The variable "tweetUrl" that will be used in the code must be something like this:
tweetUrl = https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=jakhussain";
".xml" can be replaced by the desired format, and screen name would be the twitter screen name that we want to retrieve, in this example, my own.

public void GetPublicTweet()
    {
        string twitterUrl = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=jakhussain";

        WebClient _webClient = new WebClient();
        string strtwitter = _webClient.DownloadString(twitterUrl);

        XmlDocument _xmlDocument = new XmlDocument();
        _xmlDocument.LoadXml(strtwitter);
        DataSet ds = new DataSet();
       XmlNodeReader reader = new XmlNodeReader(_xmlDocument);
        ds.ReadXml(reader);

        DataTable dt = new DataTable();
        dt.Columns.Add("Titulo");
        dt.Columns.Add("Fecha");
        dt.Columns.Add("Link");

        DataRow dr;

        if (ds.Tables["status"] != null && ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dr = dt.NewRow();
                dr["Titulo"] = ds.Tables["status"].Rows[i][2].ToString();
                dr["Fecha"] = ds.Tables["status"].Rows[i][0].ToString();
                string lnk = "http://twitter.com/" + ds.Tables["user"].Rows[i][2].ToString() + "/statuses/";
                dr["Link"] = lnk + ds.Tables["status"].Rows[i][1].ToString();
                dt.Rows.Add(dr);
            }
        }

        dgTwit.DataSource = dt;
        dgTwit.DataBind();

        dt.Dispose();
    }

First, we create a WebClient to make a request. We pass the URL with the user we want to retrieve and the format.
We create an XmlDocument and read the string to fill that document.
We create a datatable with three columns: Title, Date and Link.
When we read our XML document into a dataset, we get three different tables: statuses, status and user.
All the information that we want to display is in "status" table. We retrieve the date(position 0), the guid(position 1, with this we'll construct the link to the tweet itself), tweet (position 2), we could also use the source (position 3) but it's enough for this example. We'll also use the table "user" to get the user name to create the link. As we're iterating through the dataset's tables, we extract the information we need to the datatable we created.
I have a DataGrid(dgTwit) in the aspx. You can create the layout you want. In these lines of code, we bind the DataTable we created to the DataGrid.

Points of Interest

I notice some differences between the strings that we obtain through XML and RSS format. Apart from having a different schema, XML does not show the retweets in the timeline of that user. Another important thing is that we are not going to receive any information if the user's tweets are protected (private).
Thanks for reading and please feel free to make comments, suggestions, questions or whatever you want.

Thanks!

Comments

Popular posts from this blog

Understanding Top line and Bottom line?

How to activate PF UAN account

Scrum Master vs Product Owner