From 4f1114f297297dab3deee76b0dcc4d30ffcb9bb6 Mon Sep 17 00:00:00 2001 From: filifa Date: Sun, 6 Apr 2025 20:47:42 -0400 Subject: [PATCH] make it build again --- cmd/content.go | 6 ++-- cmd/feed.go | 6 ++-- cmd/schedule.go | 22 ++++++-------- cmd/standings.go | 14 ++++----- cmd/subscribe.go | 74 ++++++++++++++++++++++++++++++------------------ 5 files changed, 68 insertions(+), 54 deletions(-) diff --git a/cmd/content.go b/cmd/content.go index 8b966ae..1f41ddd 100644 --- a/cmd/content.go +++ b/cmd/content.go @@ -28,12 +28,14 @@ import ( func content(cmd *cobra.Command, args []string) { client := api.NewAPIClient(api.NewConfiguration()) - content, _, err := client.GameApi.Content(context.Background(), gamePk, nil) + req := client.GameAPI.Content(context.Background(), gamePk) + + resp, err := req.Execute() if err != nil { log.Fatal(err) } - json, err := json.Marshal(content) + json, err := json.Marshal(resp.Body) if err != nil { log.Fatal(err) } diff --git a/cmd/feed.go b/cmd/feed.go index c0405b2..8312677 100644 --- a/cmd/feed.go +++ b/cmd/feed.go @@ -28,12 +28,14 @@ import ( func feed(cmd *cobra.Command, args []string) { client := api.NewAPIClient(api.NewConfiguration()) - standing, _, err := client.GameApi.LiveGameV1(context.Background(), gamePk, nil) + req := client.GameAPI.LiveGameV1(context.Background(), gamePk) + + resp, err := req.Execute() if err != nil { log.Fatal(err) } - json, err := json.Marshal(standing) + json, err := json.Marshal(resp.Body) if err != nil { log.Fatal(err) } diff --git a/cmd/schedule.go b/cmd/schedule.go index 98750ee..d5ecacd 100644 --- a/cmd/schedule.go +++ b/cmd/schedule.go @@ -22,7 +22,6 @@ import ( "fmt" "log" - "github.com/antihax/optional" "github.com/spf13/cobra" "scm.dairydemon.net/filifa/mlbstats/api" ) @@ -32,26 +31,21 @@ var team teamAbbr var sport = sportAbbr("mlb") func schedule(cmd *cobra.Command, args []string) { - var teamIds []int32 - if team != "" { - teamIds = append(teamIds, int32(teamIDs[string(team)])) - } - sportId := int32(sportIDs[string(sport)]) - opts := api.ScheduleApiScheduleOpts{ - TeamId: optional.NewInterface(teamIds), - SportId: optional.NewInterface([]int32{sportId}), - Date: optional.NewString(date), - } - client := api.NewAPIClient(api.NewConfiguration()) - sched, _, err := client.ScheduleApi.Schedule(context.Background(), false, &opts) + req := client.ScheduleAPI.Schedule(context.Background()) + + req.SportId(sportId) + req.TeamId(teamIDs[string(team)]) + req.Date(date) + + resp, err := req.Execute() if err != nil { log.Fatal(err) } - json, err := json.Marshal(sched) + json, err := json.Marshal(resp.Body) if err != nil { log.Fatal(err) } diff --git a/cmd/standings.go b/cmd/standings.go index 01cea33..76b3ebf 100644 --- a/cmd/standings.go +++ b/cmd/standings.go @@ -22,10 +22,8 @@ import ( "fmt" "log" - "github.com/antihax/optional" "github.com/spf13/cobra" "scm.dairydemon.net/filifa/mlbstats/api" - "scm.dairydemon.net/filifa/mlbstats/api/models" ) var league leagueAbbr @@ -34,17 +32,17 @@ func standings(cmd *cobra.Command, args []string) { var leagueIds []int32 leagueIds = append(leagueIds, int32(leagueIDs[string(league)])) - opts := api.StandingsApiStandings1Opts{ - LeagueId: optional.NewInterface(leagueIds), - } - client := api.NewAPIClient(api.NewConfiguration()) - standing, _, err := client.StandingsApi.Standings1(context.Background(), string(models.REGULAR_SEASON_StandingsType), &opts) + req := client.StandingsAPI.Standings1(context.Background(), "regularSeason") + + req.LeagueId(leagueIDs[string(league)]) + + resp, err := req.Execute() if err != nil { log.Fatal(err) } - json, err := json.Marshal(standing) + json, err := json.Marshal(resp.Body) if err != nil { log.Fatal(err) } diff --git a/cmd/subscribe.go b/cmd/subscribe.go index 2c9ef7c..07861ad 100644 --- a/cmd/subscribe.go +++ b/cmd/subscribe.go @@ -20,24 +20,23 @@ import ( "context" "encoding/json" "fmt" + "io" "log" "time" - "github.com/antihax/optional" "github.com/evanphx/json-patch/v5" "github.com/gorilla/websocket" "github.com/spf13/cobra" "scm.dairydemon.net/filifa/mlbstats/api" - "scm.dairydemon.net/filifa/mlbstats/api/models" ) var gamePk int32 -func extractPatches(resp string) ([]jsonpatch.Patch, error) { +func extractPatches(resp []byte) ([]jsonpatch.Patch, error) { var patches []jsonpatch.Patch var objs []map[string]jsonpatch.Patch - err := json.Unmarshal([]byte(resp), &objs) + err := json.Unmarshal(resp, &objs) if err != nil { return patches, err } @@ -50,12 +49,26 @@ func extractPatches(resp string) ([]jsonpatch.Patch, error) { return patches, err } -func patch(feed *models.BaseballGameRestObject, client *api.APIClient) error { - opts := api.GameApiLiveGameDiffPatchV1Opts{ - StartTimecode: optional.NewString(feed.MetaData.TimeStamp), +func patch(body []byte, client *api.APIClient) error { + var v any + err := json.Unmarshal(body, &v) + if err != nil { + return err } - diffPatch, _, err := client.GameApi.LiveGameDiffPatchV1(context.Background(), feed.GamePk, &opts) + vobj := v.(map[string]any) + metaData := vobj["metaData"].(map[string]any) + timestamp := metaData["timeStamp"] + + req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk) + req.StartTimecode(timestamp) + + resp, err := req.Execute() + if err != nil { + return err + } + + diffPatch, err := io.ReadAll(resp.Body) if err != nil { return err } @@ -65,19 +78,13 @@ func patch(feed *models.BaseballGameRestObject, client *api.APIClient) error { return err } - enc, err := json.Marshal(feed) - if err != nil { - return err - } - for _, patch := range patches { - enc, err = patch.Apply(enc) + body, err = patch.Apply(body) if err != nil { return err } } - err = json.Unmarshal(enc, &feed) return err } @@ -93,24 +100,29 @@ func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) { return ws, ch, err } -func handleUnexpectedClose(feed *models.BaseballGameRestObject, client *api.APIClient) (*gamedayWebsocket, error) { - ws, _, err := newWebsocket(feed.GamePk) +func handleUnexpectedClose(body []byte, client *api.APIClient) (*gamedayWebsocket, error) { + ws, _, err := newWebsocket(gamePk) if err != nil { return nil, err } - newFeed, _, err := client.GameApi.LiveGameV1(context.Background(), feed.GamePk, nil) - *feed = newFeed + req := client.GameAPI.LiveGameV1(context.Background(), gamePk) + resp, err := req.Execute() + if err != nil { + return ws, err + } + + body, err = io.ReadAll(resp.Body) return ws, err } -func updateFeed(feed *models.BaseballGameRestObject, ws *gamedayWebsocket, client *api.APIClient) error { +func updateFeed(body []byte, ws *gamedayWebsocket, client *api.APIClient) error { var p push err := ws.ReadJSON(&p) if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) { log.Println(err) - newWs, err := handleUnexpectedClose(feed, client) + newWs, err := handleUnexpectedClose(body, client) if err != nil { return err } @@ -121,14 +133,18 @@ func updateFeed(feed *models.BaseballGameRestObject, ws *gamedayWebsocket, clien return err } - err = patch(feed, client) + err = patch(body, client) if err != nil { - newFeed, _, err := client.GameApi.LiveGameV1(context.Background(), feed.GamePk, nil) + req := client.GameAPI.LiveGameV1(context.Background(), gamePk) + resp, err := req.Execute() if err != nil { return err } - *feed = newFeed + body, err = io.ReadAll(resp.Body) + if err != nil { + return err + } } return err @@ -142,20 +158,22 @@ func subscribe(cmd *cobra.Command, args []string) { defer ws.Close() client := api.NewAPIClient(api.NewConfiguration()) - feed, _, err := client.GameApi.LiveGameV1(context.Background(), gamePk, nil) + req := client.GameAPI.LiveGameV1(context.Background(), gamePk) + + resp, err := req.Execute() if err != nil { log.Fatal(err) } for { - json, err := json.Marshal(feed) + body, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } - fmt.Println(string(json)) + fmt.Println(string(body)) - err = updateFeed(&feed, ws, client) + err = updateFeed(body, ws, client) if err != nil { log.Fatal(err) }