This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
293 lines (249 loc) · 7.92 KB
/
api.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package yfantasy
import (
"fmt"
"net/http"
"strings"
"github.com/famendola1/yfantasy/query"
"github.com/famendola1/yfantasy/schema"
)
// Enum of types when requesting for stats.
const (
StatsTypeUnknown = iota
StatsTypeSeason
StatsTypeAverageSeason
StatsTypeDate
StatsTypeLastWeek
StatsTypeLastWeekAverage
StatsTypeLastMonth
StatsTypeLastMonthAverage
)
// YFantasy is the client for the Yahoo Fantasy API.
type YFantasy struct {
client *http.Client
}
// New returns a new YFantasy object.
func New(client *http.Client) *YFantasy {
return &YFantasy{client: client}
}
// MakeLeagueKey creates a league key from the gameKey and leagueID.
func MakeLeagueKey(gameKey string, leagueID int) string {
return fmt.Sprintf("%s.l.%d", gameKey, leagueID)
}
// League queries the Yahoo Fantasy API for a League.
func (yf *YFantasy) League(leagueKey string) (*schema.League, error) {
fc, err := query.League().Key(leagueKey).Standings().Get(yf.client)
if err != nil {
return nil, err
}
return &fc.League, nil
}
// Standings queries the Yahoo Fantasy API for a leagues Standings.
func (yf *YFantasy) Standings(leagueKey string) (*schema.Standings, error) {
fc, err := query.League().Key(leagueKey).Standings().Get(yf.client)
if err != nil {
return nil, err
}
return &fc.League.Standings, nil
}
// CurrentScoreboard queries the Yahoo Fantasy API for a league's current scoreboard.
func (yf *YFantasy) CurrentScoreboard(leagueKey string) (*schema.Scoreboard, error) {
fc, err := query.League().Key(leagueKey).CurrentScoreboard().Get(yf.client)
if err != nil {
return nil, err
}
return &fc.League.Scoreboard, nil
}
// Scoreboard queries the Yahoo Fantasy API for the scoreboard of a given week.
func (yf *YFantasy) Scoreboard(leagueKey string, week int) (*schema.Scoreboard, error) {
fc, err := query.League().Key(leagueKey).Scoreboard(week).Get(yf.client)
if err != nil {
return nil, err
}
return &fc.League.Scoreboard, nil
}
// Rosters queries the Yahoo Fantasy API for all the team rosters in a league.
func (yf *YFantasy) Rosters(leagueKey string) (*schema.Teams, error) {
fc, err := query.League().Key(leagueKey).Teams().Roster().Get(yf.client)
if err != nil {
return nil, err
}
return &fc.Teams, nil
}
// Team searches the given league for a team with the provided team name.
// If the team is not found an error is returned.
func (yf *YFantasy) Team(leagueKey, teamName string) (*schema.Team, error) {
fc, err := query.League().Key(leagueKey).Teams().Get(yf.client)
if err != nil {
return nil, err
}
for _, tm := range fc.League.Teams.Team {
if strings.ToLower(tm.Name) == strings.ToLower(teamName) {
return &tm, nil
}
}
return nil, fmt.Errorf("team %q not found", teamName)
}
// TeamRoster searches the given league for a team with the provided team name
// and return's its roster. If the team is not found an error is returned.
func (yf *YFantasy) TeamRoster(leagueKey, teamName string) (*schema.Team, error) {
fc, err := query.League().Key(leagueKey).Teams().Roster().Get(yf.client)
if err != nil {
return nil, err
}
for _, tm := range fc.League.Teams.Team {
if strings.ToLower(tm.Name) == strings.ToLower(teamName) {
return &tm, nil
}
}
return nil, fmt.Errorf("team %q not found", teamName)
}
// TeamStats searches the given league for a team with the provided team name
// and return's its stats. If the team is not found an error is returned.
func (yf *YFantasy) TeamStats(leagueKey, teamName string, statsType int) (*schema.Team, error) {
q := query.League().Key(leagueKey).Teams().Stats()
switch statsType {
case StatsTypeUnknown:
return nil, fmt.Errorf("unknown stats type requested")
case StatsTypeSeason:
q = q.CurrentSeason()
break
case StatsTypeAverageSeason:
q = q.CurrentSeasonAverage()
break
case StatsTypeDate:
q = q.Today()
break
case StatsTypeLastWeek:
q = q.LastWeek()
break
case StatsTypeLastWeekAverage:
q = q.LastWeekAverage()
break
case StatsTypeLastMonth:
q = q.LastMonth()
break
case StatsTypeLastMonthAverage:
q = q.LastMonthAverage()
break
}
fc, err := q.Get(yf.client)
if err != nil {
return nil, err
}
for _, tm := range fc.League.Teams.Team {
if strings.ToLower(tm.Name) == strings.ToLower(teamName) {
return &tm, nil
}
}
return nil, fmt.Errorf("team %q not found", teamName)
}
// Player searches the given league for a player with the provided player name.
// If the player is not found, an error is returned. name should contain at
// least 3 letters.
func (yf *YFantasy) Player(leagueKey, name string) (*schema.Player, error) {
if len(name) < 3 {
return nil, fmt.Errorf("name (%q) must contain at least 3 letters", name)
}
fc, err := query.League().Key(leagueKey).Players().Search(name).Get(yf.client)
if err != nil {
return nil, err
}
for _, p := range fc.League.Players.Player {
if strings.ToLower(p.Name.Full) == strings.ToLower(name) {
return &p, nil
}
}
return nil, fmt.Errorf("player %q not found", name)
}
// SearchPlayers searches the given league for a players with the provided player
// name. name should contain at least 3 letters.
func (yf *YFantasy) SearchPlayers(leagueKey, name string) ([]*schema.Player, error) {
if len(name) < 3 {
return nil, fmt.Errorf("name (%q) must contain at least 3 letters", name)
}
fc, err := query.League().Key(leagueKey).Players().Search(name).Get(yf.client)
if err != nil {
return nil, err
}
var players []*schema.Player
for _, p := range fc.League.Players.Player {
players = append(players, &p)
}
return players, nil
}
// PlayerStats searches the given league for a player with the provided player name.
// and returns their average stats for the current season. If the player is not
// found, an error is returned. name should contain at least 3 letters.
func (yf *YFantasy) PlayerStats(leagueKey, name string, statsType int) (*schema.Player, error) {
if len(name) < 3 {
return nil, fmt.Errorf("name (%q) must contain at least 3 letters", name)
}
q := query.League().Key(leagueKey).Players().Search(name).Stats()
switch statsType {
case StatsTypeUnknown:
return nil, fmt.Errorf("unknown stats type requested")
case StatsTypeSeason:
q = q.CurrentSeason()
break
case StatsTypeAverageSeason:
q = q.CurrentSeasonAverage()
break
case StatsTypeDate:
q = q.Today()
break
case StatsTypeLastWeek:
q = q.LastWeek()
break
case StatsTypeLastWeekAverage:
q = q.LastWeekAverage()
break
case StatsTypeLastMonth:
q = q.LastMonth()
break
case StatsTypeLastMonthAverage:
q = q.LastMonthAverage()
break
}
fc, err := q.Get(yf.client)
if err != nil {
return nil, err
}
for _, p := range fc.League.Players.Player {
if strings.ToLower(p.Name.Full) == strings.ToLower(name) {
return &p, nil
}
}
return nil, fmt.Errorf("player %q not found", name)
}
// PlayerAdvancedStats searches the given league for a player with the provided
// player name and returns their advanced stats. If the player is not found, an
// error is returned. name should contain at least 3 letters.
func (yf *YFantasy) PlayerAdvancedStats(leagueKey, name string) (*schema.Player, error) {
if len(name) < 3 {
return nil, fmt.Errorf("name (%q) must contain at least 3 letters", name)
}
fc, err := query.League().Key(leagueKey).Players().Search(name).Stats().Get(yf.client)
if err != nil {
return nil, err
}
for _, p := range fc.League.Players.Player {
if strings.ToLower(p.Name.Full) == strings.ToLower(name) {
return &p, nil
}
}
return nil, fmt.Errorf("player %q not found", name)
}
// PlayerOwnership searches the league for a player with the provided named and
// returns their ownership status.
func (yf *YFantasy) PlayerOwnership(leagueKey, name string) (*schema.Player, error) {
fc, err := query.League().Key(leagueKey).Players().Search(name).Ownership().Get(yf.client)
if err != nil {
return nil, err
}
for _, p := range fc.League.Players.Player {
if strings.ToLower(p.Name.Full) == strings.ToLower(name) {
return &p, nil
}
}
return nil, fmt.Errorf("player %q not found", name)
}