use cobra

This commit is contained in:
filifa
2024-07-14 16:43:00 -05:00
parent d064d7bd82
commit 8aff575b4b
11 changed files with 856 additions and 107 deletions

View File

@@ -0,0 +1,111 @@
package statsapi
import (
"encoding/json"
"io"
"net/http"
"net/url"
"github.com/evanphx/json-patch/v5"
)
var DefaultClient = NewClient(http.DefaultClient)
func RequestSchedule(sportId, teamId string) ([]byte, error) {
return DefaultClient.RequestSchedule(sportId, teamId)
}
func RequestFeed(gamePk string) ([]byte, error) {
return DefaultClient.RequestFeed(gamePk)
}
func RequestDiffPatch(gamePk, startTimecode, pushUpdateId string) (DiffPatchResponse, error) {
return DefaultClient.RequestDiffPatch(gamePk, startTimecode, pushUpdateId)
}
type DiffPatchResponse []byte
type Client struct {
baseURL url.URL
httpClient *http.Client
}
func NewClient(c *http.Client) *Client {
return &Client{
baseURL: url.URL{
Scheme: "https",
Host: "statsapi.mlb.com",
},
httpClient: c,
}
}
func (resp *DiffPatchResponse) ExtractPatches() ([]jsonpatch.Patch, error) {
var patches []jsonpatch.Patch
var objs []map[string]any
err := json.Unmarshal([]byte(*resp), &objs)
if err != nil {
return patches, err
}
for _, obj := range objs {
diff := obj["diff"]
patch, err := json.Marshal(diff)
if err != nil {
break
}
p, err := jsonpatch.DecodePatch(patch)
if err != nil {
break
}
patches = append(patches, p)
}
return patches, err
}
func (c *Client) RequestSchedule(sportId, teamId string) ([]byte, error) {
endpoint := url.URL{Path: "api/v1/schedule"}
query := endpoint.Query()
query.Add("sportId", sportId)
query.Add("teamId", teamId)
endpoint.RawQuery = query.Encode()
return c.get(&endpoint)
}
func (c *Client) RequestFeed(gamePk string) ([]byte, error) {
endpoint := url.URL{Path: "api/v1.1/game/" + gamePk + "/feed/live"}
return c.get(&endpoint)
}
func (c *Client) RequestDiffPatch(gamePk, startTimecode, pushUpdateId string) (DiffPatchResponse, error) {
endpoint := url.URL{Path: "api/v1.1/game/" + gamePk + "/feed/live/diffPatch"}
query := endpoint.Query()
query.Add("language", "en")
query.Add("startTimecode", startTimecode)
query.Add("pushUpdateId", pushUpdateId)
endpoint.RawQuery = query.Encode()
return c.get(&endpoint)
}
func (c *Client) get(endpoint *url.URL) ([]byte, error) {
url := c.baseURL.ResolveReference(endpoint)
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return body, err
}

View File

@@ -0,0 +1,34 @@
package statsapi
var TeamIds = map[string]int{
"laa": 108,
"az": 109,
"bal": 110,
"bos": 111,
"chc": 112,
"cin": 113,
"cle": 114,
"col": 115,
"det": 116,
"hou": 117,
"kc": 118,
"lad": 119,
"wsh": 120,
"nym": 121,
"oak": 133,
"pit": 134,
"sd": 135,
"sea": 136,
"sf": 137,
"stl": 138,
"tb": 139,
"tex": 140,
"tor": 141,
"min": 142,
"phi": 143,
"atl": 144,
"cws": 145,
"mia": 146,
"nyy": 147,
"mil": 158,
}

View File

@@ -0,0 +1,59 @@
package statsapi
import (
"encoding/json"
)
type Feed struct {
MetaData metadata
LiveData livedata
}
type metadata struct {
TimeStamp string
}
type livedata struct {
Plays plays
Linescore linescore
}
type linescore struct {
Teams teams
}
type teams struct {
Home team
Away team
}
type team struct {
Runs json.Number
}
type plays struct {
AllPlays []Play
CurrentPlay Play
}
type Play struct {
Result result
About about
AtBatIndex int
}
type result struct {
Event string
Description string
RBI int
AwayScore int
HomeScore int
}
type about struct {
AtBatIndex json.Number
IsTopInning bool
Inning json.Number
IsScoringPlay bool
CaptivatingIndex json.Number
}

View File

@@ -0,0 +1,31 @@
package statsapi
import (
"encoding/json"
)
type ScheduleParams struct {
SportId string
TeamId string
}
type Schedule struct {
TotalGames json.Number
TotalGamesInProgress json.Number
Dates []date
}
type date struct {
TotalGames json.Number
TotalGamesInProgress json.Number
Games []game
}
type game struct {
GamePk json.Number
Content content
}
type content struct {
Link string
}

View File

@@ -0,0 +1,59 @@
package statsapi
import (
"net/url"
"time"
"github.com/gorilla/websocket"
)
type Push struct {
UpdateId string
}
type GamedayWebsocket struct {
baseURL url.URL
*websocket.Conn
}
func NewGamedayWebsocket(gamePk string) (GamedayWebsocket, error) {
ws := GamedayWebsocket{
baseURL: url.URL{
Scheme: "wss",
Host: "ws.statsapi.mlb.com",
},
}
err := ws.init(gamePk)
return ws, err
}
func (g *GamedayWebsocket) init(gamePk string) error {
endpoint := url.URL{
Path: "api/v1/game/push/subscribe/gameday/" + gamePk,
}
url := g.baseURL.ResolveReference(&endpoint)
conn, _, err := websocket.DefaultDialer.Dial(url.String(), nil)
g.Conn = conn
return err
}
func (g *GamedayWebsocket) SendKeepAlive() error {
msg := []byte("Gameday5")
err := g.Conn.WriteMessage(websocket.TextMessage, msg)
return err
}
func (g *GamedayWebsocket) KeepAlive(interval time.Duration, ch chan<- error) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
err := g.SendKeepAlive()
if err != nil {
ch <- err
}
}
}