mlblive/cmd/root.go

149 lines
3.3 KiB
Go
Raw Normal View History

2024-07-14 21:43:00 +00:00
/*
Copyright © 2024 filifa
2024-07-14 21:52:15 +00:00
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/>.
2024-07-14 21:43:00 +00:00
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mlblive/cmd/internal/statsapi"
)
2024-07-14 23:05:07 +00:00
var Team TeamFlag
2024-07-14 21:43:00 +00:00
func getGamePk() (string, error) {
if Team == "" {
return "", errors.New("need team ID")
}
2024-07-14 23:05:07 +00:00
id, ok := statsapi.TeamIds[string(Team)]
2024-07-14 21:43:00 +00:00
if !ok {
return "", errors.New("invalid team ID")
}
sched, err := statsapi.RequestSchedule("1", strconv.Itoa(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)
2024-07-14 21:43:00 +00:00
}
}
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",
Short: "Output data from Major League Baseball's Stats API",
Long: `Output data from Major League Baseball's Stats API`,
Run: mlblive,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// Cobra also supports local flags, which will only run
// when this action is called directly.
2024-07-14 23:05:07 +00:00
rootCmd.Flags().VarP(&Team, "team", "t", "team to get updates for")
2024-07-14 21:43:00 +00:00
rootCmd.MarkFlagRequired("team")
}