Example #1
0
        public async Task <HtmlNode> GetHtml()
        {
            if (_Html == null)
            {
                if (EnableUrlValidation)
                {
                    var pageResponse = await _httpClient.GetAsync(PageUrl);

                    if (!AcceptedStatusCodes.Contains(pageResponse.StatusCode))
                    {
                        throw new NotFoundException($"{PageUrl} {pageResponse.StatusCode.ToString()} {pageResponse.ReasonPhrase}");
                    }
                }

                Document = new HtmlDocument();

                using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)))
                {
                    driver.Navigate().GoToUrl(PageUrl);
                    Thread.Sleep(TimeSpan.FromSeconds(20));
                    Document.LoadHtml(driver.PageSource);

                    driver.Close();
                }

                _Html = Document.DocumentNode.FirstChild;
            }

            return(_Html);
        }
Example #2
0
        private async Task <Tuple <ActionResult, HttpResponseMessage> > Validate(string fabelioProductURL, string scenario = "new-product")
        {
            Uri  uriResult;
            bool isUriValid = Uri.TryCreate(fabelioProductURL, UriKind.Absolute, out uriResult) &&
                              (uriResult.Scheme == Uri.UriSchemeHttps || uriResult.Scheme == Uri.UriSchemeHttp);

            bool isFabelioSite = uriResult.Host.Contains("fabelio");

            if (string.IsNullOrEmpty(fabelioProductURL) || !isUriValid || !isFabelioSite)
            {
                return(new Tuple <ActionResult, HttpResponseMessage>(ReplyError("Invalid Product Url"), null));
            }

            if (scenario == "new-product")
            {
                if (_productRepo.ProductSet.AsNoTracking().Any(o => o.PageUrl == fabelioProductURL))
                {
                    return(new Tuple <ActionResult, HttpResponseMessage>(ReplyError("Product Already Exists"), null));
                }
            }
            else if (scenario == "update-product")
            {
                if (!_productRepo.ProductSet.AsNoTracking().Any(o => o.PageUrl == fabelioProductURL))
                {
                    return(new Tuple <ActionResult, HttpResponseMessage>(ReplyError("Product Not Exists"), null));
                }
            }

            var pageResponse = await _httpClient.GetAsync(fabelioProductURL);


            if (!AcceptedStatusCodes.Contains(pageResponse.StatusCode))
            {
                return(new Tuple <ActionResult, HttpResponseMessage>(ReplyError(pageResponse.ReasonPhrase), null));
            }

            var html = new HtmlAgilityPack.HtmlDocument();

            html.LoadHtml(await pageResponse.Content.ReadAsStringAsync());

            if (html.DocumentNode.SelectSingleNode("(//div[contains(@class,'product media')])[1]") == null)
            {
                return(new Tuple <ActionResult, HttpResponseMessage>(ReplyError("Its not product page"), null));
            }

            return(new Tuple <ActionResult, HttpResponseMessage>(Ok(), pageResponse));
        }