November 2007
Posted on the 2nd at 9:37 AM CST
Getting the Number of Subscribers to your FeedBurner Feed in ASP.NET
FiledFiled under ASP.NET, VB.NET

FeedBurner is a pretty amazing service. They provide a lovely image for you to display the number of subscribers on your web site. But what if you want to format that data to be uniform with the rest of your web site design? They got that covered, too. There is even a cool service out there that grants you the ability to subscribe to your feed stats via RSS!

This feed data is made available to us by FeedBurner's convenient Awareness API. You will have to activate the API through your account (it is located under the the "Publicize" tab) in order to use it. I searched the web looking for a .NET function to return the number of subscribers, but Google did not hook it up. So, I wrote one myself. You simply pass it the name of your feed (feeds.feedburner.com/NAME_HERE) and it will return the number of subscribers. There is a second parameter that expects the contexts Cache object. This will store the number in cache for 6 hours to help promote efficiency. Otherwise, you can just pass it Nothing and the number will not be stored in cache. It will return zero if there are no subscribers to your feed or if anything unexpected happens. Note that you will need to import the System.Net and System.Xml namespaces for this function to work. Here it is...

    ''' <summary>
    ''' Gets the number of subscribers to the specified FeedBurner feed
    ''' </summary>
    ''' <param name="FeedName">The name of the FeedBurner Feed (ex: http://feeds.feedburner.com/NAME_HERE )</param>
    ''' <param name="Cache">Reference to the contexts cache. Pass Nothing if you do not want to store the result in cache.</param>
    ''' <returns>Number of subscribers. Will return zero if an error occurs.</returns>
    Public Shared Function GetNumberOfSubscribers(ByVal FeedName As String, ByRef Cache As Caching.Cache) As Integer
        Const CacheId As String = "Counts.FeedburnerSubscribers"
        Const FeedBurnerApi As String = "http://api.feedburner.com/awareness/1.0/GetFeedData?uri={0}"
        Dim Result As Integer = 0

        If Cache Is Nothing OrElse Cache.Item(CacheId) Is Nothing Then
            Dim Url As String = String.Format(FeedBurnerApi, FeedName)
            Dim FeedBurner As String

            Try
                Using Client As New WebClient()
                    FeedBurner = Client.DownloadString(Url)
                End Using
            Catch ex As WebException
                'Logworthy?
                FeedBurner = String.Empty
            End Try

            If Not String.IsNullOrEmpty(FeedBurner) Then
                Dim XmlDoc As New XmlDocument()
                Dim ResponseNode As XmlNode
                Dim StatusAttribute As XmlAttribute

                Try
                    XmlDoc.LoadXml(FeedBurner)

                    ResponseNode = XmlDoc.SelectSingleNode("rsp")
                    StatusAttribute = ResponseNode.Attributes.ItemOf("stat")

                    If StatusAttribute IsNot Nothing AndAlso StatusAttribute.Value = "ok" Then
                        Dim EntryNode As XmlNode = ResponseNode.SelectSingleNode("feed/entry")

                        If EntryNode IsNot Nothing Then
                            Int32.TryParse(EntryNode.Attributes.ItemOf("circulation").Value, Result)

                            If Cache IsNot Nothing Then
                                Cache.Insert(CacheId, Result, Nothing, Now.AddHours(6), Nothing)
                            End If
                        End If
                    End If
                Catch ex As NullReferenceException
                    'Logworthy?
                End Try
            End If
        Else
            Int32.TryParse(Cache.Item(CacheId).ToString(), Result)
        End If

        Return Result
    End Function


I decided to put the function in my common utility class. I realize I could have used HttpContext.Current.Cache instead of having it as a second parameter, but I make an effort to avoid using HttpContext.Current in my utility classes. So, that's it for now. There is a couple other useful APIs listed on the FeedBurner Developers page, go check them out.

Enjoy!

Comments (4)
Permalink Comment from Gilchrist on November 2nd, 2007 at 9:56 AM
website is total bakwas...
Permalink Comment from Josh StodolaEmail on November 2nd, 2007 at 10:47 AM
Hooray for Indian slang! Thanks for the ever-so constructive feedback...
Permalink Comment from SeirelmNarBer on January 12th, 2008 at 10:44 PM
Make peace, not war!
Permalink Comment from Josh StodolaEmail on January 12th, 2008 at 10:46 PM
Amen!

Guess What?

There are a few basic guidelines you should be aware of before leaving a comment…

  • If you choose to display your email address, it will not be detected by spam bots
  • Comments are limited to 3,000 characters; so far you have used none of them
  • HTML will be encoded; links and line breaks will be converted automatically
  • Comments containing five or more links will be subject to moderation

Have Your Say

← Answer this to prove you are human
 
 

Chill Out…

No Trackbacks