-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlb.go
More file actions
138 lines (124 loc) · 3.78 KB
/
mlb.go
File metadata and controls
138 lines (124 loc) · 3.78 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
package main
import (
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
type mlbTeam struct {
AwayLost int `json:"away_lost"`
AwayWon int `json:"away_won"`
Conference string `json:"conference"`
ConferenceLost int `json:"conference_lost"`
ConferenceWon int `json:"conference_won"`
Division string `json:"division"`
DivisionLost int `json:"division_lost"`
DivisionWon int `json:"division_won"`
FirstName string `json:"first_name"`
GamesBack float64 `json:"games_back"`
GamesPlayed int `json:"games_played"`
HomeLost int `json:"home_lost"`
HomeWon int `json:"home_won"`
LastFive string `json:"last_five"`
LastName string `json:"last_name"`
LastTen string `json:"last_ten"`
Lost int64 `json:"lost"`
OrdinalRank string `json:"ordinal_rank"`
PointDifferential int `json:"point_differential"`
PointDifferentialPerGame string `json:"point_differential_per_game"`
PointsAgainst int `json:"points_against"`
PointsAllowedPerGame string `json:"points_allowed_per_game"`
PointsFor int `json:"points_for"`
PointsScoredPerGame string `json:"points_scored_per_game"`
Rank int `json:"rank"`
Streak string `json:"streak"`
StreakTotal int `json:"streak_total"`
StreakType string `json:"streak_type"`
TeamID string `json:"team_id"`
WinPercentage string `json:"win_percentage"`
Won int64 `json:"won"`
}
type mlbStandings struct {
StandingsDate string
Standing map[string]mlbTeam
}
type mlbAPIStandings struct {
StandingsDate string `json:"standings_date"`
Standing []mlbTeam `json:"standing"`
}
type mlbClient struct {
token string
userAgent string
currStandings mlbStandings
}
func (m *mlbClient) getMLBStandings() mlbStandings {
return m.currStandings
}
func (m *mlbClient) Init() {
if err := m.refreshCache(); err != nil {
fmt.Printf("error initializing cache (will retry) %v", err)
}
fmt.Println("cache initialized")
t := time.NewTicker(time.Minute * 5)
go func() {
for {
<-t.C
if err := m.refreshCache(); err != nil {
fmt.Printf("error refreshing cache %v", err)
}
fmt.Println("updated cache")
}
}()
}
func (m *mlbClient) refreshCache() error {
out := mlbStandings{
Standing: make(map[string]mlbTeam),
}
rawStandings, err := compressedCall(m.token, m.userAgent)
if err != nil {
return err
}
for _, team := range rawStandings.Standing {
out.Standing[team.TeamID] = team
}
m.currStandings = out
return nil
}
func compressedCall(token, UserAgent string) (mlbAPIStandings, error) {
mlbURL, _ := url.Parse("https://erikberg.com/mlb/standings.json")
req := &http.Request{
Method: http.MethodGet,
URL: mlbURL,
Header: http.Header{
"Accept-Encoding": []string{"gzip"},
"Authorization": []string{fmt.Sprintf("Bearer %s", token)},
"User-Agent": []string{UserAgent},
},
Close: true,
}
out := mlbAPIStandings{}
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("error %v\n", err)
return out, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return out, errors.New("cool your jets for a moment and try again")
}
gz, err := gzip.NewReader(resp.Body)
if err != nil {
fmt.Printf("error %v\n", err)
return out, err
}
decoder := json.NewDecoder(gz)
err = decoder.Decode(&out)
if err != nil {
fmt.Printf("error %v\n", err)
return out, err
}
return out, nil
}