Consuming a Twitter Feed on BlogEngine.Net

8. March 2009 20:14 by Brian in Projects - Comments: (0)

After playing with Twitter and the Twitter API, I thought that it would be interesting to try to write a BlogEngine.Net compitable page to display the latest updates from my Twitter feed as you can view here.

All Twitter users have an xml / rss feed which is accessible via http://twitter.com/statuses/user_timeline/briandorey.xml . Simply change the briandorey to your Twitter username and try to load the url in your browser. You will be asked to authenticate with your Twitter username and password and this will then return an xml document of your latest posts.

I would like to thank Albett Pascual for the code parts for the Twitter authentication section.

The BlogEngine.Net page to show a Twitter feed is comprised of three files:

twitter.aspx - the main display page

twitter.aspx.cs - the code behind page

twitter.xsl - the xml transform document.

Below is the code for each of these pages:


twitter.aspx
This page will need to be customised to use your own Twitter url and can be styled to suit your blog theme.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="twitter.aspx.cs" Inherits="twitter" Title="Twitter Feed" %>
<%@ OutputCache Duration="3600" VaryByParam="none" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server">
    <style>
        #twittercontent { margin: 20px;}
        .twittertext { font-size: 120%; font-weight:bold; padding: 0px; margin: 0px;}
        .twitterdate { font-size: 90%; font-style:italic; padding: 0px; margin: 0 0 20px 0;}
    </style>
    <div id="twittercontent">
        <h1>Brian's Twitter Feed</h1>
        <p>Latest posts and updates from my Twitter page at <a href="http://twitter.com/briandorey">http://twitter.com/briandorey</a></p>
        <asp:xml id="xmlRss" runat="server" />
        <asp:Label ID="ErrorMsg" runat="server"></asp:Label>
    </div>
</asp:Content>


twitter.aspx.cs
This page will need to be customised to use your own Twitter url.

#region Using

using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;

#endregion

public partial class twitter : BlogEngine.Core.Web.Controls.BlogBasePage
{
  protected void Page_Load(object sender, EventArgs e)
  {
   try {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/user_timeline/briandorey.xml"); 
    request.ContentType = "application/xml";
    request.AllowWriteStreamBuffering = true;
    request.UserAgent = "GeoTwitter";

    WebResponse resp = request.GetResponse();
    StreamReader sr = new StreamReader (resp.GetResponseStream());
    XmlDocument myXmlDocument = new XmlDocument();
    string xmlString = sr.ReadToEnd();
    sr.Close();

    myXmlDocument.LoadXml(xmlString);
    xmlRss.Document = myXmlDocument;
    xmlRss.TransformSource = "twitter.xsl";
   } catch {
    ErrorMsg.Text = "Error loading twitter feed";
   }
 }
}


twitter.xsl
This is the xsl transform file to format your Twitter feed entries

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:apply-templates select="statuses" />
</xsl:template>
<xsl:template match="status">
    <p class="twittertext"><xsl:value-of select="text" /></p>
    <p class="twitterdate">Posted:
    <xsl:variable name="content" select="."/>
    <xsl:call-template name="replace-string">
    <xsl:with-param name="text" select="created_at"/>
    <xsl:with-param name="replace" select="'+0000'"/>
    <xsl:with-param name="with" select="''"/>
    </xsl:call-template>
    </p>
</xsl:template>

<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
        <xsl:when test="contains($text,$replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:value-of select="$with"/>
            <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="substring-after($text,$replace)"/>
            <xsl:with-param name="replace" select="$replace"/>
            <xsl:with-param name="with" select="$with"/>
            </xsl:call-template>
        </xsl:when>
       <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>


Please Note: This code may contain bugs or crash your server and burn down your house, use at your own risk!

Tags: , , - Posted in: Projects

Comments are closed

RecentComments

Comment RSS

 

 

About the Blog

Welcome to my space on the web! I made this site as a central place with various links and information for my various hobbies, sports and interests and for anyone who knows me to find out what I have been doing since I left school.

My LinkedIn Profile

My Flickr Profile

My Twitter Profile

Powered by BlogEngine.NET 1.6.0.0 - Theme by apexweb - Log in