This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
/
gameday.go
357 lines (316 loc) · 9.48 KB
/
gameday.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package gameday
import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
const (
Angels = "108"
Astros = "117"
Athletics = "133"
BlueJays = "141"
Braves = "144"
Brewers = "158"
Cardinals = "138"
Cubs = "112"
Dbacks = "109"
Dodgers = "119"
Giants = "137"
Indians = "114"
Mariners = "136"
Marlins = "146"
Mets = "121"
Nationals = "120"
Orioles = "110"
Padres = "135"
Phillies = "143"
Pirates = "134"
Rangers = "140"
Rays = "139"
RedSox = "111"
Reds = "113"
Rockies = "115"
Royals = "118"
Tigers = "116"
Twins = "142"
WhiteSox = "145"
Yankees = "147"
)
const (
GamedayHostname = "http://gd2.mlb.com"
GamedayBaseUrl = "http://gd2.mlb.com/components/game/mlb"
)
type BoxLineScore struct {
Inning string `xml:"inning,attr"`
Home string `xml:"home,attr"`
Away string `xml:"away,attr"`
}
type Pitcher struct {
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
InningsPitched int `xml:"`
Hits int `xml:"h,attr"`
Runs int `xml:"r,attr"`
EarnedRuns int `xml:"er,attr"`
Walks int `xml:"bb,attr"`
Strikeouts int `xml:"so,attr"`
Pitches int `xml:"np,attr"`
Position string `xml:"pos,attr"`
Outs int `xml:"out,attr"`
Strikes int `xml:"s,attr"`
Win bool `xml:"win,attr"`
Loss bool `xml:"loss,attr"`
Note string `xml:"note,attr"`
ERA float64 `xml:"era,attr"`
}
type BoxPitching struct {
InningsPitched int `xml:"`
Hits int `xml:"h,attr"`
Runs int `xml:"r,attr"`
EarnedRuns int `xml:"er,attr"`
Walks int `xml:"bb,attr"`
Strikeouts int `xml:"so,attr"`
Homeruns int `xml:"hr,attr"`
Outs int `xml:"outs,attr"`
ERA float64 `xml:"era,attr"`
Pitchers []Pitcher `xml:"pitcher"`
}
type Batter struct {
Id string `xml:"id,attr"`
Name string `xml:"name,attr"`
AtBats int `xml:"ab,attr"`
Runs int `xml:"r,attr"`
Hits int `xml:"h,attr"`
Doutbles int `xml:"d,attr"`
Triples int `xml:"t,attr"`
HomeRuns int `xml:"hr,attr"`
RBIs int `xml:"rbi,attr"`
Walks int `xml:"bb,attr"`
Strikeouts int `xml:"so,attr"`
BattingAverage float64 `xml:"avg,attr"`
Position string `xml:"pos,attr"`
}
type BoxBatting struct {
Flag string `xml:"team_flag,attr"`
AtBats int `xml:"ab,attr"`
Runs int `xml:"r,attr"`
Hits int `xml:"h,attr"`
Doutbles int `xml:"d,attr"`
Triples int `xml:"t,attr"`
HomeRuns int `xml:"hr,attr"`
RBIs int `xml:"rbi,attr"`
Walks int `xml:"bb,attr"`
StrikeOuts int `xml:"so,attr"`
BattingAverage float64 `xml:"avg,attr"`
Batters []Batter `xml:"batter"`
}
type BoxScore struct {
GameId string `xml:"game_id,attr"`
GameKey string `xml:"game_pk,attr"`
HomeId string `xml:"home_id,attr"`
AwayId string `xml:"away_id,attr"`
Lines []BoxLineScore `xml:"linescore>inning_line_score"`
Pitching []BoxPitching `xml:"pitching"`
Batting []BoxBatting `xml:"batting"`
}
type GameLineScore struct {
Inning string `xml:"inning,attr"`
Home string `xml:"home_inning_runs,attr"`
Away string `xml:"away_inning_runs,attr"`
}
type Score struct {
Team string `xml:"team,attr"`
Description string `xml:"description,attr"`
AwayScore int `xml:"away_score,attr"`
HomeScore int `xml:"home_score"`
}
type Summary struct {
Inning string `xml:"inning,attr"`
Plays []Score `xml:"score"`
}
type Game struct {
AwayAPMP string `xml:"away_ampm,attr"`
AwayLoss string `xml:"away_loss,attr"`
AwayTeamCity string `xml:"away_team_city,attr"`
AwayTeamCode string `xml:"away_code,attr"`
AwayTeamId string `xml:"away_team_id,attr"`
AwayTeamName string `xml:"away_team_name,attr"`
AwayTime string `xml:"away_time,attr"`
AwayTimezone string `xml:"away_time_zone,attr"`
AwayWin string `xml:"away_win,attr"`
HomeAMPM string `xml:"home_ampm,attr"`
HomeLoss string `xml:"home_loss,attr"`
HomeTeamCity string `xml:"home_team_city,attr"`
HomeTeamCode string `xml:"home_code,attr"`
HomeTeamId string `xml:"home_team_id,attr"`
HomeTeamName string `xml:"home_team_name,attr"`
HomeTime string `xml:"home_time,attr"`
HomeTimezone string `xml:"home_time_zone,attr"`
HomeWin string `xml:"home_win,attr"`
Id string `xml:"id,attr"`
Key string `xml:"game_pk,attr"`
Lines []GameLineScore `xml:"linescore"`
Timezone string `xml:"time_zone,attr"`
Venue string `xml:"venue,attr"`
Weather Weather `xml:"weather"`
Summaries []Summary `xml:"scoring-summary"`
DataDir string `xml:"game_data_directory,attr"`
}
func (g *Game) loadFile(path string, val interface{}) error {
if g.DataDir == "" {
return fmt.Errorf("The DataDir is empty, so now URL can be created")
}
url := fmt.Sprintf(GamedayHostname + g.DataDir + "/" + path)
resp, err := http.Get(url)
if err != nil {
return err
}
err = Load(resp.Body, val)
if err != nil {
return err
}
return nil
}
func (g *Game) GetGameCenter() (GameCenter, error) {
var gc GameCenter
err := g.loadFile("gamecenter.xml", &gc)
return gc, err
}
func (g *Game) GetBoxScore() (BoxScore, error) {
var box BoxScore
err := g.loadFile("boxscore.xml", &box)
return box, err
}
func (g *Game) GetSummaries() ([]Summary, error) {
var gd Gameday
err := g.loadFile("gameday_Syn.xml", &gd)
return gd.Game.Summaries, err
}
func (g *Game) GetWeather() (Weather, error) {
var game Game
err := g.loadFile("plays.xml", &game)
return game.Weather, err
}
type Weather struct {
Wind string `xml:"wind,attr"`
Temperature string `xml:"temp,attr"`
Conditions string `xml:"condition,attr"`
}
type Schedule struct {
Games []Game `xml:"game"`
Date string `xml:"date,attr"`
DisplayTimeZone string `xml:"display_time_zone,attr"`
}
type Broadcast struct {
Radio string `xml:"radio"`
TV string `xml:"tv"`
}
type Story struct {
Headline string `xml:"headline"`
Blurb string `xml:"blurb"`
Url string `xml:"url"`
}
type GCPitcher struct {
Id string `xml:"player_id"`
FirstName string `xml:"useName"`
LastName string `xml:"lastName"`
RosterName string `xml:"rosterDisplayName"`
Number int `xml:"number"`
Hand string `xml:"throwinghand"`
Wins int `xml:"wins"`
Lossers int `xml:"losses"`
StrikeOuts int `xml:"so"`
ERA float64 `xml:"era"`
Report string `xml:"report"`
}
type GameCenter struct {
Id string `xml:"id,attr"`
Status string `xml:"status,attr"`
StartTime string `xml:"start_time,attr"`
AMPM string `xml:"ampm,attr"`
Timezone string `xml:"time_zone,attr"`
Type string `xml:"type,attr"`
League string `xml:"league,attr"`
VenueShort string `xml:"venueShort"`
VenueLong string `xml:"venueLong"`
HomeBroadcast Broadcast `xml:"broadcast>home"`
HomePitcher GCPitcher `xml:"probables>home"`
HomePreview Story `xml:"previews>home"`
HomeRecap Story `xml:"recaps>home"`
AwayBroadcast Broadcast `xml:"broadcast>away"`
AwayPitcher GCPitcher `xml:"probables>away"`
AwayRecap Story `xml:"recaps>away"`
MLBPreview Story `xml:"preview>mlb"`
MLBWrap Story `xml:"wrap>mlb"`
}
type Gameday struct {
Game Game `xml:"game"`
}
func Load(reader io.Reader, anything interface{}) error {
blob, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
err = xml.Unmarshal(blob, anything)
if err != nil {
return err
}
return nil
}
func TeamSubreddit(id string) string {
subs := map[string]string{
Angels: "/r/AngelsBaseball",
Astros: "/r/Astros",
Athletics: "/r/oaklandathletics",
BlueJays: "/r/TorontoBlueJays",
Braves: "/r/Braves",
Brewers: "/r/Brewers",
Cardinals: "/r/Cardinals",
Cubs: "/r/Cubs",
Dbacks: "/r/azdiamondbacks",
Dodgers: "/r/Dodgers",
Giants: "/r/SFGiants",
Indians: "/r/WahoosTipi",
Mariners: "/r/Mariners",
Marlins: "/r/letsgofish",
Mets: "/r/NewYorkMets",
Nationals: "/r/Nationals",
Orioles: "/r/Orioles",
Padres: "/r/Padres",
Phillies: "/r/Phillies",
Pirates: "/r/Buccos",
Rangers: "/r/TexasRangers",
Rays: "/r/TampaBayRays",
RedSox: "/r/RedSox",
Reds: "/r/Reds",
Rockies: "/r/ColoradoRockies",
Royals: "/r/KCRoyals",
Tigers: "/r/MotorCityKitties",
Twins: "/r/MinnesotaTwins",
WhiteSox: "/r/WhiteSox",
Yankees: "/r/NYYankees",
}
return subs[id]
}
func GetGame(id string, t time.Time) (Game, error) {
y, m, d := t.Date()
url := fmt.Sprintf(GamedayBaseUrl+"/year_%d/month_%02d/day_%02d/epg.xml", y, m, d)
resp, err := http.Get(url)
if err != nil {
return Game{}, err
}
var s Schedule
err = Load(resp.Body, &s)
if err != nil {
return Game{}, err
}
for _, game := range s.Games {
if game.HomeTeamId == id || game.AwayTeamId == id {
return game, nil
}
}
return Game{}, fmt.Errorf("Team %s doesn't have a game on %s", id, t)
}