Ejemplo n.º 1
0
        public async Task <RatingParameters> SetRating(RatingParameters rating)
        {
            var temp = await repo.GetOneEnterprise(rating.EnterpriseId);

            var car = await repo2.GetOneCar(rating.EnterpriseId, rating.CarId);

            var rating1 = new EnterpriseRating();
            var rating2 = new CarRating();

            rating1.Rating = rating.EnterpriseRating;
            rating2.Rating = rating.CarRating;

            temp.Rating.Add(rating1);
            car.Ratings.Add(rating2);
            try
            {
                await unitOfWork.CompleteAsync();
            }
            catch
            {
                return(null);
            }

            return(rating);
        }
Ejemplo n.º 2
0
        private async Task <List <dynamic> > GetPageData(string url, List <dynamic> results)
        {
            var httpClient = new HttpClient();
            var html       = await httpClient.GetStringAsync(url);

            CarRating advert = new CarRating();

            var htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(html);

            var carRatesHTMLView = htmlDocument.DocumentNode.Descendants("tbody").ToList();

            string carType = "";
            var    allRows = carRatesHTMLView[0].Descendants("tr").ToList();

            foreach (var row in allRows)
            {
                var category = row.Descendants("td")
                               .Where(node => node.GetAttributeValue("class", "")
                                      .Contains("lightgray")).ToList();

                if (category.Count != 0)
                {
                    carType = category[0].InnerText.Trim('\n', '\t', '\r');
                }
                else
                {
                    var data = row.Descendants("td")
                               .Where(node => node.GetAttributeValue("class", "")
                                      .Contains("text")).ToList();

                    if (data.Count == 4)
                    {
                        advert              = new CarRating();
                        advert.CarCategory  = carType;
                        advert.Model        = data[0].InnerText;
                        advert.RatePerMonth = float.Parse(data[1].InnerText.Trim(','));
                        advert.RatePerWeek  = float.Parse(data[2].InnerText.Trim(','));
                        advert.Milleage     = float.Parse(data[3].InnerText.Trim(','));
                        results.Add(advert);
                        _logger.LogInformation("New Car Rating web scraped");
                    }
                }
            }
            return(results);
        }