Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflow reputation using PowerShell

Tags:

powershell

How can I view my reputation with a PowerShell function ?

like image 498
Andy Schneider Avatar asked Jun 03 '09 18:06

Andy Schneider


2 Answers

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.

like image 135
Andy Schneider Avatar answered Nov 15 '22 16:11

Andy Schneider


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
like image 19
Scott Saad Avatar answered Nov 15 '22 17:11

Scott Saad