public async Task <Product> Parse(string source) { var domParser = new HtmlParser(); var document = await domParser.ParseAsync(source); string header = await Task.Run(() => document.QuerySelectorAll(settings.HeaderPattern).FirstOrDefault().TextContent.ToString()); string priceRaw = await Task.Run(() => (document.QuerySelectorAll(settings.PricePattern)?.FirstOrDefault()?.Attributes[settings.PriceAttributeKey]?.Value)); decimal price = priceRaw != null?Decimal.Parse(priceRaw) : 0m; string url = await Task.Run(() => (document.QuerySelectorAll(settings.Url).FirstOrDefault()).Attributes["href"].Value); string description = await Task.Run(() => document.QuerySelectorAll(settings.DescriptionPattern).FirstOrDefault().TextContent); description = description.Trim().Replace('\n', ' '); string[] imagesUrls = await Task.Run(() => document.QuerySelectorAll(settings.ImagePattern).Select(i => i.Attributes["href"].Value).ToArray()); ImageDownloader imageDownloader = new ImageDownloader(); List <Image> imageList = new List <Image>(); foreach (string imagesUrl in imagesUrls) { Image image = await imageDownloader.DownloadImageAsync(imagesUrl); imageList.Add(image); } if (!String.IsNullOrWhiteSpace(header) && price > 0 && !String.IsNullOrWhiteSpace(url)) { Product product = new Product() { Url = url, Name = header, Description = description, Images = imageList, Prices = new List <Price>() { new Price() { ProductPrice = price, PriceLastDate = DateTime.Now } } }; return(product); } return(new Product()); }
public async void ImageDownloaderLoadImage() { // Arrange ImageDownloader loader = new ImageDownloader(); string url = "https://www.prodj.com.ua/img/img2b/2015/04/20150428170457.jpg"; // Act Image result = await loader.DownloadImageAsync(url); // Assert Assert.NotNull(result); Assert.IsType <Image>(result); Assert.NotEmpty(result.Data); Assert.IsType <Guid>(result.Id); }