-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_providers.go
More file actions
230 lines (205 loc) · 5.41 KB
/
import_providers.go
File metadata and controls
230 lines (205 loc) · 5.41 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type ImportOptions struct {
LeagueID string
Season int
SWID string
ESPN_S2 string
}
type ImportedPlayer struct {
Name string `json:"name"`
Position string `json:"position"`
}
type ImportedTeam struct {
TeamID int `json:"team_id"`
Name string `json:"name"`
Owner string `json:"owner"`
Roster []ImportedPlayer `json:"roster"`
}
type ImportedLeague struct {
Provider string `json:"provider"`
LeagueID string `json:"league_id"`
Season int `json:"season"`
LeagueName string `json:"league_name"`
Teams []ImportedTeam `json:"teams"`
}
type LeagueImporter interface {
ImportLeague(ctx context.Context, opts ImportOptions) (*ImportedLeague, error)
}
func getImporter(provider string) (LeagueImporter, error) {
switch strings.ToLower(strings.TrimSpace(provider)) {
case "espn":
return &espnImporter{client: httpClient}, nil
case "yahoo":
return &yahooImporter{}, nil
default:
return nil, fmt.Errorf("unsupported provider: %s", provider)
}
}
type espnImporter struct {
client *http.Client
}
func (e *espnImporter) ImportLeague(ctx context.Context, opts ImportOptions) (*ImportedLeague, error) {
season := opts.Season
if season == 0 {
season = timeNowYear()
}
if strings.TrimSpace(opts.LeagueID) == "" {
return nil, fmt.Errorf("league_id is required")
}
baseURL := fmt.Sprintf(
"https://lm-api-reads.fantasy.espn.com/apis/v3/games/ffl/seasons/%d/segments/0/leagues/%s",
season,
url.PathEscape(opts.LeagueID),
)
params := url.Values{}
params.Add("view", "mTeam")
params.Add("view", "mRoster")
params.Add("view", "mSettings")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"?"+params.Encode(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
if strings.TrimSpace(opts.SWID) != "" && strings.TrimSpace(opts.ESPN_S2) != "" {
req.Header.Set("Cookie", fmt.Sprintf("SWID=%s; espn_s2=%s", strings.TrimSpace(opts.SWID), strings.TrimSpace(opts.ESPN_S2)))
}
resp, err := e.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("espn import failed: status %d", resp.StatusCode)
}
var payload espnLeaguePayload
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
return nil, err
}
return parseESPNLeaguePayload(payload), nil
}
type yahooImporter struct{}
func (y *yahooImporter) ImportLeague(ctx context.Context, opts ImportOptions) (*ImportedLeague, error) {
return nil, fmt.Errorf("yahoo read-only import scaffold is in place but live import requires oauth/session setup")
}
type espnLeaguePayload struct {
ID int `json:"id"`
SeasonID int `json:"seasonId"`
Settings struct {
Name string `json:"name"`
} `json:"settings"`
Members []struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
} `json:"members"`
Teams []struct {
ID int `json:"id"`
Location string `json:"location"`
Nickname string `json:"nickname"`
Abbrev string `json:"abbrev"`
Owners []string `json:"owners"`
Roster struct {
Entries []struct {
PlayerPoolEntry struct {
Player struct {
FullName string `json:"fullName"`
DefaultPositionID int `json:"defaultPositionId"`
} `json:"player"`
} `json:"playerPoolEntry"`
} `json:"entries"`
} `json:"roster"`
} `json:"teams"`
}
func parseESPNLeaguePayload(p espnLeaguePayload) *ImportedLeague {
ownerNameByID := map[string]string{}
for _, m := range p.Members {
display := strings.TrimSpace(m.DisplayName)
if display == "" {
display = strings.TrimSpace(strings.TrimSpace(m.FirstName + " " + m.LastName))
}
if display == "" {
display = "Unknown"
}
ownerNameByID[m.ID] = display
}
out := &ImportedLeague{
Provider: "espn",
LeagueID: strconv.Itoa(p.ID),
Season: p.SeasonID,
LeagueName: strings.TrimSpace(p.Settings.Name),
Teams: []ImportedTeam{},
}
for _, t := range p.Teams {
teamName := strings.TrimSpace(strings.TrimSpace(t.Location + " " + t.Nickname))
if teamName == "" {
teamName = strings.TrimSpace(t.Abbrev)
}
if teamName == "" {
teamName = fmt.Sprintf("Team %d", t.ID)
}
owner := "Unknown"
if len(t.Owners) > 0 {
if name, ok := ownerNameByID[t.Owners[0]]; ok {
owner = name
}
}
players := make([]ImportedPlayer, 0, len(t.Roster.Entries))
for _, entry := range t.Roster.Entries {
player := entry.PlayerPoolEntry.Player
name := strings.TrimSpace(player.FullName)
if name == "" {
continue
}
players = append(players, ImportedPlayer{
Name: name,
Position: espnPosition(player.DefaultPositionID),
})
}
out.Teams = append(out.Teams, ImportedTeam{
TeamID: t.ID,
Name: teamName,
Owner: owner,
Roster: players,
})
}
if out.Season == 0 {
out.Season = timeNowYear()
}
if out.LeagueID == "0" {
out.LeagueID = ""
}
return out
}
func espnPosition(id int) string {
switch id {
case 1:
return "QB"
case 2:
return "RB"
case 3:
return "WR"
case 4:
return "TE"
case 5:
return "K"
case 16:
return "DST"
default:
return "UNK"
}
}
// Wrapped for unit tests.
var timeNowYear = func() int {
return time.Now().Year()
}