mlbstats/cmd/subscribe.go

208 lines
4.3 KiB
Go
Raw Normal View History

2024-07-20 02:59:25 +00:00
/*
Copyright © 2024 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 (
2025-04-06 04:45:24 +00:00
"context"
2024-07-20 02:59:25 +00:00
"encoding/json"
"fmt"
2025-04-07 00:47:42 +00:00
"io"
2024-07-20 02:59:25 +00:00
"log"
"time"
2025-04-06 04:45:24 +00:00
"github.com/evanphx/json-patch/v5"
2024-07-20 18:11:10 +00:00
"github.com/gorilla/websocket"
2024-07-20 02:59:25 +00:00
"github.com/spf13/cobra"
2025-04-06 04:45:24 +00:00
"scm.dairydemon.net/filifa/mlbstats/api"
2024-07-20 02:59:25 +00:00
)
2025-04-06 04:45:24 +00:00
var gamePk int32
2024-07-20 02:59:25 +00:00
2025-04-07 00:47:42 +00:00
func extractPatches(resp []byte) ([]jsonpatch.Patch, error) {
2025-04-06 04:45:24 +00:00
var patches []jsonpatch.Patch
var objs []map[string]jsonpatch.Patch
2025-04-07 00:47:42 +00:00
err := json.Unmarshal(resp, &objs)
2025-04-06 04:45:24 +00:00
if err != nil {
return patches, err
}
2025-04-06 04:45:24 +00:00
for _, obj := range objs {
patch := obj["diff"]
patches = append(patches, patch)
}
return patches, err
}
2025-04-07 00:47:42 +00:00
func patch(body []byte, client *api.APIClient) error {
var v any
err := json.Unmarshal(body, &v)
if err != nil {
return err
2025-04-06 04:45:24 +00:00
}
2025-04-07 00:47:42 +00:00
vobj := v.(map[string]any)
metaData := vobj["metaData"].(map[string]any)
timestamp := metaData["timeStamp"]
req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk)
req.StartTimecode(timestamp)
resp, err := req.Execute()
2024-07-20 02:59:25 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
return err
2024-07-20 02:59:25 +00:00
}
2025-04-07 00:47:42 +00:00
diffPatch, err := io.ReadAll(resp.Body)
2024-07-20 02:59:25 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
return err
}
2025-04-07 00:47:42 +00:00
patches, err := extractPatches(diffPatch)
2025-04-06 04:45:24 +00:00
if err != nil {
return err
2024-07-20 02:59:25 +00:00
}
for _, patch := range patches {
2025-04-07 00:47:42 +00:00
body, err = patch.Apply(body)
2024-07-20 02:59:25 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
return err
2024-07-20 02:59:25 +00:00
}
}
2025-04-06 04:45:24 +00:00
return err
2024-07-20 02:59:25 +00:00
}
2025-04-06 05:15:34 +00:00
func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) {
ws, err := newGamedayWebsocket(gamePk)
2024-07-20 18:11:10 +00:00
if err != nil {
return nil, nil, err
}
ch := make(chan error)
2025-04-06 05:15:34 +00:00
go ws.keepAlive(10*time.Second, ch)
2024-07-20 18:11:10 +00:00
2024-07-20 22:17:06 +00:00
return ws, ch, err
2024-07-20 18:11:10 +00:00
}
2025-04-07 00:47:42 +00:00
func handleUnexpectedClose(body []byte, client *api.APIClient) (*gamedayWebsocket, error) {
ws, _, err := newWebsocket(gamePk)
2024-07-20 20:08:53 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
return nil, err
2024-07-20 20:08:53 +00:00
}
2025-04-07 00:47:42 +00:00
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
if err != nil {
return ws, err
}
body, err = io.ReadAll(resp.Body)
2025-04-06 04:45:24 +00:00
return ws, err
2024-07-20 20:08:53 +00:00
}
2025-04-07 00:47:42 +00:00
func updateFeed(body []byte, ws *gamedayWebsocket, client *api.APIClient) error {
2025-04-06 05:15:34 +00:00
var p push
2025-04-06 04:45:24 +00:00
err := ws.ReadJSON(&p)
2025-04-06 05:15:34 +00:00
if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) {
2024-07-20 22:07:58 +00:00
log.Println(err)
2025-04-07 00:47:42 +00:00
newWs, err := handleUnexpectedClose(body, client)
2024-07-20 22:07:58 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
return err
2024-07-20 22:07:58 +00:00
}
2025-04-06 04:45:24 +00:00
2024-07-20 22:07:58 +00:00
*ws = *newWs
2025-04-06 04:45:24 +00:00
return err
2024-07-20 22:07:58 +00:00
} else if err != nil {
2025-04-06 04:45:24 +00:00
return err
2024-07-20 22:07:58 +00:00
}
2025-04-07 00:47:42 +00:00
err = patch(body, client)
2024-07-20 22:07:58 +00:00
if err != nil {
2025-04-07 00:47:42 +00:00
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
2025-04-06 04:45:24 +00:00
if err != nil {
return err
}
2025-04-07 00:47:42 +00:00
body, err = io.ReadAll(resp.Body)
if err != nil {
return err
}
2024-07-20 22:07:58 +00:00
}
2025-04-06 04:45:24 +00:00
return err
2024-07-20 22:07:58 +00:00
}
2024-07-20 02:59:25 +00:00
func subscribe(cmd *cobra.Command, args []string) {
2025-04-06 04:45:24 +00:00
ws, _, err := newWebsocket(gamePk)
2024-07-20 02:59:25 +00:00
if err != nil {
log.Fatal(err)
}
defer ws.Close()
2025-04-07 02:13:37 +00:00
req := statsAPIClient.GameAPI.LiveGameV1(context.Background(), gamePk)
2025-04-07 00:47:42 +00:00
resp, err := req.Execute()
2024-07-20 02:59:25 +00:00
if err != nil {
log.Fatal(err)
}
for {
2025-04-07 00:47:42 +00:00
body, err := io.ReadAll(resp.Body)
2025-04-06 04:45:24 +00:00
if err != nil {
log.Fatal(err)
}
2025-04-07 00:47:42 +00:00
fmt.Println(string(body))
2024-07-20 02:59:25 +00:00
2025-04-07 02:13:37 +00:00
err = updateFeed(body, ws, statsAPIClient)
2024-07-20 02:59:25 +00:00
if err != nil {
log.Fatal(err)
}
}
}
// subscribeCmd represents the subscribe command
var subscribeCmd = &cobra.Command{
Use: "subscribe -g [gamePk]",
2024-07-20 03:03:45 +00:00
Short: "Output live game data",
Long: `Output live game data.
2024-07-20 02:59:25 +00:00
2024-07-20 03:03:45 +00:00
Establish a websocket connection with ws.statsapi.mlb.com and output JSON with
live updates of a game.`,
2024-07-20 17:30:13 +00:00
Args: cobra.NoArgs,
Run: subscribe,
2024-07-20 02:59:25 +00:00
}
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.:
2025-04-06 04:45:24 +00:00
subscribeCmd.Flags().Int32VarP(&gamePk, "gamePk", "g", 0, "game PK")
2024-07-20 02:59:25 +00:00
subscribeCmd.MarkFlagRequired("gamePk")
}