public async Task ProcessRosters(FixtureRosters rosters, FixtureShots shots, FixtureMatchInfo matchInfo, IDbConnection conn) { var homePlayers = rosters.Home.Values.ToList(); var awayPlayers = rosters.Away.Values.ToList(); var players = homePlayers.Union(awayPlayers).ToList(); var allShots = shots.Home.Union(shots.Away).ToList(); var playerEntities = players.Select(p => new Entities.Player { Id = p.PlayerId, Name = p.PlayerName }).ToList(); await _playerRepository.InsertPlayersAsync(playerEntities, conn); var fixturePlayers = new List <Entities.FixturePlayer>(); foreach (var player in players) { var rosterOutPlayer = players.Find(p => p.Id == player.RosterOut); var rosterInPlayer = players.Find(p => p.Id == player.RosterIn); var fixturePlayer = new Entities.FixturePlayer { ExpectedGoalsBuildup = player.ExpectedGoalsBuildup, ExpectedGoalsChain = player.ExpectedGoalsChain, Minutes = player.Time, Player = new Entities.Player { Id = player.PlayerId }, Position = player.Position, PositionOrder = player.PositionOrder, RedCards = player.RedCard, YellowCards = player.YellowCard, Assists = player.Assists, ExpectedAssists = player.ExpectedAssists, ExpectedGoals = player.ExpectedGoals, FixtureId = matchInfo.Id, Goals = player.Goals, KeyPasses = player.KeyPasses, OwnGoals = player.OwnGoals, Shots = player.Shots, ShotsOnTarget = allShots.Count(s => s.PlayerId == player.PlayerId && (s.Result == "Goal" || s.Result == "SavedShot")), Replaced = rosterOutPlayer == null ? null : new Entities.Player { Id = rosterOutPlayer.PlayerId }, ReplacedBy = rosterInPlayer == null ? null : new Entities.Player { Id = rosterInPlayer.PlayerId }, Team = new Entities.Team { Id = player.TeamId } }; fixturePlayers.Add(fixturePlayer); } await _fixtureRepository.InsertFixturePlayers(fixturePlayers, conn); }
public async Task ProcessShots(FixtureShots shots, FixtureRosters rosters, FixtureMatchInfo matchInfo, IDbConnection conn) { var allShots = shots.Home.Union(shots.Away).ToList(); var homePlayers = rosters.Home.Values.ToList(); var awayPlayers = rosters.Away.Values.ToList(); var entities = new List <Entities.FixtureShot>(); foreach (var shot in allShots) { var entity = new Entities.FixtureShot(); // Try and resolve the assister if there is one if (shot.PlayerAssistedName != null) { FixtureRosterEntry matchingPlayer = null; if (shot.HomeOrAway == "h") { matchingPlayer = homePlayers.Find(v => v.PlayerName == shot.PlayerAssistedName); } else if (shot.HomeOrAway == "a") { matchingPlayer = awayPlayers.Find(p => p.PlayerName == shot.PlayerAssistedName); } if (matchingPlayer != null) { entity.Assist = new Entities.Player { Id = matchingPlayer.PlayerId }; } } entity.ExpectedGoal = shot.ExpectedGoal; entity.ShotId = shot.Id; entity.Player = new Entities.Player { Id = shot.PlayerId }; entity.Minute = shot.Minute; entity.Result = shot.Result; entity.X = shot.X; entity.Y = shot.Y; entity.FixtureId = matchInfo.Id; entity.ExpectedGoal = shot.ExpectedGoal; entity.Team = new Entities.Team { Id = shot.HomeOrAway == "h" ? matchInfo.HomeTeamId : matchInfo.AwayTeamId }; entity.Situation = shot.Situation; entity.Type = shot.ShotType; entity.LastAction = shot.LastAction; entities.Add(entity); } await _fixtureRepository.InsertFixtureShots(entities, conn); }
public async Task Run(int fixtureId) { try { _logger.LogTrace($"Entering Run({fixtureId})"); FixtureRosters rosters = null; FixtureShots shots = null; FixtureMatchInfo matchInfo = null; using (var conn = await _connectionProvider.GetOpenConnectionAsync()) using (var trans = conn.BeginTransaction()) { var browser = await Puppeteer.ConnectAsync(await _chromeHelper.GetConnectOptionsAsync()); try { using (var page = await browser.NewPageAsync()) { var url = $"https://understat.com/match/{fixtureId}"; _logger.LogInformation("Loading " + url); var response = await page.GoToAsync(url, new NavigationOptions { WaitUntil = new WaitUntilNavigation[] { WaitUntilNavigation.Networkidle2 } }); if (!response.Ok) { _logger.LogError($"Unable to load {url}. Http Status {response.Status}"); return; } _logger.LogInformation($"Success loading {url}"); var sd = await page.EvaluateExpressionAsync("shotsData"); var rd = await page.EvaluateExpressionAsync("rostersData"); var mi = await page.EvaluateExpressionAsync("match_info"); shots = sd.ToObject <FixtureShots>(); rosters = rd.ToObject <FixtureRosters>(); matchInfo = mi.ToObject <FixtureMatchInfo>(); } } finally { browser.Disconnect(); } await _fixtureDetailsManager.ProcessRosters(rosters, shots, matchInfo, conn); await _fixtureDetailsManager.ProcessShots(shots, rosters, matchInfo, conn); await _fixtureDetailsManager.ProcessMatchInfo(matchInfo, conn); await _fixtureDetailsManager.ConfirmDetailsSaved(fixtureId, conn); trans.Commit(); _logger.LogTrace($"Exiting Run({fixtureId})"); } } catch (Exception ex) { _logger.LogError(ex, $"Error running FixtureDetailsScraper for fixture {fixtureId}"); throw; } }