Powershell Game Results

Sort:
TheLev79

Hi Everyone,

I'm very new to chess.com but quite addicted already and was stoked to discover this API !!  Only just found it a few hours ago but knocked up a little Powershell (my preferred language) and thought I'd share (since nobody else I know "in real life" will appreciate it)...

Clear-Host

$myUser = Read-Host "Your chess.com username"
$opponent = Read-Host "Opponent's username (leave blank for all opponents)"
if (!$opponent) { $opponent = ".*"}

$myArchives = Invoke-WebRequest -Uri https://api.chess.com/pub/player/$myUser/games/archives | ConvertFrom-Json

$games = @()
foreach ($archive in $myArchives.archives) {
$games += Invoke-WebRequest -Uri $archive | select -ExpandProperty content | ConvertFrom-Json
}

$results = $games.games | select white,black

$whiteWins = 0
$whiteLosses = 0
$whiteDraws = 0
$whiteAbandons = 0
$blackWins = 0
$blackLosses = 0
$blackDraws = 0
$blackAbandons = 0

foreach ($row in $results) {

if (($row | select -ExpandProperty white | select -ExpandProperty username) -eq $myUser `
-and ($row | select -ExpandProperty black | select -ExpandProperty username) -match $opponent) {

switch (($row | select -ExpandProperty white | select -ExpandProperty result)) {

"win" { $whiteWins += 1}
"checkmated" { $whiteLosses += 1}
"resigned" { $whiteLosses += 1}
"agreed" { $whiteDraws += 1}
"abandoned" { $whiteAbandons += 1}
"timeout" { $whiteLosses += 1}
}

}
if (($row | select -ExpandProperty black | select -ExpandProperty username) -eq $myUser `
-and ($row | select -ExpandProperty white | select -ExpandProperty username) -match $opponent) {

switch (($row | select -ExpandProperty black | select -ExpandProperty result)) {

"win" { $blackWins += 1}
"checkmated" { $blackLosses += 1}
"resigned" { $blackLosses += 1}
"agreed" { $blackDraws += 1}
"abandoned" { $blackAbandons += 1}
}

}

}

Write-Host "`n$myUser playing as White...
Wins: $whiteWins
Losses: $whiteLosses
Draws: $whiteDraws
Abandoned: $whiteAbandons"

Write-Host "`n$myUser playing as Black...
Wins: $blackWins
Losses: $blackLosses
Draws: $blackDraws
Abandoned: $blackAbandons"

Write-Host "`n$myUser Totals...
Wins: $($blackWins + $whiteWins)
Losses: $($blackLosses + $whiteLosses)
Draws: $($blackDraws + $whiteDraws)
Abandoned: $($blackAbandons + $whiteAbandons)"


WhiteDrake

Cool happy.png 

skelos

Thanks. Powershell is not something I've tried (Windows is for running PGN Spy ... only!? wink.png) but have discussed Powershell with someone I respect who's done good stuff (not related to chess) with it. Thanks for giving us another example of accessing api.chess.com from a different language.

I believe Windows is quite widely used, so this may be helpful to others. grin.png

TheLev79

Updated version available here, which now breaks down the results by each time class...

git clone https://mattleventhal@bitbucket.org/mattleventhal/chess.git