mlblive/cmd/subscribe.go

155 lines
3.6 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 (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
2024-07-20 18:11:10 +00:00
"github.com/gorilla/websocket"
2024-07-20 02:59:25 +00:00
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mlblive/cmd/internal/statsapi"
)
var gamePk int
2024-07-20 22:07:58 +00:00
func patch(feedResp []byte, gamePk, ts, updateId string) ([]byte, error) {
2024-07-20 02:59:25 +00:00
diffPatchResp, err := statsapi.RequestDiffPatch(gamePk, ts, updateId)
if err != nil {
2024-07-20 22:07:58 +00:00
return feedResp, err
2024-07-20 02:59:25 +00:00
}
patches, err := diffPatchResp.ExtractPatches()
if err != nil {
2024-07-20 22:07:58 +00:00
return feedResp, err
2024-07-20 02:59:25 +00:00
}
for _, patch := range patches {
feedResp, err = patch.Apply(feedResp)
if err != nil {
2024-07-20 22:07:58 +00:00
return feedResp, err
2024-07-20 02:59:25 +00:00
}
}
return feedResp, err
}
2024-07-20 18:11:10 +00:00
func newWebsocket(gamePk string) (*statsapi.GamedayWebsocket, <-chan error, error) {
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
}
2024-07-20 20:30:05 +00:00
func handleUnexpectedClose(gamePk string) (*statsapi.GamedayWebsocket, []byte, error) {
ws, _, err := newWebsocket(gamePk)
2024-07-20 20:08:53 +00:00
if err != nil {
2024-07-20 20:30:05 +00:00
return nil, nil, err
2024-07-20 20:08:53 +00:00
}
2024-07-20 20:30:05 +00:00
feedResp, err := statsapi.RequestFeed(gamePk)
return ws, feedResp, err
2024-07-20 20:08:53 +00:00
}
2024-07-20 22:07:58 +00:00
func updateFeed(ws *statsapi.GamedayWebsocket, feedResp []byte, gamePk string) ([]byte, error) {
var feed statsapi.Feed
err := json.Unmarshal(feedResp, &feed)
if err != nil {
return nil, err
}
ts := feed.MetaData.TimeStamp
var p statsapi.Push
err = ws.ReadJSON(&p)
if websocket.IsUnexpectedCloseError(err, statsapi.GameFinalCode, statsapi.GameUnavailableCode) {
log.Println(err)
newWs, feedResp, err := handleUnexpectedClose(gamePk)
if err != nil {
return feedResp, err
}
*ws = *newWs
return feedResp, err
} else if err != nil {
return nil, err
}
feedResp, err = patch(feedResp, gamePk, ts, p.UpdateId)
if err != nil {
feedResp, err = statsapi.RequestFeed(gamePk)
}
return feedResp, err
}
2024-07-20 02:59:25 +00:00
func subscribe(cmd *cobra.Command, args []string) {
pkStr := strconv.Itoa(gamePk)
2024-07-20 18:11:10 +00:00
ws, _, err := newWebsocket(pkStr)
2024-07-20 02:59:25 +00:00
if err != nil {
log.Fatal(err)
}
defer ws.Close()
feedResp, err := statsapi.RequestFeed(pkStr)
if err != nil {
log.Fatal(err)
}
for {
fmt.Println(string(feedResp))
2024-07-20 22:07:58 +00:00
feedResp, err = updateFeed(ws, feedResp, pkStr)
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.:
subscribeCmd.Flags().IntVarP(&gamePk, "gamePk", "g", 0, "game PK")
subscribeCmd.MarkFlagRequired("gamePk")
}