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.
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.
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
Dim ds As New DataSet
Dim dt As New DataTable()
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()
dlTwitter.DataSource = dt
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:
When we read our XML document into a
dataset, we get three different tables:
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
DataList in the aspx. You can create the layout you want. You can see the result of mine in the picture above. In these lines of code, we bind the
DataTable we created to the
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
Doing this with YouTube is similar. I will write 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!
- 9th December, 2010: Initial post