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"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"scm.dairydemon.net/filifa/mlblive/cmd/internal/statsapi"
|
|
|
|
)
|
|
|
|
|
2024-07-15 00:41:40 +00:00
|
|
|
var team teamFlag
|
2024-07-15 00:12:55 +00:00
|
|
|
var teamIDs = map[string]statsapi.TeamID{
|
|
|
|
"laa": statsapi.LAA,
|
|
|
|
"az": statsapi.AZ,
|
|
|
|
"bal": statsapi.BAL,
|
|
|
|
"bos": statsapi.BOS,
|
|
|
|
"chc": statsapi.CHC,
|
|
|
|
"cin": statsapi.CIN,
|
|
|
|
"cle": statsapi.CLE,
|
|
|
|
"col": statsapi.COL,
|
|
|
|
"det": statsapi.DET,
|
|
|
|
"hou": statsapi.HOU,
|
|
|
|
"kc": statsapi.KC,
|
|
|
|
"lad": statsapi.LAD,
|
|
|
|
"wsh": statsapi.WSH,
|
|
|
|
"nym": statsapi.NYM,
|
|
|
|
"oak": statsapi.OAK,
|
|
|
|
"pit": statsapi.PIT,
|
|
|
|
"sd": statsapi.SD,
|
|
|
|
"sea": statsapi.SEA,
|
|
|
|
"sf": statsapi.SF,
|
|
|
|
"stl": statsapi.STL,
|
|
|
|
"tb": statsapi.TB,
|
|
|
|
"tex": statsapi.TEX,
|
|
|
|
"tor": statsapi.TOR,
|
|
|
|
"min": statsapi.MIN,
|
|
|
|
"phi": statsapi.PHI,
|
|
|
|
"atl": statsapi.ATL,
|
|
|
|
"cws": statsapi.CWS,
|
|
|
|
"mia": statsapi.MIA,
|
|
|
|
"nyy": statsapi.NYY,
|
|
|
|
"mil": statsapi.MIL,
|
|
|
|
}
|
2024-07-14 21:43:00 +00:00
|
|
|
|
|
|
|
func getGamePk() (string, error) {
|
2024-07-15 00:41:40 +00:00
|
|
|
id := teamIDs[string(team)]
|
2024-07-15 00:12:55 +00:00
|
|
|
sched, err := statsapi.RequestSchedule("1", strconv.Itoa(int(id)))
|
2024-07-14 21:43:00 +00:00
|
|
|
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 {
|
2024-07-14 22:14:10 +00:00
|
|
|
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{
|
2024-07-15 00:29:38 +00:00
|
|
|
Use: "mlblive -t [team]",
|
2024-07-14 21:43:00 +00:00
|
|
|
Short: "Output data from Major League Baseball's Stats API",
|
2024-07-15 00:41:40 +00:00
|
|
|
Long: `Output data from Major League Baseball's Stats API.
|
2024-07-15 00:37:35 +00:00
|
|
|
|
|
|
|
mlblive will establish a websocket connection with ws.statsapi.mlb.com and
|
|
|
|
output JSON with live updates of a team's game.`,
|
2024-07-15 00:41:40 +00:00
|
|
|
Run: mlblive,
|
2024-07-14 21:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-15 00:41:40 +00:00
|
|
|
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)")
|
2024-07-14 21:43:00 +00:00
|
|
|
rootCmd.MarkFlagRequired("team")
|
|
|
|
}
|