-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBSApi.psm1
115 lines (102 loc) · 3.03 KB
/
CBSApi.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
[cmdletbinding()]
Param()
function Invoke-CBSApi {
Param(
[Parameter(Mandatory=$true)][string]$queryendpoint,
[string]$queryparams
)
write-host "$Env:cbsurl/$queryendpoint`?version=$Env:apiversion&league_id=$Env:leagueid&response_format=$Env:responseformat&access_token=$Env:cbstoken&$queryparams"
$response = invoke-webrequest "$Env:cbsurl/$queryendpoint`?version=$Env:apiversion&league_id=$Env:leagueid&response_format=$Env:responseformat&access_token=$Env:cbstoken&$queryparams"
$obj = $response.content | ConvertFrom-Json
return $obj
}
function Send-SlackMessage {
Param(
[Parameter(Mandatory=$true)][string]$Body
)
$msgrequest = invoke-webrequest -Method POST -body $body $env:slackhookurl
return $msgrequest.rawcontent
}
function Send-SimpleSlackMessage {
Param(
[Parameter(Mandatory=$true)][string]$Text,
[Parameter(Mandatory=$true)][string]$Channel
)
$body = New-Object -TypeName psobject -Property @{
type = 'mrkdwn'
text = $Text
channel = $Channel
}
$body = $body | ConvertTo-JSON | ForEach-Object{[regex]::Unescape($_)}
$msgrequest = invoke-webrequest -Method POST -body $body $env:slackhookurl
return $msgrequest.rawcontent
}
function Get-TeamID {
Param(
[Parameter(Mandatory=$true)][string]$Team
)
if($Team -match "^[\d\.]+$") {
$theteam = Get-TeamInfo | Where-Object { $_.id -eq $Team }
}
else {
$theteam = Get-Teaminfo | Where-Object { $_.name -eq $Team }
}
if ($theteam) { return $theteam.id }
else { return }
}
function Get-Team {
Param(
[Parameter(Mandatory=$true)][string]$Team
)
if($Team -match "^[\d\.]+$") {
$theteam = Get-TeamInfo | Where-Object { $_.id -eq $Team }
}
else {
$theteam = Get-Teaminfo | Where-Object { $_.name -eq $Team }
}
if ($theteam) { return $theteam }
else { return }
}
function Get-Roster {
Param(
[Parameter(Mandatory=$true)][string]$Team
)
$response = Invoke-CBSApi -queryendpoint 'rosters' -queryparams "team_id=$team"
$rosters = $response.body.rosters
return $rosters
}
function Get-Stats {
Param(
[int]$year = '2019',
[Parameter(Mandatory=$true)][string]$team
)
$response = Invoke-CBSApi -queryendpoint 'stats' -queryparams "timeframe=$year&team_id=$team"
$stats = $response.body.league_stats.players
return $stats
}
function Get-Teaminfo {
Param(
)
$response = Invoke-CBSApi -queryendpoint 'teams'
$teams = $response.body.teams
return $teams
}
function Get-Contracts {
Param(
[Parameter(Mandatory=$true)][string]$Team
)
$roster = Get-Roster -Team $Team
$contracts = $roster.teams.players | Select-Object fullname, wildcards
return $contracts
}
function Get-SalaryCap {
Param(
[string]$Team
)
$contracts = Get-Contracts -Team $Team
$salary = 0
foreach($contract in $contracts) {
$salary += $contract.wildcards.salary
}
return $salary
}