mlbstats/cmd/subscribe.go

208 lines
4.4 KiB
Go

/*
Copyright © 2024,2025 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"time"
"github.com/evanphx/json-patch/v5"
"github.com/gorilla/websocket"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mlbstats/api"
)
var gamePk int32
func extractPatches(resp []byte) ([]jsonpatch.Patch, error) {
var patches []jsonpatch.Patch
var objs []map[string]jsonpatch.Patch
err := json.Unmarshal(resp, &objs)
if err != nil {
return patches, err
}
for _, obj := range objs {
patch := obj["diff"]
patches = append(patches, patch)
}
return patches, nil
}
func patch(body []byte, client *api.APIClient) ([]byte, error) {
var v any
err := json.Unmarshal(body, &v)
if err != nil {
return body, err
}
vobj := v.(map[string]any)
metaData := vobj["metaData"].(map[string]any)
timestamp := metaData["timeStamp"]
req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk)
req = req.StartTimecode(timestamp)
resp, err := req.Execute()
if err != nil {
return body, err
}
diffPatch, err := io.ReadAll(resp.Body)
if err != nil {
return body, err
}
patches, err := extractPatches(diffPatch)
if err != nil {
return body, err
}
for _, patch := range patches {
body, err = patch.Apply(body)
if err != nil {
return body, err
}
}
return body, nil
}
func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) {
ws, err := newGamedayWebsocket(gamePk)
if err != nil {
return nil, nil, err
}
ch := make(chan error)
go ws.keepAlive(10*time.Second, ch)
return ws, ch, nil
}
func handleUnexpectedClose(body []byte, client *api.APIClient) (*gamedayWebsocket, error) {
ws, _, err := newWebsocket(gamePk)
if err != nil {
return nil, err
}
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
if err != nil {
return ws, err
}
body, err = io.ReadAll(resp.Body)
return ws, err
}
func updateFeed(body []byte, ws *gamedayWebsocket, client *api.APIClient) ([]byte, error) {
var p push
err := ws.ReadJSON(&p)
if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) {
log.Println(err)
newWs, err := handleUnexpectedClose(body, client)
if err != nil {
return body, err
}
*ws = *newWs
return body, nil
} else if err != nil {
return body, err
}
body, err = patch(body, client)
if err != nil {
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
if err != nil {
return body, err
}
body, err = io.ReadAll(resp.Body)
if err != nil {
return body, err
}
}
return body, nil
}
func subscribe(cmd *cobra.Command, args []string) {
ws, _, err := newWebsocket(gamePk)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
req := statsAPIClient.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
for {
fmt.Println(string(body))
body, err = updateFeed(body, ws, statsAPIClient)
if err != nil {
log.Fatal(err)
}
}
}
// subscribeCmd represents the subscribe command
var subscribeCmd = &cobra.Command{
Use: "subscribe -g [gamePk]",
Short: "Output live game data",
Long: `Output live game data.
Establish a websocket connection with ws.statsapi.mlb.com and output JSON with
live updates of a game.`,
Args: cobra.NoArgs,
Run: subscribe,
}
func init() {
rootCmd.AddCommand(subscribeCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// subscribeCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
subscribeCmd.Flags().Int32VarP(&gamePk, "gamePk", "g", 0, "game PK")
subscribeCmd.MarkFlagRequired("gamePk")
}