Michael Stephenson

keeping your feet on premise while your heads in the cloud
posts - 358 , comments - 427 , trackbacks - 11

My Links

News

View Michael Stephenson's profile on BizTalk Blog Doc View Michael Stephenson's profile on LinkedIn

Twitter












Archives

Post Categories

Image Galleries

BizTalk

Mates

Klout API to get a users Influence Rating

I've been working on a little project recently and one of the requirements was to be able to identify someone's influence in the social media space. I happened to come across Klout which if you are not familiar is a service where people can connect all of their social networking accounts together and it will then analyze your public activity and workout how much Klout you have. Even if you have not joined Klout it still looks at public Twitter information and can rate users just based on their public Twitter activity.

This sounded like a service worth investigating because it looks at who you follow and who follows you and things like retweets and mentions. It then rates you. This sounds like a good service based way of finding out how influential a user is.

The aim of this article is as much for myself as a reminder of this simple POC just to retrieve a score.

Start off by registering your application by signing up with the Klout developer site.

http://developer.klout.com/

Next the below class can wrap up interaction with Klout. You can see in the get user ratings method I take a list of user names and then format the url and make calls to Klout. The response is loaded into a Linq to XML XDocument so I can easily extract the score. A dictionary is returned of the user ratings.

There is probably tidier ways to do this and im sure we will refactor the code etc as we go through the project but for now the little sample below may be useful to someone as I haven't seen many C# samples online.

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Net;

using System.Xml.Linq;

using System.Xml;

using System.Diagnostics;

 

namespace Acme.Klout

{

/// <summary>

/// Wrapper for the klout service

/// </summary>

public class KloutClient

{

private string KloutKey

{

get { return "[ADD YOUR KEY HERE]"; }

}

public Dictionary<string, decimal> GetUserRatings(List<string> users)

{

const string urlFormat = @"http://api.klout.com/1/klout.xml?users={1}&key={0}";

var response = new Dictionary<string, decimal>();

var scoreDecimal = decimal.Zero;

 

foreach(var user in users)

{

var url = string.Format(urlFormat, KloutKey, user);

try

{

scoreDecimal = decimal.Zero;

var kloutResponse = GetKloutResponse(url);

var document = XDocument.Load(kloutResponse);

var score = document.Descendants("kscore").First().Value;

scoreDecimal = decimal.Parse(score);

}

catch (Exception ex)

{

Trace.WriteLine("Error getting klout rating: " + ex.ToString());

}

 

response.Add(user, scoreDecimal);

}

 

return response;

}

 

private Stream GetKloutResponse(string url)

{

var request = WebRequest.Create(url);

var response = request.GetResponse();

return response.GetResponseStream();

}

}

}

 

Print | posted on Sunday, January 15, 2012 8:09 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: