예제 #1
0
        private async Task <IList <ProductStantekViewModel> > GetProductsFromCategory(IList <int> productIds, Category category)
        {
            var builder = new UriBuilder("https://stantek.com");

            builder.Port = -1;
            var query = HttpUtility.ParseQueryString(builder.Query);


            HttpClient httpClient = new HttpClient();
            IList <ProductStantekViewModel> products = new List <ProductStantekViewModel>();

            foreach (var id in productIds)
            {
                query["i"]    = id.ToString();
                builder.Query = query.ToString();
                string url = builder.ToString();

                ProductStantekViewModel product = await GetProductDetails(url, httpClient);

                product.Category = category.Name;

                products.Add(product);
            }



            return(products);
        }
예제 #2
0
        private async Task <ProductStantekViewModel> GetProductDetails(string url, HttpClient httpClient)
        {
            string html;

            try
            {
                html = await httpClient.GetStringAsync(url);
            }
            catch (Exception)
            {
                throw new Exception();
            }

            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(html);

            var productDiv = htmlDocument.DocumentNode.Descendants("div")
                             .Where(node => node.GetAttributeValue("class", "")
                                    .Equals("item-info-desc"))
                             .FirstOrDefault();

            // TODO https://weblog.west-wind.com/posts/2012/jul/19/net-html-sanitation-for-rich-html-input - Make class HtmlSanitizer

            string name = "";

            if (productDiv != null)
            {
                name = productDiv.Descendants("div")
                       .LastOrDefault()
                       .InnerText
                       .Trim();

                if (name.Length > ValidationConstants.StandartMaxLength)
                {
                    name = name.Substring(0, ValidationConstants.StandartMaxLength);
                }
            }

            var pricesDivs = htmlDocument.DocumentNode.Descendants("div")
                             .Where(node => node.GetAttributeValue("class", "")
                                    .Equals("item-info-order"))
                             .FirstOrDefault()
                             .Descendants("div")
                             .Where(node => node.GetAttributeValue("class", "")
                                    .Equals("item-price"))
                             .FirstOrDefault()
                             .Descendants("div")
                             .ToArray();

            decimal oldPrice = 0;
            decimal price    = 0;

            if (pricesDivs != null)
            {
                string oldPriceLeva = pricesDivs[1].InnerText.Trim();
                // Remove "лв."
                oldPriceLeva = oldPriceLeva.Substring(0, oldPriceLeva.Length - 3);

                oldPrice = decimal.Parse(oldPriceLeva) / 100;

                string priceLeva = pricesDivs[2].InnerText.Trim();
                // Remove "лв."
                priceLeva = priceLeva.Substring(0, priceLeva.Length - 3);

                price = decimal.Parse(priceLeva) / 100;
            }

            string pictureUrl = htmlDocument.DocumentNode.Descendants("img")
                                .Where(node => node.GetAttributeValue("id", "")
                                       .Equals("item-info-big-image"))
                                .FirstOrDefault()
                                .ChildAttributes("src")
                                .FirstOrDefault()
                                .Value;

            var fullDescriptionNode = htmlDocument.DocumentNode.Descendants("div")
                                      .Where(node => node.GetAttributeValue("class", "")
                                             .Equals("more-info-panel"))
                                      .FirstOrDefault();

            string fullDescription = fullDescriptionNode == null ? "" : fullDescriptionNode.InnerHtml;

            ProductStantekViewModel productViewModel = new ProductStantekViewModel()
            {
                Name            = name,
                PictureUrl      = "../../images/no-image.jpg", // string.IsNullOrEmpty(pictureUrl) ? "../../images/no-image.jpg" : "https://stantek.com" + pictureUrl,
                Price           = price,
                OldPrice        = oldPrice,
                Discount        = oldPrice > 0 ? Math.Round((double)(100 - (oldPrice / price * 100)), MidpointRounding.AwayFromZero) : 0,
                FullDescription = fullDescription,
            };

            return(productViewModel);
        }