
First of all, I have to admit that I decided to take a vacation. Vacation from everything – work, freelance, projects, blog, etc. It’s summer and despite the crappy weather here in Toronto, I am spending time with my family which actually rocks. Now vacation is over, let’s get back to coding – today I wanted to continue with Twitter examples. Twitter seems to be very hot, and I was curious as to how to get the most out of it from the coding perspective. Here is a tiny function that gets the followers of a user, providing we know the username and password:
| 01 | public string FetchFollowers(string userName, string password) |
| 02 | { |
| 03 | using (var client = new WebClient()) |
| 04 | { |
| 05 | client.Credentials = new NetworkCredential(userName, password); |
| 06 | try |
| 07 | { |
| 08 | using (var stream = client.OpenRead("http://twitter.com/statuses/followers.xml")) |
| 09 | { |
| 10 | using (var reader = new StreamReader(stream)) |
| 11 | { |
| 12 | return reader.ReadToEnd(); |
| 13 | } |
| 14 | } |
| 15 | } |
| 16 | catch (WebException ex) |
| 17 | { |
| 18 | //insert code to deal with exception HERE |
| 19 | } |
| 20 | } |
| 21 | return string.Empty; |
| 22 | } |
There are two types of “connection” between the people on twitter – followers and friends – I am too lazy to look up the difference between the two, but if you want list of friends then line 8 in the function above should look like this:
| 1 | using (var stream = client.OpenRead("http://twitter.com/statuses/friends.xml")); |
I usually stuff the output of that in XML and then do what I need to do with it.
Let me take a break for now, next time we will cover posting to Twitter (status update).
BTW, first part of this could be found here

Currently I am involved in a project where number of things have to be done using Twitter API. What really amazes me today is the quality of API’s available to developers. Twitter is a perfect example of such API. I will try to publish some code that I used to accomplish several tasks starting with getting the details of a Twitter user providing you are aware of the user password and username.
Following code will retrieve a variety of information about a Twitter account:
| 01 | |
| 02 | public string FetchUserDetailsAsXml(string userName, string password, string IDorScreenName) |
| 03 | { |
| 04 | |
| 05 | if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) |
| 06 | { |
| 07 | throw new ArgumentException("userName or Password are not supplied, that is not good."); |
| 08 | } |
| 09 | |
| 10 | string url = string.Format(TwitterBaseUrlFormat, "users", "show" + "/" + IDorScreenName, "xml"); |
| 11 | |
| 12 | using (var client = new WebClient()) |
| 13 | { |
| 14 | client.Credentials = new NetworkCredential(userName, password); |
| 15 | |
| 16 | try |
| 17 | { |
| 18 | using (var stream = client.OpenRead(url)) |
| 19 | { |
| 20 | using (var reader = new StreamReader(stream)) |
| 21 | { |
| 22 | return reader.ReadToEnd(); |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | catch (WebException weex) |
| 27 | { |
| 28 | if (weex.Response is HttpWebResponse) |
| 29 | { |
| 30 | if ((weex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound) |
| 31 | { |
| 32 | return null; |
| 33 | } |
| 34 | } |
| 35 | throw; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | } |
| 40 | |
That’s pretty much it. Need System.Net and System.Web for this, obviously. What you get out of that looks like this:
| 01 | |
| 02 | |
| 03 | - <user> |
| 04 | <id>14381487</id> |
| 05 | <name>pzubkov</name> |
| 06 | <screen_name>pzubkov</screen_name> |
| 07 | <location>Ontario, Canada</location> |
| 08 | <description>Coder, mostly .net</description> |
| 09 | <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/56116140/Me_normal.jpg</profile_image_url> |
| 10 | <url>http://www.paul-zubkov.com</url> |
| 11 | <protected>false</protected> |
| 12 | <followers_count>39</followers_count> |
| 13 | <profile_background_color>709397</profile_background_color> |
| 14 | <profile_text_color>333333</profile_text_color> |
| 15 | <profile_link_color>FF3300</profile_link_color> |
| 16 | <profile_sidebar_fill_color>A0C5C7</profile_sidebar_fill_color> |
| 17 | <profile_sidebar_border_color>86A4A6</profile_sidebar_border_color> |
| 18 | <friends_count>61</friends_count> |
| 19 | <created_at>Mon Apr 14 05:08:22 +0000 2008</created_at> |
| 20 | <favourites_count>0</favourites_count> |
| 21 | <utc_offset>-18000</utc_offset> |
| 22 | <time_zone>Eastern Time (US Canada)</time_zone> |
| 23 | <profile_background_image_url>http://static.twitter.com/images/themes/theme6/bg.gif</profile_background_image_url> |
| 24 | <profile_background_tile>false</profile_background_tile> |
| 25 | <statuses_count>49</statuses_count> |
| 26 | <notifications>false</notifications> |
| 27 | <verified>false</verified> |
| 28 | <following>false</following> |
| 29 | - <status> |
| 30 | <created_at>Wed Jul 01 19:15:11 +0000 2009</created_at> |
| 31 | <id>2424620582</id> |
| 32 | <text>twitter api rocks, this is sweet</text> |
| 33 | <source></source>web |
| 34 | <truncated>false</truncated> |
| 35 | <in_reply_to_status_id> |
| 36 | <in_reply_to_user_id> |
| 37 | <favorited>false</favorited> |
| 38 | <in_reply_to_screen_name> |
| 39 | </in_reply_to_screen_name> |
| 40 | </in_reply_to_user_id> |
| 41 | </in_reply_to_status_id></status></user> |
As we can see from above, I am not an avid Twitter user myself, but I find that the API produced by Twitter is extremely well done. In Part 2 and 3 I will cover sending new update and getting followers.
Tweet, tweet, tweeeeeeet just keep the 140 char limit in mind. Looks like everyone and their grandma has drank the kool-aid on this twitter trend. I know this is supposed to be cool and useful, and at times it is, but this article is a prime example of why twitter is complete waste of time and effort for the most parts. Jennifer Von Grove goes on to explore some extraordinary examples of how to use twitter, honestly the use of the word “extraordinary” in this article is as appropriate as describing a process of passing a kidney stone using the same term. Let’s look at those, shall we? Read more…