Example #1
0
        // Amazon UK price history can be checked in uk.camelcamelcamel.com, for example: http://uk.camelcamelcamel.com/Sennheiser-Professional-blocking-gaming-headset-Black/product/B00JQDOANK
        private static double? GetAmazonProductPrice(string p_amazonProductUrl)
        {
            string errorMessage = String.Empty;
            double price = 0.0;
            var webpage = new HttpClient().GetStringAsync(p_amazonProductUrl).Result;
            Utils.Logger.Info("HttpClient().GetStringAsync returned: " + ((webpage.Length > 100) ? webpage.Substring(0, 100) : webpage));

            // <span id="priceblock_ourprice" class="a-size-medium a-color-price">£199.95</span>
            string searchStr = @"id=""priceblock_ourprice"" class=""a-size-medium a-color-price"">";
            int startInd = webpage.IndexOf(searchStr);
            if (startInd == -1)
            {   // it is expected (not an exception), that sometimes Amazon changes its website, so we will fail. User will be notified.
                Utils.Logger.Info($"searchString '{searchStr}' was not found.");
                return null;
            }
            int endInd = webpage.IndexOf('<', startInd + searchStr.Length);
            if (endInd == -1)
            {   // it is expected (not an exception), that sometimes Amazon changes its website, so we will fail. User will be notified.
                Utils.Logger.Info($"'<' after searchString '{searchStr}' was not found.");
                return null;
            }
            string priceStr = webpage.Substring(startInd + searchStr.Length + 1, endInd - (startInd + searchStr.Length + 1));
            if (!Double.TryParse(priceStr, out price))
            {
                Utils.Logger.Info($"{priceStr} cannot be parsed to Double.");
                return null;
            }
            return price;
        }