Archive

Posts Tagged ‘Code Samples’

Twitter API with C# example(part 2)

August 2nd, 2009 Paul No comments

sundial

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:

public string FetchFollowers(string userName, string password)
{
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(userName, password);
try
{
using (var stream = client.OpenRead("http://twitter.com/statuses/followers.xml"))
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (WebException ex)
{
//insert code to deal with exception HERE
}
}
return string.Empty;
}

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:

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

Twitter API with C# example(part 1)

July 1st, 2009 Paul No comments

sunflower

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:

public string FetchUserDetailsAsXml(string userName, string password, string IDorScreenName)
{

if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
{
throw new ArgumentException("userName or Password are not supplied, that is not good.");
}

string url = string.Format(TwitterBaseUrlFormat, "users", "show" + "/" + IDorScreenName, "xml");

using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(userName, password);

try
{
using (var stream = client.OpenRead(url))
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (WebException weex)
{
if (weex.Response is HttpWebResponse)
{
if ((weex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
{
return null;
}
}
throw;
}
}

}

That’s pretty much it. Need System.Net and System.Web for this, obviously. What you get out of that looks like this:

-
14381487
pzubkov
pzubkov
Ontario, Canada
Coder, mostly .net

http://s3.amazonaws.com/twitter_production/profile_images/56116140/Me_normal.jpg http://www.paul-zubkov.com

false 39

709397 333333 FF3300 A0C5C7 86A4A6 61
Mon Apr 14 05:08:22 +0000 2008
0
-18000
Eastern Time (US Canada)

http://static.twitter.com/images/themes/theme6/bg.gif false 49
false
false
false
-
Wed Jul 01 19:15:11 +0000 2009
2424620582
twitter api rocks, this is sweet
web

false

false



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.

Displaying list of installed fonts with C#

September 28th, 2008 Paul No comments

I know that all 1337 hax0rz (sorry, I promise I will not be doing this any more, as a matter of fact it really bugs me when people are using this 1337 crap) are using stuff like Ruby and Python, I know that system programming is for old farts and the future is in web development and all that. I am not disputing this, but there are some cool things you can do with C# and Windows coding. For some bizzare reason, I feel that it might be a good idea to publish some code which I find neat. One of the reasons I like C# is that generally if you need a solution to a particular problem, it is most likely to be alot simpler then what I think. Here is how you get the list of all installed fonts loaded into a ComboBox. Read more…