Code to Get Twitter Follower's Count

Lakhangarg
Posted by Lakhangarg under ASP.NET category on | Views : 4760
When we send a request on the following url http://twitter.com/statuses/user_timeline/lakhangarg.xml?count=1 then the XML data will be returned as a XML.

This is sample XML retured from twitter:

<statuses type="array">
<status>
<created_at>Tue Oct 27 16:44:10 +0000 2009</created_at>
<id>5204855334</id>
<text>
http://bit.ly/oUB92 Reading: Create Script File For Each Store Procedure And Save into Seperate SQL File For Each Store Procedure
</text>
<source>web</source>
<truncated>false</truncated>
<in_reply_to_status_id/>
<in_reply_to_user_id/>
<favorited>false</favorited>
<in_reply_to_screen_name/>
<user>
<id>28538625</id>
<name>Lakhan Pal Garg</name>
<screen_name>lakhangarg</screen_name>
<location/>
<description/>
<profile_image_url>
http://a1.twimg.com/profile_images/319559736/PhotoFunia-19dab_normal.jpg
</profile_image_url>
<url>http://lakhangarg.blogspot.com/</url>
<protected>false</protected>
<followers_count>16</followers_count>
<profile_background_color>9ae4e8</profile_background_color>
<profile_text_color>000000</profile_text_color>
<profile_link_color>0000ff</profile_link_color>
<profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color>
<profile_sidebar_border_color>87bc44</profile_sidebar_border_color>
<friends_count>15</friends_count>
<created_at>Fri Apr 03 10:39:09 +0000 2009</created_at>
<favourites_count>0</favourites_count>
<utc_offset>-36000</utc_offset>
<time_zone>Hawaii</time_zone>
<profile_background_image_url>
http://s.twimg.com/a/1258070043/images/themes/theme1/bg.png
</profile_background_image_url>
<profile_background_tile>false</profile_background_tile>
<statuses_count>12</statuses_count>
<notifications/>
<geo_enabled>false</geo_enabled>
<verified>false</verified>
<following/>
</user>
<geo/>
</status>
</statuses>


Sample code to get the follower count from the XML data is:

 private static string GetTwitterFollowers(string TwitterURL)
{
try
{
if (TwitterURL != "")
{
TwitterURL = TwitterURL.Substring(TwitterURL.LastIndexOf('/') + 1);
Uri uri = new Uri("http://twitter.com/statuses/user_timeline/" + TwitterURL + ".xml?count=1");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.UserAgent = "Get Content";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.LoadXml(s);
string FollowerCount = xDoc.GetElementsByTagName("followers_count").Item(0).InnerText;
if (FollowerCount == "")
FollowerCount = "0";
return FollowerCount;
}
else
return "0";
}
catch
{
return "0";
}
}


Thanks & Regards
Lakhan Pal Garg

Comments or Responses

Login to post response