Ejemplo n.º 1
0
        public void Save(ProductFeedbackItem item)
        {
            var feedback = item.Id == 0
                ? Db.CreateAndAdd<tblProductFeedback>()
                : Db.Set<tblProductFeedback>().Single(x => x.Id == item.Id);

            feedback.Comment = item.Comment;
            feedback.Minuses = item.Minuses;
            feedback.Pluses = item.Pluses;
            feedback.ProductId = item.ProductId;
            feedback.Stars = item.Stars;
            feedback.UserName = item.UserName;

            Db.SaveChanges();

            item.Id = feedback.Id;
        }
Ejemplo n.º 2
0
        public static List<ProductFeedbackItem> ParseFeedbacks(string url)
        {
            return PhantomJsUtils.Process(driver =>
            {
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));
                driver.Navigate().GoToUrl(url);
                var pageSource = driver.PageSource;
                var docuemnt = new HtmlDocument();
                docuemnt.LoadHtml(pageSource);

                var productFeedbackItems = new List<ProductFeedbackItem>();
                foreach (var reviewItem in docuemnt.DocumentNode.CssSelect(".product-review-item").ToList())
                {
                    ProductFeedbackItem productFeedbackItem = null;
                    try
                    {
                        var rating = reviewItem.CssSelect("div.rating > meta").Single().Attributes["content"].Value.TryParseNullable<decimal>();
                        var user = reviewItem.CssSelect(".product-review-user a").FirstOrDefault()?.InnerText ?? "Анонимно";
                        var pluses = reviewItem.TryDo(x => x.CssSelect(".product-review-item__stat").Skip(1).First().CssSelect(".product-review-item__text").First().InnerText);
                        var minuses = reviewItem.TryDo(x => x.CssSelect(".product-review-item__stat").Skip(2).First().CssSelect(".product-review-item__text").First().InnerText);
                        var comment = reviewItem.TryDo(x => x.CssSelect(".product-review-item__text").Last().InnerHtml);

                        productFeedbackItem = new ProductFeedbackItem
                        {
                            Stars = (int?)Math.Round(rating ?? 0),
                            Comment = comment,
                            Minuses = minuses,
                            Pluses = pluses,
                            UserName = user
                        };

                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                    productFeedbackItems.Add(productFeedbackItem);
                }

                return productFeedbackItems;
            });
        }