Get Twitter, Facebook And Feed Readers Count In ASP.NET

2. September 2011 03:40

API ASP.NET Code Snippets Web  6 Comments

I am working on some web stuff these days and surfing around to look at some of the best web developer's work around the web. As I was surfing I came across one of the most popular and reputed web tutorials site Nettus+. I am not going talking about any tutorial or article published on this site, but one thing that attracts my attention was the way they are displaying their followers count on twitter, facebook likes and RSS readers.
This looks pretty cool and just out of curiosity I viewed the page source, hoping to find some script or API call which gets the count. But eventually I can't found anything. For Twitter and Facebook I know that there is an API and therefore I can get the twitter followers and facebook like count but don't know anything about the feed readers count. So I search the web get myself aware of the feedburner API. So let's take a look at the code.
 
Get Twitter Followers Count

To get your twitter followers count, I am making a REST API call. This call will return XML string and then I am going to parse the received XML using LINQ. You just need to pass your twitter user name to get the followers count.
public string GetTwitterFollowersCount(string UserName)
{
            XDocument xdoc = XDocument.Load("http://api.twitter.com/1/users/show.xml?screen_name=" + UserName + "&include_entities=false");
            return (from item in xdoc.Descendants("user")
                    select item.Element("followers_count").Value).SingleOrDefault();
}
Get Facebook Likes Count

To get total Facebook likes count you need to remember that you have to use the URL of your page for e.g. http://www.facebook.com/audi or http://www.facebook.com/nettutsplus and not just the name of your page. Check out the code:
public string GetFacebookLikes(string FaceBookURL)
{
         string URL = "https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22" + FaceBookURL + "%22";
         XElement xdoc = null;
         XElement counts = null;
         xdoc = XElement.Load(URL);

         IEnumerable<XElement> total_Like_count =
             from elem in xdoc.Descendants()
             where elem.Name.LocalName == "like_count"
             select elem;

         counts = total_Like_count.First();
         string FBLikes = Convert.ToString(counts.Value);
         return FBLikes;
}
Get RSS Feed Readers Count

To get the RSS feed readers count you need to pass the complete URL of your feed URL for e.g. http://feeds.feedburner.com/MidnightProgrammer
public string GetFeedReadersCount(string url)
{
            XDocument xdoc = XDocument.Load("http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=" + url);
            return (from item in xdoc.Descendants("entry")
                    select item.Attribute("circulation").Value).SingleOrDefault();
}

This is it, now it all depends on the designer or developer how to get the stats and present it in a more attractive way. I am not a designer and this is why I am not putting any effort to do something like this to show you this thing in action. At present I am working on JQuery version of the above code, so people who are willing to show stats on their blogs or website which are different platforms. Till that time, try the above code with ASP.NET and let me know of this helps you out.

Currently rated 3.9 by 7 people

Comments (6)

Aaron Crowder Aaron Crowder
9/2/2011 8:32:44 PM #

Great article! I'll probably be implementing a similar solution for my companies web site.

Raghavendra Raghavendra
10/28/2011 9:54:11 AM #

While getting face book count i am getting The remote server returned an error: (403) Forbidden.

prashant prashant
10/29/2011 11:35:01 AM #

Get the URL from the code and try running the code in the browser and check. I tried it and it is running correctly.

Raghavendra Raghavendra
11/21/2011 5:47:03 PM #

In browser its working fine but i need to get count from code behind

Prashant Prashant
11/21/2011 9:06:28 PM #

Sorry, but I don't understand. The above code is the server-side code and wiil work return the count to you. It will be goos if you can elaborate the error.

ahmadali shafiee ahmadali shafiee
8/11/2012 8:26:46 AM #

thanks. the feedReadersCount helped me.

Add Comment

Visit blogadda.com to discover Indian blogs Computers Blogs