Improve popular viewer count mapping
This commit is contained in:
@@ -102,8 +102,18 @@ func (c *Client) GetPopularMatches(ctx context.Context) ([]Match, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := range matches {
|
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
|
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)
|
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"
|
url := "https://streami.su/api/matches/live/popular-viewcount"
|
||||||
|
|
||||||
var payload []struct {
|
var payload []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Viewers int `json:"viewers"`
|
Viewers int `json:"viewers"`
|
||||||
|
Sources []struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
} `json:"sources"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.get(ctx, url, &payload); err != nil {
|
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 {
|
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) {
|
func (c *Client) GetStreamsForMatch(ctx context.Context, mt Match) ([]Stream, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user