private void SaveGP(string grandPrixUrl, string webId) { Uri url = new Uri(grandPrixUrl); HtmlReader reader = new HtmlReader(); string html = reader.GetHtmlFromUrl(url); object[] grandPrixRead = F1HtmlParserHelper.ParseRacesHtml(html); GrandPrix gp = new GrandPrix() { Name = (string)grandPrixRead[0], CircuitName = (string)grandPrixRead[1], CelebrationDate = (DateTime)grandPrixRead[2], CircuitLength = Convert.ToDecimal((double)grandPrixRead[4]), LapsNo = Convert.ToInt32((byte)grandPrixRead[3]), WebId = webId }; var grandPrixRepo = this.container.Resolve<IGenericRepository<GrandPrix>>(); List<GrandPrix> existing = grandPrixRepo.GetAll().Where(x => x.CelebrationDate == gp.CelebrationDate).ToList<GrandPrix>(); if (existing.Count == 0) { grandPrixRepo.Save(gp); } }
/// <summary> /// Gets the race results. /// </summary> /// <param name="season">The season.</param> /// <param name="gp">The Grand Prix.</param> /// <returns>A list of RaceResults.</returns> public IList<RaceResult> GetRaceResults(int season, GrandPrix gp) { if (DateTime.Now.AddDays(1) < gp.CelebrationDate) { throw new InvalidOperationException("Grand Prix results will not be available until " + gp.CelebrationDate.AddDays(1).ToShortDateString()); } IList<RaceResult> results = new List<RaceResult>(); // Check if its on DB var resultRepo = this.container.Resolve<IResultRepository>(); var uow = this.container.Resolve<IUnitOfWork>(); results = resultRepo.GetFromGP(gp); if (results.Count == 0) { // Is not on DB, get from web results = this.GetResultsFromWeb(season, gp); foreach (var item in results) { resultRepo.Save(item); } uow.Commit(); } return results.OrderByDescending(x => x.LapsNo).ThenBy(x => x.Time).ThenBy(x => x.Position).ToList<RaceResult>(); }
private IList<RaceResult> GetResultsFromWeb(int season, GrandPrix gp) { IList<RaceResult> results = new List<RaceResult>(); Uri url = new Uri("http://www.formula1.com/results/season/" + season.ToString(CultureInfo.GetCultureInfo("en-US")) + "/" + gp.WebId + "/"); HtmlReader reader = new HtmlReader(); string html = reader.GetHtmlFromUrl(url); Collection<object[]> resultsRead = F1HtmlParserHelper.ParseRaceResultHtml(html); var carRepo = this.container.Resolve<IVehicleRepository>(); TimeSpan pos1Time = new TimeSpan(); foreach (var item in resultsRead) { // Vehicle car = carRepo.GetByDriverName((string)item[2]); Vehicle car = carRepo.GetByCarNo(Convert.ToInt32((byte)item[1])); RaceResult rr = new RaceResult() { GrandPrix = gp, Position = Convert.ToInt32((byte)item[0], CultureInfo.GetCultureInfo("en-US")), Vehicle = car, LapsNo = Convert.ToInt32((byte)item[4], CultureInfo.GetCultureInfo("en-US")), Time = pos1Time.Add((TimeSpan)item[5]), Description = (string)item[6] }; if (((TimeSpan)item[5]).TotalMilliseconds == 0d) { rr.Time = TimeSpan.FromMilliseconds(-1); } if (rr.Position == 1) { pos1Time = rr.Time; } results.Add(rr); } return results; }
/// <summary> /// Gets the race results. /// </summary> /// <param name="season">The season.</param> /// <param name="gp">The Grand Prix.</param> /// <returns>A list of RaceResults.</returns> public IList<RaceResult> GetRaceResults(int season, GrandPrix gp) { ResultsServices resSvc = new ResultsServices(); return resSvc.GetRaceResults(season, gp); }