private async Task ImportMatch(Competition competition, Guid?team1Key, Guid?team2Key, string matchId, DateTime matchDate) { // http://www.soccerbase.com/matches/additional_information.sd?id_game=689457 var uri = new Uri(string.Format("http://www.soccerbase.com/matches/additional_information.sd?id_game={0}", matchId)); var document = GetHtmlDocument(uri); //win - on aggregate var matchStatus = document.DocumentNode.Descendants("p").First(p => p.Attributes["class"].Value == "status").InnerText; var isCompleted = matchStatus == "result" || matchStatus.Contains("on aggregate") || matchStatus.Contains("on penalties") || matchStatus.Contains("AFTER EXTRA TIME") || matchStatus.Contains("win on away goals"); if (!isCompleted) { return; } //Scores var span = document.DocumentNode.Descendants("span").FirstOrDefault(s => s.Attributes.Contains("title") && s.Attributes["title"].Value.Contains("Half-time")); var team1Ht = span != null?span.Descendants("em").ElementAt(0).InnerText.ToNullableShort() : null; var team2Ht = span != null?span.Descendants("em").ElementAt(1).InnerText.ToNullableShort() : null; span = document.DocumentNode.Descendants("span").FirstOrDefault(s => s.Attributes.Contains("title") && s.Attributes["title"].Value.Contains("Full-time")); var team1Ft = span != null?span.Descendants("em").ElementAt(0).InnerText.ToNullableShort() : null; var team2Ft = span != null?span.Descendants("em").ElementAt(1).InnerText.ToNullableShort() : null; //Attendance var attendance = GetAttendance(matchId); //Does match exist in Soccerbase lookup? var matchKey = await LookupMatch(matchId); Match match = null; MatchV matchV = null; //Does campaign exist? var campaign = await ProcessCampaign(competition.PrimaryKey, matchDate); if (campaign == null) { return; } var venueGuid = await GetVenueGuid((Guid)team1Key, matchDate); if (matchKey == null) { var endOfMatchDay = matchDate.ToEndOfDay(); var beginningOfMatchDay = matchDate.Date; //Does match exist in Match tables //var matchQuery = from m in provider.MatchVs // where m.CampaignGuid == campaign.PrimaryKey // && m.Team1Guid == team1Key // && m.Team2Guid == team2Key // && m.IsActive // && !m.IsMarkedForDeletion // && m.MatchDate >= beginningOfMatchDay // && m.MatchDate <= endOfMatchDay // select m; var searchMatchV = await Provider.GetMatchByCampaignAndTeams(campaign.PrimaryKey, (Guid)team1Key, (Guid)team2Key, matchDate); if (searchMatchV != null) { match = searchMatchV.Match; matchKey = match.PrimaryKey; matchV = match.GetApprovedVersion <MatchV>(matchDate); } else { match = Match.Create <Match>(); matchV = MatchV.CreateNew <MatchV>(User.GetUserId()); matchV.HeaderKey = match.PrimaryKey; matchV.EffectiveFrom = Date.LowDate; matchV.EffectiveTo = Date.HighDate; CreateLookupMatch(match.PrimaryKey, matchId); } } else { match = (await Provider.GetMatch((Guid)matchKey, matchDate)).Match; matchV = match.GetApprovedVersion <MatchV>(matchDate); } if (matchV.MatchImportType == MatchImportType.AutomaticResultWithEvents || matchV.MatchImportType == MatchImportType.ManualResult) { return; } matchV.MatchDate = matchDate.Date; matchV.MatchTimeTicks = matchV.MatchTimeTicks > 0 ? matchV.MatchTimeTicks : (matchDate - matchDate.Date).Ticks; matchV.MatchImportType = MatchImportType.AutomaticResultWithEvents; matchV.VenueGuid = venueGuid; matchV.Attendance = attendance; if (competition.GetApprovedVersion <CompetitionV>(matchDate).CompetitionType == CompetitionType.League) { var campaignStage = campaign.CampaignStages.FirstOrDefault(f => f.IsDefault); if (campaignStage != null) { matchV.CampaignStageKey = campaignStage.PrimaryKey; } } matchV.Team1Guid = (Guid)team1Key; matchV.Team1HT = team1Ht; matchV.Team1FT = team1Ft; matchV.Team2Guid = (Guid)team2Key; matchV.Team2HT = team2Ht; matchV.Team2FT = team2Ft; if (matchKey == null) { Provider.Add(match); Provider.Add(matchV); } Provider.SaveChanges(); await ProcessMatchPlayers(document, matchV); }
protected async Task ProcessMatch(string lookupId, DateTime matchDate, CompetitionV competitionV, Guid?venueGuid, int?attendance, Guid team1Guid, short?team1Ht, short?team1Ft, Guid team2Guid, short?team2Ht, short?team2Ft, Guid campaignStageKey) { if (campaignStageKey == null) { return; } var lookupMatchSearch = await Provider.GetLookupMatch(ImportSite, lookupId); var startOfDay = matchDate.Date; var endOfDay = matchDate.ToEndOfDay(); var matchSearch = await Provider.GetMatchByTeams(team1Guid, team2Guid, matchDate); var campaign = await Provider.FindCampaignAsync(competitionV.HeaderKey, matchDate); if (campaign == null) { var startDate = Date.LowDate; var endDate = Date.HighDate; if (!competitionV.GetCampaignDates(matchDate, ref startDate, ref endDate)) { return; } var newCampaign = Campaign.CreateNew(competitionV.HeaderKey, startDate, endDate); campaign = newCampaign; Provider.Add(newCampaign); Provider.SaveChanges(); } if (lookupMatchSearch != null || matchSearch != null) { MatchV matchV = null; if (lookupMatchSearch != null) { var lookupMatch = lookupMatchSearch; matchV = (await Provider.GetMatch(lookupMatch.MatchGuid, DateTime.Now)); } else if (matchSearch != null) { matchV = matchSearch; } if (matchV == null || matchV.MatchImportType == MatchImportType.ManualResult) { return; } matchV.MatchDate = matchDate.Date; matchV.MatchTimeTicks = (matchDate - matchDate.Date).Ticks; matchV.VenueGuid = venueGuid; matchV.Attendance = attendance; matchV.Team1Guid = team1Guid; matchV.Team1HT = team1Ht; matchV.Team1FT = team1Ft; matchV.Team2Guid = team2Guid; matchV.Team2HT = team2Ht; matchV.Team2FT = team2Ft; matchV.MatchImportType = matchV.GetMatchImportType(true); matchV.CampaignStageKey = campaignStageKey; } else { if (matchDate < DateTime.Now && team1Ft == null && team2Ft == null) { return; } var matchGuid = Guid.NewGuid(); Provider.Add(new Match() { PrimaryKey = matchGuid }); var matchV = MatchV.CreateNew <MatchV>(User.GetUserId()); matchV.HeaderKey = matchGuid; matchV.MatchDate = matchDate.Date; matchV.MatchTimeTicks = (matchDate - matchDate.Date).Ticks; matchV.VenueGuid = venueGuid; matchV.Attendance = attendance; matchV.Team1Guid = team1Guid; matchV.Team1HT = team1Ht; matchV.Team1FT = team1Ft; matchV.Team2Guid = team2Guid; matchV.Team2HT = team2Ht; matchV.Team2FT = team2Ft; matchV.EffectiveFrom = Date.LowDate; matchV.EffectiveTo = Date.HighDate; matchV.MatchImportType = matchV.GetMatchImportType(true); matchV.CampaignStageKey = campaignStageKey; Provider.Add(matchV); if (lookupId != string.Empty) { Provider.Add(new LookupMatch() { PrimaryKey = Guid.NewGuid(), ImportSite = ImportSite, MatchGuid = matchGuid, LookupId = lookupId }); } } Provider.SaveChanges(); }