The blog system I'm using has a Twitter widget that can be added to pages to show your latest tweets. I wasn't quite happy with it so here's a simple Twitter status ASP.NET user control that I put together using LINQ to XML and VB.NET.
The control gets the two latest tweets and caches them for 60 seconds. Just change the .Take(2) line in the LINQ if you want more or less tweets and the OutputCache tag if you want more or less caching. There's a VB.NET function to add hyperlinks to URLs and usernames and a Javascript function to format the time of tweets.
Here's the .ascx file:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Twitter.ascx.vb" Inherits="usercontrols_Twitter" %>
<%@ OutputCache VaryByParam="none" Duration="60" %>
<script language="javascript" type="text/javascript">
function relative_time(C) { var B = C.split(" "); C = B[1] + " " + B[2] + ", " + B[5] + " " + B[3]; var A = Date.parse(C); var D = (arguments.length > 1) ? arguments[1] : new Date(); var E = parseInt((D.getTime() - A) / 1000); E = E + (D.getTimezoneOffset() * 60); if (E < 60) { return "less than a minute ago" } else { if (E < 120) { return "about a minute ago" } else { if (E < (60 * 60)) { return (parseInt(E / 60)).toString() + " minutes ago" } else { if (E < (120 * 60)) { return "about an hour ago" } else { if (E < (24 * 60 * 60)) { return "about " + (parseInt(E / 3600)).toString() + " hours ago" } else { if (E < (48 * 60 * 60)) { return "1 day ago" } else { return (parseInt(E / 86400)).toString() + " days ago" } } } } } } };
</script>
<div>
<asp:Repeater ID="Repeater1" runat="server" EnableViewState="false">
<HeaderTemplate>
Twitter
</HeaderTemplate>
<ItemTemplate>
"<%#Eval("TweetText")%>"<br />
<script language="javascript" type="text/javascript">
document.write('<i>' + relative_time('<%#Eval("TweetTime")%>') + '</i>');
</script>
<br /><br />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</div>
And here's the code behind:
Imports System.Web
Imports System.Data
Imports System.Linq
Imports System.Xml.Linq
Imports System.Text.RegularExpressions
Partial Class usercontrols_Twitter
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Try
Dim myTwitterXML = XDocument.Load("http://twitter.com/statuses/user_timeline/[YOUR TWITTER USERNAME].xml")
Dim Twitters = (From Twitter In myTwitterXML.Descendants("status") _
Select TweetID = Twitter.Element("id"), _
TweetText = HyperlinkTweet(Twitter.Element("text")), _
TweetTime = CStr(Twitter.Element("created_at").Value)).Take(2)
Repeater1.DataSource = Twitters
Repeater1.DataBind()
Catch ex As Exception
End Try
End If
End Sub
Function HyperlinkTweet(ByVal strT As String) As String
Dim strR As String
strR = Regex.Replace(strT, "(\bhttp://[^ ]+\b)", "<a href=""$0"" target=""_blank"">$0</a>")
strR = Regex.Replace(strR, "(\A|\s)@(\w+)", "<a href=""http://twitter.com/$2"" title=""$0 on Twitter"">$0</a>")
Return strR End Function
End Class