StackOverflow reputation using PowerShell

后端 未结 3 528
野的像风
野的像风 2021-02-04 01:02

How can I view my reputation with a PowerShell function ?

3条回答
  •  春和景丽
    2021-02-04 01:26

    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
    

提交回复
热议问题