-
Notifications
You must be signed in to change notification settings - Fork 0
/
players.go
158 lines (130 loc) · 5.45 KB
/
players.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
package sleeper
import (
"encoding/json"
"fmt"
"os"
)
type TrendingPlayer struct {
Count int `json:"count"`
PlayerID string `json:"player_id"`
}
type Players map[string]Player
type Player struct {
Active bool `json:"active"`
Age int `json:"age"`
BirthCity interface{} `json:"birth_city"`
BirthCountry string `json:"birth_country"`
BirthDate string `json:"birth_date"`
BirthState interface{} `json:"birth_state"`
College string `json:"college"`
DepthChartOrder int `json:"depth_chart_order"`
DepthChartPosition string `json:"depth_chart_position"`
EspnID int `json:"espn_id"`
FantasyDataID int `json:"fantasy_data_id"`
FantasyPositions []string `json:"fantasy_positions"`
FirstName string `json:"first_name"`
FullName string `json:"full_name"`
GsisID string `json:"gsis_id"`
Hashtag string `json:"hashtag"`
Height string `json:"height"`
HighSchool string `json:"high_school"`
InjuryBodyPart string `json:"injury_body_part"`
InjuryNotes string `json:"injury_notes"`
InjuryStartDate interface{} `json:"injury_start_date"`
InjuryStatus string `json:"injury_status"`
LastName string `json:"last_name"`
Metadata interface{} `json:"metadata"`
NewsUpdated int64 `json:"news_updated"`
Number int `json:"number"`
PandascoreID interface{} `json:"pandascore_id"`
PlayerID string `json:"player_id"`
Position string `json:"position"`
PracticeDescription string `json:"practice_description"`
PracticeParticipation string `json:"practice_participation"`
RotowireID int `json:"rotowire_id"`
RotoworldID int `json:"rotoworld_id"`
SearchFirstName string `json:"search_first_name"`
SearchFullName string `json:"search_full_name"`
SearchLastName string `json:"search_last_name"`
SearchRank int `json:"search_rank"`
Sport string `json:"sport"`
SportradarID string `json:"sportradar_id"`
StatsID int `json:"stats_id"`
Status string `json:"status"`
SwishID int `json:"swish_id"`
Team string `json:"team"`
Weight string `json:"weight"`
YahooID int `json:"yahoo_id"`
YearsExp int `json:"years_exp"`
}
// Get all players.
// (GET `https://api.sleeper.app/v1/players/<sport>`)
//
// You should save this information on your own servers as this is not intended to be called every time you need to look up players due to the filesize being close to 5MB in size.
// You do not need to call this endpoint more than once per day.
func (c *Client) GetAllPlayers(sport string) (Players, error) {
players := Players{}
url := fmt.Sprintf("%s/players/%s", c.sleeperURL, sport)
data, err := c.getRequest(url)
if err != nil {
return players, err
}
err = json.Unmarshal(data, &players)
return players, err
}
// Get all players and save to a file.
// (GET `https://api.sleeper.app/v1/players/<sport>`)
//
// You should save this information on your own servers as this is not intended to be called every time you need to look up players due to the filesize being close to 5MB in size.
// You do not need to call this endpoint more than once per day.
func (c *Client) SaveAllPlayers(sport string, file string) (bool, error) {
url := fmt.Sprintf("%s/players/%s", c.sleeperURL, sport)
data, err := c.getRequest(url)
if err != nil {
return false, err
}
f, err := os.Create(file)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
return false, err
}
return true, nil
}
// Get all players from a file.
func GetAllPlayers(file string) (Players, error) {
players := Players{}
data, err := os.ReadFile(file)
if err != nil {
return players, err
}
err = json.Unmarshal(data, &players)
return players, err
}
// Get a list of trending players based on adds or drops in the past 24 hours. Trending type is add or drop.
// (GET `https://api.sleeper.app/v1/players/<sport>/trending/<type>`)
func (c *Client) GetTrendingPlayers(sport string, trending_type string) ([]TrendingPlayer, error) {
trendingPlayer := []TrendingPlayer{}
url := fmt.Sprintf("%s/players/%s/trending/%s", c.sleeperURL, sport, trending_type)
data, err := c.getRequest(url)
if err != nil {
return trendingPlayer, err
}
err = json.Unmarshal(data, &trendingPlayer)
return trendingPlayer, err
}
// Get a list of trending players based on adds or drops in the past X hours. Trending type is add or drop.
// (GET `https://api.sleeper.app/v1/players/<sport>/trending/<type>?lookback_hours=<hours>&limit=<int>`)
func (c *Client) GetTrendingPlayersParams(sport string, trending_type string, hours int, limit int) ([]TrendingPlayer, error) {
trendingPlayer := []TrendingPlayer{}
url := fmt.Sprintf("%s/players/%s/trending/%s?loopback_hours=%d&limit=%d", c.sleeperURL, sport, trending_type, hours, limit)
data, err := c.getRequest(url)
if err != nil {
return trendingPlayer, err
}
err = json.Unmarshal(data, &trendingPlayer)
return trendingPlayer, err
}