diff --git a/internal/client.go b/internal/client.go index b618fb2..a189c7d 100644 --- a/internal/client.go +++ b/internal/client.go @@ -102,8 +102,18 @@ func (c *Client) GetPopularMatches(ctx context.Context) ([]Match, error) { } for i := range matches { - if viewers, ok := viewCounts[matches[i].ID]; ok { + // Prefer a direct match on the match ID. + if viewers, ok := viewCounts.ByMatchID[matches[i].ID]; ok { matches[i].Viewers = viewers + continue + } + + // Fallback: some IDs can differ between endpoints, so also try source IDs. + for _, src := range matches[i].Sources { + if viewers, ok := viewCounts.BySourceID[src.ID]; ok { + matches[i].Viewers = viewers + break + } } } @@ -115,24 +125,39 @@ func (c *Client) GetMatchesBySport(ctx context.Context, sportID string) ([]Match return c.getMatches(ctx, url) } -func (c *Client) GetPopularViewCounts(ctx context.Context) (map[string]int, error) { +type PopularViewCounts struct { + ByMatchID map[string]int + BySourceID map[string]int +} + +func (c *Client) GetPopularViewCounts(ctx context.Context) (PopularViewCounts, error) { url := "https://streami.su/api/matches/live/popular-viewcount" var payload []struct { ID string `json:"id"` Viewers int `json:"viewers"` + Sources []struct { + ID string `json:"id"` + } `json:"sources"` } if err := c.get(ctx, url, &payload); err != nil { - return nil, err + return PopularViewCounts{}, err } - out := make(map[string]int, len(payload)) + matchMap := make(map[string]int, len(payload)) + sourceMap := make(map[string]int, len(payload)) for _, item := range payload { - out[item.ID] = item.Viewers + matchMap[item.ID] = item.Viewers + for _, src := range item.Sources { + if src.ID == "" { + continue + } + sourceMap[src.ID] = item.Viewers + } } - return out, nil + return PopularViewCounts{ByMatchID: matchMap, BySourceID: sourceMap}, nil } func (c *Client) GetStreamsForMatch(ctx context.Context, mt Match) ([]Stream, error) {