move live data to subscribe subcommand

This commit is contained in:
filifa 2024-07-19 21:59:25 -05:00
parent 422e666d9f
commit 9f528ab4ae
2 changed files with 117 additions and 91 deletions

View File

@ -16,107 +16,19 @@ this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mlblive/cmd/internal/statsapi"
)
func getGamePk() (string, error) {
id := teamIDs[string(team)]
sched, err := statsapi.RequestSchedule("1", strconv.Itoa(int(id)))
if err != nil {
return "", err
}
var s statsapi.Schedule
err = json.Unmarshal(sched, &s)
if err != nil {
return "", err
}
gamePk := s.Dates[0].Games[0].GamePk.String()
return gamePk, nil
}
func updateFeed(feedResp []byte, gamePk, ts, updateId string) ([]byte, error) {
diffPatchResp, err := statsapi.RequestDiffPatch(gamePk, ts, updateId)
if err != nil {
return statsapi.RequestFeed(gamePk)
}
patches, err := diffPatchResp.ExtractPatches()
if err != nil {
return statsapi.RequestFeed(gamePk)
}
for _, patch := range patches {
feedResp, err = patch.Apply(feedResp)
if err != nil {
return statsapi.RequestFeed(gamePk)
}
}
return feedResp, err
}
func mlblive(cmd *cobra.Command, args []string) {
gamePk, err := getGamePk()
if err != nil {
log.Fatal(err)
}
ws, err := statsapi.NewGamedayWebsocket(gamePk)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
ch := make(chan error)
go ws.KeepAlive(10*time.Second, ch)
feedResp, err := statsapi.RequestFeed(gamePk)
if err != nil {
log.Fatal(err)
}
var feed statsapi.Feed
for {
fmt.Println(string(feedResp))
err = json.Unmarshal(feedResp, &feed)
if err != nil {
log.Fatal(err)
}
ts := feed.MetaData.TimeStamp
var p statsapi.Push
err = ws.ReadJSON(&p)
if err != nil {
log.Fatal(err)
}
feedResp, err = updateFeed(feedResp, gamePk, ts, p.UpdateId)
if err != nil {
log.Fatal(err)
}
}
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "mlblive -t [team]",
Use: "mlblive",
Short: "Output data from Major League Baseball's Stats API",
Long: `Output data from Major League Baseball's Stats API.
mlblive will establish a websocket connection with ws.statsapi.mlb.com and
output JSON with live updates of a team's game.`,
Run: mlblive,
}
// Execute adds all child commands to the root command and sets flags appropriately.
@ -135,6 +47,4 @@ func init() {
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().VarP(&team, "team", "t", "team to get updates for (atl, az, bal, bos, chc, cin, cle, col, cws, det, hou, kc, laa, lad, mia, mil, min, nym, nyy, oak, phi, pit, sd, sea, sf, stl, tb, tex, tor, wsh)")
rootCmd.MarkFlagRequired("team")
}

116
cmd/subscribe.go Normal file
View File

@ -0,0 +1,116 @@
/*
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"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mlblive/cmd/internal/statsapi"
)
var gamePk int
func updateFeed(feedResp []byte, gamePk, ts, updateId string) ([]byte, error) {
diffPatchResp, err := statsapi.RequestDiffPatch(gamePk, ts, updateId)
if err != nil {
return statsapi.RequestFeed(gamePk)
}
patches, err := diffPatchResp.ExtractPatches()
if err != nil {
return statsapi.RequestFeed(gamePk)
}
for _, patch := range patches {
feedResp, err = patch.Apply(feedResp)
if err != nil {
return statsapi.RequestFeed(gamePk)
}
}
return feedResp, err
}
func subscribe(cmd *cobra.Command, args []string) {
pkStr := strconv.Itoa(gamePk)
ws, err := statsapi.NewGamedayWebsocket(pkStr)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
ch := make(chan error)
go ws.KeepAlive(10*time.Second, ch)
feedResp, err := statsapi.RequestFeed(pkStr)
if err != nil {
log.Fatal(err)
}
var feed statsapi.Feed
for {
fmt.Println(string(feedResp))
err = json.Unmarshal(feedResp, &feed)
if err != nil {
log.Fatal(err)
}
ts := feed.MetaData.TimeStamp
var p statsapi.Push
err = ws.ReadJSON(&p)
if err != nil {
log.Fatal(err)
}
feedResp, err = updateFeed(feedResp, pkStr, ts, p.UpdateId)
if err != nil {
log.Fatal(err)
}
}
}
// subscribeCmd represents the subscribe command
var subscribeCmd = &cobra.Command{
Use: "subscribe -g [gamePk]",
Short: "Output data from Major League Baseball's Stats API",
Long: `Output data from Major League Baseball's Stats API.
mlblive will establish a websocket connection with ws.statsapi.mlb.com and
output JSON with live updates of a game.`,
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().IntVarP(&gamePk, "gamePk", "g", 0, "game PK")
subscribeCmd.MarkFlagRequired("gamePk")
}