private async Task SetPriceForProduct(BonusProductEvent product)
        {
            var url = string.Format(AH_PRODUCT_URL, product.Id);

            try
            {
                var response = await httpClient.GetAsync(url).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(content);
                var priceNode = doc.DocumentNode.SelectSingleNode(@"//*[@class=""product-card-hero-price_root__PgaiS""]");

                var fromPriceNode  = priceNode.ChildNodes[0];
                var fromPriceWhole = int.Parse(fromPriceNode.ChildNodes[0].InnerText) * 100;
                var fromPriceCents = int.Parse(fromPriceNode.ChildNodes[2].InnerText);

                product.FromPriceInCents = fromPriceWhole + fromPriceCents;

                var forPriceNode  = priceNode.ChildNodes[1];
                var forPriceWhole = int.Parse(forPriceNode.ChildNodes[0].InnerText) * 100;
                var forPriceCents = int.Parse(forPriceNode.ChildNodes[2].InnerText);

                product.ForPriceInCents = forPriceWhole + forPriceCents;
            }
            catch (Exception exc)
            {
                this.logger.LogError("Unable to get price for {0}; {1}", product.Id, exc.Message);
            }
        }
예제 #2
0
        private async Task <IReadOnlyList <BonusProductEvent> > GetBonusProductsFromSubPage(BonusProductEvent product)
        {
            var source = AH_BASE_URL + ConstructApiPath(product.Id);

            try
            {
                var response = await client.GetAsync(source).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();

                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var detailContent = JObject.Parse(content);

                return(detailContent["cards"]
                       .SelectMany(x => x["products"])
                       .Select(x => new BonusProductEvent(x as JObject))
                       .ToList());
            }
            catch (Exception exc)
            {
                this.logger.LogError(exc, "Something went wrong retrieving the subpage at {source}", source);
                return(Array.Empty <BonusProductEvent>());
            }
        }