Created
April 21, 2024 08:41
-
-
Save jpomfret/94eb263d262fd9c51738b6d73fad91ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Function to get a list of Quarterfinal scores from CrossFit Games API for a specific affiliate | |
.DESCRIPTION | |
Function to get a list of Quarterfinal scores from CrossFit Games API for a specific affiliate | |
.PARAMETER affiliateName | |
The name of the affiliate to filter by, e.g. 'CrossFit Southampton' | |
.PARAMETER division | |
The division to filter by, either 'Women' or 'Men' | |
.PARAMETER year | |
The year of the quarterfinals, defaults to this year. | |
.PARAMETER incScores | |
This switch will include the workout scores in the output | |
.EXAMPLE | |
PS> Get-CfQfAffiliateAthletes -affiliateName 'CrossFit Southampton' -Division Women | |
Gets a list of women athletes from CrossFit Southampton and their rank | |
.EXAMPLE | |
Get-CfQfAffiliateAthletes -affiliateName 'CrossFit Southampton' -Division Women -incScores | |
Gets a list of women athletes from CrossFit Southampton and their rank and workout scores | |
.NOTES | |
Jess Pomfret | |
jesspomfret.com | |
This goes through the API page by page - in testing it took about 2 mins for women, 4 mins for men. | |
#> | |
function Get-CfQfAffiliateAthletes { | |
param ( | |
$affiliateName, | |
[ValidateSet ('Women','Men')] | |
$division, | |
$year = (Get-Date).Year, | |
[Switch]$incScores | |
) | |
$div = switch ($division) { | |
'Men' {1} | |
'Women' {2} | |
} | |
Write-Verbose ("Getting first page to determine total pages for division {0}" -f $division) | |
$uri = 'https://c3po.crossfit.com/api/competitions/v2/competitions/quarterfinals/{0}/leaderboards?division={1}®ion=0&scaled=0&sort=0&quarterfinal=229&page={2}' | |
$data = ConvertFrom-Json (Invoke-WebRequest -uri ($uri -f $year, $div, 1)) # get page 1 | |
$athletes = @() | |
for ($p = 1; $p -le $data.pagination.totalPages; $p++) { | |
Write-Verbose ("Getting page {0} for division {1} and filtering by {2}" -f $p, $division, $affiliateName) | |
$page = ConvertFrom-Json (Invoke-WebRequest -uri ($uri -f $year, $div, $p)) | |
$athletes += $page.LeaderboardRows | | |
Where-Object { $_.entrant.affiliateName -eq 'CrossFit Southampton' } #| | |
} | |
# add a rank | |
Write-Verbose "Adding rank to athletes" | |
$rank = 1 | |
$athletes | ForEach-Object { | |
$_ | Add-Member -MemberType NoteProperty -Name Rank -Value $rank | |
$rank++ | |
} | |
# return based on rank | |
Write-Verbose "Returning athletes" | |
if($incScores) { | |
# foreach score available add a column to results | |
$athletes | ForEach-Object -PipelineVariable ath -Process { $_ } | ForEach-Object { | |
$ath.scores | ForEach-Object { | |
$ath | Add-Member -MemberType NoteProperty -Name ('Workout {0}' -f $_.ordinal) -Value $_.scoreDisplay | |
} | |
} | |
$athletes | Sort-Object Rank | Select-Object Rank, @{l='FirstName'; e={$_.entrant.firstName}}, @{l='LastName'; e={$_.entrant.lastName}}, overallRank, overallScore, Workout* | |
} else { | |
$athletes | Sort-Object Rank | Select-Object Rank, @{l='FirstName'; e={$_.entrant.firstName}}, @{l='LastName'; e={$_.entrant.lastName}}, overallRank, overallScore | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment