mlbstats/cmd/subscribe.go

192 lines
4.4 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"
"log"
"time"
2025-04-06 04:45:24 +00:00
"github.com/antihax/optional"
"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"
"scm.dairydemon.net/filifa/mlbstats/api/models"
2025-04-06 02:43:26 +00:00
"scm.dairydemon.net/filifa/mlbstats/cmd/internal/statsapi"
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-06 04:45:24 +00:00
func extractPatches(resp string) ([]jsonpatch.Patch, error) {
var patches []jsonpatch.Patch
var objs []map[string]jsonpatch.Patch
err := json.Unmarshal([]byte(resp), &objs)
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-06 04:45:24 +00:00
func patch(feed *models.BaseballGameRestObject, client *api.APIClient) error {
opts := api.GameApiLiveGameDiffPatchV1Opts{
StartTimecode: optional.NewString(feed.MetaData.TimeStamp),
}
diffPatch, _, err := client.GameApi.LiveGameDiffPatchV1(context.Background(), feed.GamePk, &opts)
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
patches, err := extractPatches(diffPatch)
2024-07-20 02:59:25 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
return err
}
enc, err := json.Marshal(feed)
if err != nil {
return err
2024-07-20 02:59:25 +00:00
}
for _, patch := range patches {
2025-04-06 04:45:24 +00:00
enc, err = patch.Apply(enc)
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
err = json.Unmarshal(enc, &feed)
return err
2024-07-20 02:59:25 +00:00
}
2025-04-06 04:45:24 +00:00
func newWebsocket(gamePk int32) (*statsapi.GamedayWebsocket, <-chan error, error) {
2024-07-20 18:11:10 +00:00
ws, err := statsapi.NewGamedayWebsocket(gamePk)
if err != nil {
return nil, nil, err
}
ch := make(chan error)
go ws.KeepAlive(10*time.Second, ch)
2024-07-20 22:17:06 +00:00
return ws, ch, err
2024-07-20 18:11:10 +00:00
}
2025-04-06 04:45:24 +00:00
func handleUnexpectedClose(feed *models.BaseballGameRestObject, client *api.APIClient) (*statsapi.GamedayWebsocket, error) {
ws, _, err := newWebsocket(feed.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-06 04:45:24 +00:00
newFeed, _, err := client.GameApi.LiveGameV1(context.Background(), feed.GamePk, nil)
*feed = newFeed
return ws, err
2024-07-20 20:08:53 +00:00
}
2025-04-06 04:45:24 +00:00
func updateFeed(feed *models.BaseballGameRestObject, ws *statsapi.GamedayWebsocket, client *api.APIClient) error {
2024-07-20 22:07:58 +00:00
var p statsapi.Push
2025-04-06 04:45:24 +00:00
err := ws.ReadJSON(&p)
2024-07-20 22:07:58 +00:00
if websocket.IsUnexpectedCloseError(err, statsapi.GameFinalCode, statsapi.GameUnavailableCode) {
log.Println(err)
2025-04-06 04:45:24 +00:00
newWs, err := handleUnexpectedClose(feed, 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-06 04:45:24 +00:00
err = patch(feed, client)
2024-07-20 22:07:58 +00:00
if err != nil {
2025-04-06 04:45:24 +00:00
newFeed, _, err := client.GameApi.LiveGameV1(context.Background(), feed.GamePk, nil)
if err != nil {
return err
}
*feed = newFeed
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-06 04:45:24 +00:00
client := api.NewAPIClient(api.NewConfiguration())
feed, _, err := client.GameApi.LiveGameV1(context.Background(), gamePk, nil)
2024-07-20 02:59:25 +00:00
if err != nil {
log.Fatal(err)
}
for {
2025-04-06 04:45:24 +00:00
json, err := json.Marshal(feed)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(json))
2024-07-20 02:59:25 +00:00
2025-04-06 04:45:24 +00:00
err = updateFeed(&feed, ws, client)
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")
}