How can I view my reputation with a PowerShell function ?
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.
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
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.
In Powershell v3 CTP1 it's shorter and easier:
function Get-StackOverflowReputation {
param($userID)
$JSON = (Invoke-WebRequest "http://stackoverflow.com/users/flair/$userid.json").content
convertfrom-Json $JSON
}
running
Get-StackOverflowReputation 1021945
returns:
id : 1021945
gravatarHtml :
profileUrl : http://stackoverflow.com/users/1021945/jon-z
displayName : jon Z
reputation : 1,271
badgeHtml : <span title="1 silver badge"><span class="badge2">●</span><span class="badgecount">1</span></span><span title="7 bronze badges"><span lass="badge3">●/span><span class="badgecount">7</span></span>