tags:

views:

445

answers:

2

How can I view my reputation with a PowerShell function ?

+21  A: 

You can use the following function

Function Get-StackOverFlowReputation {
param($userID)
    $client = new-object System.Net.WebClient
    $JSONFlair = $client.DownloadString("http://stackoverflow.com/users/flair/$userid.json")
    $JSONFlair.split(",") | select-string "reputation","displayName"
}


260 >  Get-StackOverFlowReputation -userID 45571

"displayName":"Andy Schneider"
"reputation":"344"

It's quick and dirty. I am sure you could use some nifty library to convert JSON to a PSobject but this will get the job done.

Andy Schneider
Hmm, perhaps DataContractJsonSerializer? It's part of .NET 3.5. But I think it's also overkill.
Jay Bazuzi
There is a typo in the third line. $clnt should be $client.
aphoria
@aphoria - thanks for the note - code is updated
Andy Schneider
+1 -- cool answer! I had to add one line of code to your script because we have to authenticate to our proxy server. it was:$client.Proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentialsAlso, 1 syntax error (renamed $clnt to $client)
JMarsch
I just can't resist porting this to cmd.exe
grawity
Hmm, goes slightly wrong here for 73070. DisplayName has the wrong encoding and the rep is cut off after the first digit (presumably because of the included , when rep > 1000).
Joey
+6  A: 

This question looked very fun and I had to give it a try even though its already has an accepted answer. Plus, the accepted answer does not seem to properly work for reputations that are greater than 999 (i.e. 1,000 contains a comma which is being also being split).

Being that the format of Flair is in JSON, simply splitting on it does not always work and regex against JSON is almost impossible. While there are .NET JSON libraries out there I wanted to keep the solution all within PowerShell (including V1).

The following uses the 3.5 JavaScriptSerializer class, which requires us to load the assembly in our script.

Update

With PowerShell 2.0 it's a lot easier to create "custom objects" with hashes.

function Get-StackOverflowReputation 
{
    param ( $UserId )
    $assembly = [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
    $client = New-Object System.Net.WebClient
    $json = $client.DownloadString("http://stackoverflow.com/users/flair/$UserId.json")
    $transmogrifer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
    $jsonFlair = $transmogrifer.DeserializeObject( $json ) 
    $flair = New-Object PSObject -Property @{ user = $jsonFlair["displayName"]; rep = $jsonFlair["reputation"] }
    $flair
}

1> Get-StackOverflowReputation -UserId 45571      
    user                 rep
    ----                 --- 
    Andy Schneider       779
Scott Saad
I was hoping someone was going to figure out how to use the javascript serializer. This is definitely much more elegant than my original solution. My only suggestion would be to convert the resulting dictionary into a property bag so you can pipe to format-list or format-table so the function would look like this at the end.$flair = $transmogrifer.DeserializeObject( $json ) $obj = "" | select Name,Reputation,Id$obj.Name = $flair.displayName$obj.Reputation = $flair.reputation$obj.id = $flair.id$obj
Andy Schneider
With PowerShell 2.0's easier and more elegant way of creating custom objects, I updated the code to take advantage and produce an actual object.
Scott Saad