Introduction

Hi everyone!

Again this article is the fruit of a need I had. I've been searching for a couple of days how to show social networks' updates (from twitter, youtube, facebook...) and after getting a terrible headache, I decided to do it myself. And I want to share how I did it.

Background

The truth is that all this 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) was not worth it to spend that time when my requirement was as simple as showing my lastest 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 this open source libraries or with the APIs, though I insist, to integrate whole applications, libraries and APIs are great!  

What we'll do  

Well 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 

Using the Code

The variable "twitterUrl" that will be used in the code must be something like this:

twitterUrl = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=nereolopez"

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

   

Private Sub getTwitterInfo()        
        Dim str As String
        Dim client As New WebClient
        str = client.DownloadString(twitterUrl)

        Dim myXml As New XmlDocument
        myXml.LoadXml(str)

        Dim ds As New DataSet
        ds.ReadXml(New XmlNodeReader(myXml))

        Dim dt As New DataTable()
        dt.Columns.Add("Titulo")
        dt.Columns.Add("Fecha")
        dt.Columns.Add("Link")

        Dim row As DataRow
        If ds.Tables("status") IsNot Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
            For i As Integer = 0 To ds.Tables("status").Rows.Count - 1
                row = dt.NewRow()
                row("Titulo") = ds.Tables("status").Rows(i)(2).ToString()
                row("Fecha") = ds.Tables("status").Rows(i)(0).ToString()
                Dim lnk As String = "http://twitter.com/" & ds.Tables("user").Rows(i)(2).ToString() & "/statuses/"
                row("Link") = lnk & ds.Tables("status").Rows(i)(1).ToString()
                dt.Rows.Add(row)
            Next
        End If

        dlTwitter.DataSource = dt
        dlTwitter.DataBind()
        dt.Dispose()
    End Sub

twitterResult.jpg

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 XmlDocumentand 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 (posittion 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 DataList in the aspx. You can create the layout you want. You can see the result of mine's in the picture above. In these lines of code we bind the DataTable we created to the DataList. 

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

Doing this with YouTube is similar. I will go on an article on it in a couple days.

Thanks for reading and please feel free to make comments, suggestions, questions or whatever you want.

Hope this helps!

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"