282 lines
8.0 KiB
Go
282 lines
8.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"git.lehouerou.net/laurent/sorare"
|
||
|
"git.lehouerou.net/laurent/sorare/football"
|
||
|
"git.lehouerou.net/laurent/sorare/graphql"
|
||
|
"github.com/jackc/pgx/v5/pgtype"
|
||
|
"github.com/pkg/errors"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
"github.com/samber/lo"
|
||
|
"github.com/spf13/cobra"
|
||
|
|
||
|
"git.lehouerou.net/laurent/sorarebuddy/cmd/common"
|
||
|
"git.lehouerou.net/laurent/sorarebuddy/model"
|
||
|
)
|
||
|
|
||
|
var Cmd = &cobra.Command{
|
||
|
Use: "console",
|
||
|
Short: "console",
|
||
|
Long: `console`,
|
||
|
RunE: run,
|
||
|
PersistentPreRunE: common.CmdPreRunE,
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
common.Start(Cmd)
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
common.InitParams(Cmd)
|
||
|
}
|
||
|
|
||
|
func run(cmd *cobra.Command, _ []string) error {
|
||
|
//var wg conc.WaitGroup
|
||
|
|
||
|
ctx := cmd.Context()
|
||
|
s, ok := ctx.Value(common.SorareContextKey).(*sorare.Sorare)
|
||
|
if !ok {
|
||
|
return errors.New("sorare not found in context")
|
||
|
}
|
||
|
|
||
|
db, ok := ctx.Value(common.DbContextKey).(*model.Queries)
|
||
|
if !ok {
|
||
|
return errors.New("db not found in context")
|
||
|
}
|
||
|
|
||
|
sfixtures, err := s.Football.So5.So5Fixtures.Get(ctx, football.So5FixturesParams{})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Debug().Msgf("fixtures: %v", sfixtures)
|
||
|
|
||
|
cnt, err := db.CreateFixtures(
|
||
|
ctx,
|
||
|
lo.Map(sfixtures, func(fixture football.So5Fixture, index int) model.CreateFixturesParams {
|
||
|
return model.CreateFixturesParams{
|
||
|
Slug: fixture.Slug,
|
||
|
DisplayName: fixture.DisplayName,
|
||
|
State: fixture.AasmState,
|
||
|
StartDate: pgtype.Timestamptz{Time: fixture.StartDate, Valid: true},
|
||
|
EndDate: pgtype.Timestamptz{Time: fixture.EndDate, Valid: true},
|
||
|
GameWeek: int32(fixture.GameWeek),
|
||
|
}
|
||
|
}),
|
||
|
)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Debug().Msgf("created %d fixtures", cnt)
|
||
|
|
||
|
fixtures, err := db.GetAllFixtures(ctx)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
var games []football.Game
|
||
|
for _, fixture := range fixtures {
|
||
|
log.Debug().Msgf("fixture: %v", fixture)
|
||
|
g, err := s.Football.So5.FixtureGames(fixture.Slug).Get(ctx, graphql.EmptyParams{})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Debug().Msgf("games: %d", len(g))
|
||
|
games = append(games, g...)
|
||
|
}
|
||
|
log.Debug().Msgf("games: %d", len(games))
|
||
|
|
||
|
var teamSlugs []string
|
||
|
teamSlugs = lo.Map(games, func(game football.Game, index int) string {
|
||
|
return game.AwayTeam.Team.Slug
|
||
|
})
|
||
|
teamSlugs = append(teamSlugs, lo.Map(games, func(game football.Game, index int) string {
|
||
|
return game.HomeTeam.Team.Slug
|
||
|
})...)
|
||
|
teamSlugs = lo.Filter(teamSlugs, func(slug string, index int) bool {
|
||
|
return slug != ""
|
||
|
})
|
||
|
teamSlugs = lo.Uniq(teamSlugs)
|
||
|
|
||
|
var clubs []football.Club
|
||
|
for _, chunk := range lo.Chunk(teamSlugs, 100) {
|
||
|
t, err := s.Football.Clubs.Get(ctx, graphql.SlugsParams{Slugs: chunk})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
clubs = append(clubs, t...)
|
||
|
}
|
||
|
|
||
|
var nationalTeams []football.NationalTeam
|
||
|
for _, chunk := range lo.Chunk(lo.Without(teamSlugs, lo.Map(clubs, func(club football.Club, index int) string {
|
||
|
return club.Slug
|
||
|
})...), 100) {
|
||
|
t, err := s.Football.NationalTeams.Get(ctx, graphql.SlugsParams{Slugs: chunk})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
nationalTeams = append(nationalTeams, t...)
|
||
|
}
|
||
|
|
||
|
var teamsToInsert []model.CreateTeamsParams
|
||
|
teamsToInsert = append(teamsToInsert, lo.Map(clubs, func(club football.Club, index int) model.CreateTeamsParams {
|
||
|
return model.CreateTeamsParams{
|
||
|
Slug: club.Slug,
|
||
|
DisplayName: club.Name,
|
||
|
CountrySlug: club.Country.Slug,
|
||
|
DomesticLeagueSlug: func() *string {
|
||
|
if club.DomesticLeague.Slug == "" {
|
||
|
return nil
|
||
|
}
|
||
|
return &club.DomesticLeague.Slug
|
||
|
}(),
|
||
|
ShortName: club.ShortName,
|
||
|
PictureUrl: club.PictureUrl,
|
||
|
TeamType: "club",
|
||
|
}
|
||
|
})...)
|
||
|
teamsToInsert = append(
|
||
|
teamsToInsert,
|
||
|
lo.Map(nationalTeams, func(nationalTeam football.NationalTeam, index int) model.CreateTeamsParams {
|
||
|
return model.CreateTeamsParams{
|
||
|
Slug: nationalTeam.Slug,
|
||
|
DisplayName: nationalTeam.Name,
|
||
|
CountrySlug: nationalTeam.Country.Slug,
|
||
|
DomesticLeagueSlug: nil,
|
||
|
ShortName: nationalTeam.ShortName,
|
||
|
PictureUrl: nationalTeam.PictureUrl,
|
||
|
TeamType: "national",
|
||
|
}
|
||
|
})...)
|
||
|
|
||
|
competitionSlugs := lo.Map(games, func(game football.Game, index int) string {
|
||
|
return game.Competition.Slug
|
||
|
})
|
||
|
competitionSlugs = append(
|
||
|
competitionSlugs,
|
||
|
lo.Map(teamsToInsert, func(team model.CreateTeamsParams, index int) string {
|
||
|
if team.DomesticLeagueSlug == nil {
|
||
|
return ""
|
||
|
}
|
||
|
return *team.DomesticLeagueSlug
|
||
|
})...)
|
||
|
competitionSlugs = lo.Filter(competitionSlugs, func(slug string, index int) bool {
|
||
|
return slug != ""
|
||
|
})
|
||
|
competitionSlugs = lo.Uniq(competitionSlugs)
|
||
|
|
||
|
var competitions []football.Competition
|
||
|
for _, slug := range competitionSlugs {
|
||
|
log.Debug().Msgf("competition: %s", slug)
|
||
|
c, err := s.Football.Competition.Get(ctx, graphql.SlugParams{Slug: slug})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
competitions = append(competitions, c)
|
||
|
}
|
||
|
|
||
|
var countrySlugs []string
|
||
|
countrySlugs = append(countrySlugs, lo.Map(teamsToInsert, func(team model.CreateTeamsParams, index int) string {
|
||
|
return team.CountrySlug
|
||
|
})...)
|
||
|
countrySlugs = append(countrySlugs, lo.Map(competitions, func(competition football.Competition, index int) string {
|
||
|
return competition.Country.Slug
|
||
|
})...)
|
||
|
countrySlugs = lo.Filter(countrySlugs, func(slug string, index int) bool {
|
||
|
return slug != ""
|
||
|
})
|
||
|
countrySlugs = lo.Uniq(countrySlugs)
|
||
|
|
||
|
var countries []sorare.Country
|
||
|
for _, chunk := range lo.Chunk(countrySlugs, 100) {
|
||
|
c, err := s.Countries.Get(ctx, graphql.SlugsParams{Slugs: chunk})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
countries = append(countries, c...)
|
||
|
}
|
||
|
|
||
|
cnt, err = db.CreateCountries(
|
||
|
ctx,
|
||
|
lo.Map(countries, func(country sorare.Country, index int) model.CreateCountriesParams {
|
||
|
return model.CreateCountriesParams{
|
||
|
Slug: country.Slug,
|
||
|
Code: country.Code,
|
||
|
DisplayName: country.Name,
|
||
|
ThreeLetterCode: country.ThreeLetterCode,
|
||
|
FlagFlat64Url: country.FlagFlat64Url,
|
||
|
FlagFlat32Url: country.FlagFlat32Url,
|
||
|
FlagRound64Url: country.FlagRound64Url,
|
||
|
FlagRound32Url: country.FlagRound32Url,
|
||
|
}
|
||
|
}),
|
||
|
)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Debug().Msgf("created %d countries", cnt)
|
||
|
|
||
|
log.Debug().Msgf("competitions: %d", len(competitions))
|
||
|
for _, competition := range competitions {
|
||
|
|
||
|
err := db.CreateOrUpdateCompetition(ctx, model.CreateOrUpdateCompetitionParams{
|
||
|
Slug: competition.Slug,
|
||
|
CompetitionFormat: competition.Format,
|
||
|
CompetitionType: competition.Type,
|
||
|
DisplayName: competition.DisplayName,
|
||
|
PictureUrl: competition.PictureUrl,
|
||
|
LogoUrl: competition.LogoUrl,
|
||
|
CountrySlug: competition.Country.Slug,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cnt, err = db.CreateTeams(ctx, teamsToInsert)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
log.Debug().Msgf("created %d teams", cnt)
|
||
|
|
||
|
cnt, err = db.CreateGames(ctx, lo.Map(games, func(game football.Game, index int) model.CreateGamesParams {
|
||
|
return model.CreateGamesParams{
|
||
|
CoverageStatus: game.CoverageStatus,
|
||
|
LowCoverage: game.LowCoverage,
|
||
|
Minutes: int32(game.Minute),
|
||
|
PeriodType: game.PeriodType,
|
||
|
Scored: game.Scored,
|
||
|
Status: game.Status,
|
||
|
CompetitionSlug: game.Competition.Slug,
|
||
|
FixtureSlug: game.So5Fixture.Slug,
|
||
|
AwayTeamSlug: game.AwayTeam.Team.Slug,
|
||
|
AwayGoals: int32(game.AwayGoals),
|
||
|
AwayExtraTimeScore: int32(game.ExtraTimeScoreAway),
|
||
|
AwayPenaltyScore: int32(game.PenaltyScoreAway),
|
||
|
HomeTeamSlug: game.HomeTeam.Team.Slug,
|
||
|
HomeGoals: int32(game.HomeGoals),
|
||
|
HomeExtraTimeScore: int32(game.ExtraTimeScoreHome),
|
||
|
HomePenaltyScore: int32(game.PenaltyScoreHome),
|
||
|
WinnerTeamSlug: func() *string {
|
||
|
if game.Winner.Team.Slug == "" {
|
||
|
return nil
|
||
|
}
|
||
|
return &game.Winner.Team.Slug
|
||
|
}(),
|
||
|
}
|
||
|
}))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Debug().Msgf("created %d games", cnt)
|
||
|
|
||
|
// log.Debug().Msg("start sequence completed. waiting for shutdown request")
|
||
|
// <-ctx.Done()
|
||
|
// log.Debug().Msg("shutdown requested")
|
||
|
// wg.Wait()
|
||
|
// log.Debug().Msg("shutdown complete")
|
||
|
return nil
|
||
|
|
||
|
}
|