コード例 #1
0
        public async Task <SitePriceInfo> GetSiteInfo(Uri url)
        {
            var document = await this._documentLoader.LoadDocument(url, this._source.CustomHeaders);

            if (document == null)
            {
                throw new ParseException("Error parsing the document", url);
            }

            var sku = this.GetProductSku(document);

            using (var httpClient = new HttpClient {
                BaseAddress = this._source.Domain
            })
            {
                ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var productInfo = await GetProductInfo(httpClient, sku);

                var priceInfo = await GetProductPrice(httpClient, sku);

                var title    = productInfo.Name;
                var imageUrl = $"https://s7d2.scene7.com/is/image/homedepotcanada/p_{sku}.jpg";
                var price    = priceInfo.OptimizedPrice.DisplayPrice?.Value ?? productInfo.Price.Value;

                ServicePointManager.ServerCertificateValidationCallback = null;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;

                var sitePriceInfo = new SitePriceInfo
                {
                    ProductIdentifier = string.Empty,
                    Uri      = url.AbsoluteUri,
                    Title    = title,
                    ImageUrl = imageUrl,
                    Price    = price
                };

                return(sitePriceInfo);
            }
        }
コード例 #2
0
        public async Task <SitePriceInfo> HandleGetInfo(Uri url)
        {
            SitePriceInfo siteInfo = null;
            var           cleanUrl = this.HandleCleanUrl(url);

            var currentIndex = 0;
            var parsers      = this.Commands.OfType <IInspector>().ToList();

            while (siteInfo == null && currentIndex < parsers.Count)
            {
                siteInfo = await parsers[currentIndex].GetSiteInfo(cleanUrl);
                currentIndex++;
            }

            if (siteInfo != null)
            {
                siteInfo.Uri = cleanUrl.AbsoluteUri;
            }

            return(siteInfo);
        }