-
Notifications
You must be signed in to change notification settings - Fork 0
/
nhl.schedule.go
164 lines (148 loc) · 3.93 KB
/
nhl.schedule.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/nleeper/goment"
)
type Team struct {
Conference string
Division string
Team string
W int
L int
Wl int
W10 int
L10 int
WL10 int
GP int
GD int
WCFlag int
}
type gameschedule struct {
Copyright string `json:"copyright"`
TotalItems int `json:"totalItems"`
TotalEvents int `json:"totalEvents"`
TotalGames int `json:"totalGames"`
TotalMatches int `json:"totalMatches"`
Wait int `json:"wait"`
Dates []struct {
Date string `json:"date"`
TotalItems int `json:"totalItems"`
TotalEvents int `json:"totalEvents"`
TotalGames int `json:"totalGames"`
TotalMatches int `json:"totalMatches"`
Games []struct {
GamePk int `json:"gamePk"`
Link string `json:"link"`
GameType string `json:"gameType"`
Season string `json:"season"`
GameDate time.Time `json:"gameDate"`
Status struct {
AbstractGameState string `json:"abstractGameState"`
CodedGameState string `json:"codedGameState"`
DetailedState string `json:"detailedState"`
StatusCode string `json:"statusCode"`
StartTimeTBD bool `json:"startTimeTBD"`
} `json:"status"`
Teams struct {
Away struct {
LeagueRecord struct {
Wins int `json:"wins"`
Losses int `json:"losses"`
Ot int `json:"ot"`
Type string `json:"type"`
} `json:"leagueRecord"`
Score int `json:"score"`
Team struct {
ID int `json:"id"`
Name string `json:"name"`
Link string `json:"link"`
} `json:"team"`
} `json:"away"`
Home struct {
LeagueRecord struct {
Wins int `json:"wins"`
Losses int `json:"losses"`
Ot int `json:"ot"`
Type string `json:"type"`
} `json:"leagueRecord"`
Score int `json:"score"`
Team struct {
ID int `json:"id"`
Name string `json:"name"`
Link string `json:"link"`
} `json:"team"`
} `json:"home"`
} `json:"teams"`
Content struct {
Link string `json:"link"`
} `json:"content"`
Venue struct {
ID int `json:"id"`
Name string `json:"name"`
Link string `json:"link"`
} `json:"venue,omitempty"`
} `json:"games"`
Events []interface{} `json:"events"`
Matches []interface{} `json:"matches"`
} `json:"dates"`
}
func schedule(teamIds []int) {
url := urls["schedule"]
// reckon the date range
// for now
today, _ := goment.New()
startDate := today.Subtract(2, "days").Format("YYYY-MM-DD")
endDate := today.Add(14, "days").Format("YYYY-MM-DD")
url += "?startDate=" + startDate + "&endDate=" + endDate
if len(teamIds) > 0 {
var IDs []string
for _, i := range teamIds {
IDs = append(IDs, strconv.Itoa(i))
}
url += "&teamId=" + strings.Join(IDs, ",")
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Print(err)
}
req.Header.Set("User-Agent", "nhl-stats-api")
res, getErr := client.Do(req)
if getErr != nil {
fmt.Print(getErr)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Print(err)
}
var sched gameschedule
err = json.Unmarshal(data, &sched)
if err != nil {
fmt.Println("error:", err)
}
for _, r := range sched.Dates {
dt, _ := goment.New(r.Date, "YYYY-MM-DD")
fmt.Println(dt.Format("dddd MMMM D YYYY"))
prefix := " "
for _, g := range r.Games {
t := g.Teams
a := t.Away.Team
h := t.Home.Team
switch g.Status.DetailedState {
case "Final":
fmt.Println(prefix, lastWord(a.Name), t.Away.Score, lastWord(h.Name), t.Home.Score)
case "In Progress":
fmt.Println(prefix, lastWord(a.Name), t.Away.Score, lastWord(h.Name), t.Home.Score)
default:
fmt.Println(prefix, lastWord(t.Away.Team.Name), "at", lastWord(t.Home.Team.Name))
}
}
fmt.Println()
}
fmt.Println()
}