-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgames.go
More file actions
76 lines (59 loc) · 1.87 KB
/
games.go
File metadata and controls
76 lines (59 loc) · 1.87 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
package mlb
import (
"errors"
"strconv"
"time"
)
// Get games by specific date. Date is assumed to be relevant local to game timezone.
func (m *Mlb) GetGamesByDate(date time.Time) ([]Game, error) {
return m.GetGamesByDateRange(date, date)
}
// Get games by specific date range. Date is assumed to be relevant local to game timezone.
func (m *Mlb) GetGamesByDateRange(start, end time.Time) ([]Game, error) {
return m.GetGames(start, end, 0)
}
// Get games by date for a specific team. Date is assumed to be relevant local to game timezone.
func (m *Mlb) GetGamesByDateForTeam(date time.Time, teamId int) ([]Game, error) {
return m.GetGames(date, date, teamId)
}
// Get games in date range for specific team. Date is assumed to be relevant local to game timezone.
func (m *Mlb) GetGamesByDateRangeForTeam(start, end time.Time, teamId int) ([]Game, error) {
return m.GetGames(start, end, teamId)
}
func (m *Mlb) GetGames(start, end time.Time, teamId int) ([]Game, error) {
// &season=2018&startDate=2018-08-01&endDate=2018-08-31&teamId=119&eventTypes=primary&scheduleTypes=games
if start.IsZero() {
return nil, errors.New("Invalid start date")
}
if end.IsZero() {
return nil, errors.New("Invalid end date")
}
params := map[string]string{
"season": start.Format("2006"),
"eventTypes": "primary",
"scheduleTypes": "games",
"startDate": start.Format("2006-01-02"),
"endDate": end.Format("2006-01-02"),
}
if params["sportId"] == "" {
params["sportId"] = "1"
}
if teamId > 0 {
params["teamId"] = strconv.Itoa(teamId)
}
resp, err := m.Call("/schedule", params)
if err != nil {
ifError(err)
return []Game{}, err
}
if len(resp.Dates) == 0 {
return []Game{}, err
}
games := make([]Game, len(resp.Dates))
for _, date := range resp.Dates {
for _, game := range date.Games {
games = append(games, game)
}
}
return games, err
}