sorarebuddy/sorare_utils/fixture.go

111 lines
3.4 KiB
Go

package sorare_utils
import (
"context"
"git.lehouerou.net/laurent/sorare"
"git.lehouerou.net/laurent/sorare/football"
"git.lehouerou.net/laurent/sorare/graphql"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
)
func GetGamesFromFixtures(
ctx context.Context,
s *sorare.Sorare,
fixtureSlugs []string,
) ([]football.Game, error) {
var games []football.Game
for _, slug := range fixtureSlugs {
log.Debug().Msgf("getting games for fixture %s...", slug)
g, err := s.Football.So5.FixtureGames(slug).Get(ctx, graphql.EmptyParams{})
if err != nil {
return nil, errors.Wrapf(err, "getting games for fixture %s", slug)
}
log.Debug().Msgf("found %d games", len(g))
games = append(games, g...)
}
log.Debug().Msgf("found total of %d games before filtering", len(games))
games = lo.Filter(games, func(game football.Game, index int) bool {
return game.Id.Value != ""
})
log.Debug().Msgf("found total of %d games after filtering empty ids", len(games))
games = lo.UniqBy(games, func(game football.Game) string {
return game.Id.Value
})
log.Debug().Msgf("found total of %d games after filtering duplicate ids", len(games))
return games, nil
}
func ExtractTeamSlugsFromGames(games []football.Game) []string {
var res []string
res = lo.Map(games, func(game football.Game, index int) string {
return game.AwayTeam.Team.Slug
})
res = append(res, lo.Map(games, func(game football.Game, index int) string {
return game.HomeTeam.Team.Slug
})...)
res = lo.Filter(res, func(slug string, index int) bool {
return slug != ""
})
return lo.Uniq(res)
}
func ExtractCountrySlugsFromCompetitions(competitions []football.Competition) []string {
return lo.Uniq(lo.Filter(lo.Map(competitions, func(competition football.Competition, index int) string {
return competition.Country.Slug
}), func(slug string, index int) bool {
return slug != ""
}))
}
func ExtractCountrySlugsFromTeams(teams []football.Team) []string {
return lo.Uniq(lo.Filter(lo.Map(teams, func(team football.Team, index int) string {
return team.Country.Slug
}), func(slug string, index int) bool {
return slug != ""
}))
}
func ExtractCountrySlugsFromCompetitionsClubsAndNationalTeams(
competitions []football.Competition,
clubs []football.Club,
nationalTeams []football.NationalTeam,
) []string {
return lo.Uniq(lo.Union(
ExtractCountrySlugsFromCompetitions(competitions),
ExtractCountrySlugsFromTeams(lo.Map(clubs, func(club football.Club, index int) football.Team {
return club.Team
})),
ExtractCountrySlugsFromTeams(
lo.Map(nationalTeams, func(nationalTeam football.NationalTeam, index int) football.Team {
return nationalTeam.Team
}),
),
))
}
func ExtractCompetitionSlugsFromGames(games []football.Game) []string {
return lo.Uniq(lo.Filter(lo.Map(games, func(game football.Game, index int) string {
return game.Competition.Slug
}), func(slug string, index int) bool {
return slug != ""
}))
}
func ExtractCompetitionSlugsFromClubs(clubs []football.Club) []string {
return lo.Uniq(lo.Filter(lo.Map(clubs, func(club football.Club, index int) string {
return club.DomesticLeague.Slug
}), func(slug string, index int) bool {
return slug != ""
}))
}
func ExtractCompetitionSlugsFromGamesAndClubs(games []football.Game, clubs []football.Club) []string {
return lo.Uniq(lo.Union(
ExtractCompetitionSlugsFromGames(games),
ExtractCompetitionSlugsFromClubs(clubs),
))
}