public async Task Import(CompetitionV competitionV, bool includeFixtures, bool includeResults, DateTime viewDate) { var startDate = Date.LowDate; var endDate = Date.HighDate; if (!competitionV.GetCampaignDates(viewDate, ref startDate, ref endDate)) { return; } // http://www.espn.co.uk/football/sport/match/index.html?event=3;page=5;season=2013%2F14 // http://www.espn.co.uk/football/sport/match/index.html?event=3;type=fixtures var country = competitionV.Competition.GetParentCountry(viewDate); countryGuid = country != null ? (Guid?)country.HeaderKey : null; var seasonId = string.Format("{0}%2F{1}", startDate.ToString("yyyy"), endDate.ToString("yy")); var eventId = (await Provider.GetLookupCompetition(competitionV.HeaderKey, ImportSite)).LookupId; if (includeFixtures) { var uri = new Uri(string.Format("http://www.espn.co.uk/football/sport/match/index.html?event={0};type=fixtures", eventId)); await ImportMatches(competitionV, uri, eventId); } var resultsFound = true; var page = 1; if (includeResults) { while (resultsFound) { var uri = new Uri(string.Format("http://www.espn.co.uk/football/sport/match/index.html?event={0};page={1};season={2}", eventId, page, seasonId)); resultsFound = await ImportMatches(competitionV, uri, eventId); page++; } } }
private Guid GetCampaignStage(HtmlNode tr, CompetitionV competitionV, DateTime matchDate) { var campaign = competitionV.Competition.Campaigns.SingleOrDefault(s => s.StartDate <= matchDate && s.EndDate >= matchDate); var cells = tr.Descendants("td").Where(t => t.Attributes.Contains("class") && t.Attributes["class"].Value == "left"); foreach (var cell in cells) { foreach (var campaignStage in campaign.CampaignStages) { var lookup = campaignStage.LookupCampaignStages.FirstOrDefault(w => w.ImportSite == ImportSite && cell.InnerText.Trim() == w.LookupId); if (lookup != null) { return(lookup.CampaignStageKey); } } } var defaultStage = campaign.CampaignStages.SingleOrDefault(s => s.IsDefault); return(defaultStage.PrimaryKey); }
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(); }
private async Task <bool> ImportMatches(CompetitionV competitionV, Uri uri, string eventId) { var resultsFound = false; var document = GetHtmlDocument(uri); var table = document.DocumentNode.Descendants("table").Where(tb => tb.Attributes["class"].Value == "engineTable").FirstOrDefault(); var baseDate = Date.LowDate; foreach (var tr in table.Descendants("tr")) { if (tr.Descendants("td").Any(t => t.InnerText == "No matches found")) { return(false); } switch (tr.Attributes["class"].Value) { case "heading": var temp = tr.Descendants("a").First().Attributes["href"].Value; baseDate = DateTime.Parse(temp.GetTextBetween("?date=", ";")); break; case "data1": var columnCount = tr.Descendants("td").Count(); //Skip check if (eventId == "21" && tr.Descendants("td").Count() >= 7 && tr.Descendants("td").ElementAt(6).InnerText.IndexOf("HASH") > 0) { break; } if (tr.InnerText.Contains("After extra time") || tr.InnerText.Contains("penalties in progress")) { break; } var matchDate = GetMatchTime(tr, baseDate); var team1Guid = await GetTeamGuid(tr, true); var team2Guid = await GetTeamGuid(tr, false); var venueGuid = await GetVenueGuid(team1Guid, matchDate); var team1Ft = GetFullTimeScore(tr, true); var team2Ft = GetFullTimeScore(tr, false); var team1Ht = GetHalfTimeScore(tr, true); var team2Ht = GetHalfTimeScore(tr, false); var attendance = GetAttendance(tr); var matchId = GetMatchLookupId(tr); var campaignStageKey = GetCampaignStage(tr, competitionV, matchDate); //MatchType matchType = MatchType.League; //StageType? stageType = null; //if (competitionV.CompetitionType == CompetitionType.Cup) // GetMatchAndStageTypes(tr, out matchType, out stageType); await ProcessMatch(matchId, matchDate, competitionV, venueGuid, attendance, team1Guid, team1Ht, team1Ft, team2Guid, team2Ht, team2Ft, campaignStageKey); resultsFound = true; break; } } return(resultsFound); }
public void Remove(CompetitionV competitionV) { CompetitionVRepository.Remove(competitionV); }
public void Add(CompetitionV competitionV) { CompetitionVRepository.Add(competitionV); }
public static void CompetitionBreadcrumb(this IList<BreadcrumbViewModel> breadcrumbViewModels, CompetitionV competitionV, DateTime viewDate) { breadcrumbViewModels.OrganisationBreadcrumb(competitionV.Organisation.GetApprovedVersion<OrganisationV>(viewDate), viewDate); breadcrumbViewModels.Add(AreaType.Cmp, competitionV.HeaderKey, competitionV.CompetitionName, string.Empty); }