public override async Task <MemeInfo> RandomAsync() { WebClient wc; string html; try { wc = new WebClient(); html = await wc.DownloadStringTaskAsync(_uris[UriType.Random]); } catch (WebException ex) { throw new ServiceOrConnectionException("Could not load the page", ex); } IConfiguration config = Configuration.Default; IBrowsingContext context = BrowsingContext.New(config); IDocument document = await context.OpenAsync(req => req.Content(html).Address(_baseUrl)); IElement picDiv = document.DocumentElement.QuerySelector(".media-element-wrapper"); IHtmlImageElement img = (IHtmlImageElement)picDiv.QuerySelector(".figure-holder figure img"); IHtmlHeadingElement h = (IHtmlHeadingElement)picDiv.QuerySelector(".content h1"); IElement player = picDiv.QuerySelector(".figure-holder figure player"); if ((player == null && img == null) || h == null) { throw new NotFoundException( "Either \"img\", \"player\" or \"h\" tag could not be found"); } // Unfortunately on Kwejk random image page there's no link to the // original image page, so ViewURI = URI MemeInfo meme; if (player != null) { meme = new MemeInfo { ViewURI = player.GetAttribute("source"), URI = player.GetAttribute("source"), Alt = string.Empty, Name = h.TextContent.Trim(), Type = MediaType.Video }; } else { meme = new MemeInfo { ViewURI = img.Source, URI = img.Source, Alt = img.AlternativeText, Name = h.TextContent.Trim(), Type = MediaType.Image }; } return(meme); }
private TocEntry GetTocEntry(IHtmlHeadingElement heading) { return(new TocEntry() { Title = heading.TextContent, HeadingId = heading.Id }); }
public override async Task <MemeInfo> RandomAsync() { WebClientPlus wc; string html; try { wc = new WebClientPlus(); html = await wc.DownloadStringTaskAsync(_uris[UriType.Random]); } catch (WebException ex) { throw new ServiceOrConnectionException("Could not load the page", ex); } IConfiguration config = Configuration.Default; IBrowsingContext context = BrowsingContext.New(config); IDocument document = await context.OpenAsync(req => req.Content(html).Address(_baseUrl)); IElement picDiv = document.DocumentElement.QuerySelector("#main_container .pic"); IHtmlImageElement img = (IHtmlImageElement)picDiv.QuerySelector(".pic_image img"); IHtmlHeadingElement h = (IHtmlHeadingElement)picDiv.QuerySelector("h1.picture"); if (img == null || h == null) { throw new NotFoundException("Either \"img\" or \"h1\" tag could not be found"); } return(new MemeInfo { ViewURI = wc.ResponseURI.ToString(), URI = img.Source, Alt = img.AlternativeText, Name = h.TextContent }); }
/// <summary> /// Заполнение данных о товаре с сайта поставщика /// </summary> public async Task <bool> FillByUrl() { bool res = false; if (await Loader.LoadPageAsync(Url)) { // Получение наименования товара IHtmlHeadingElement pageHeader = (IHtmlHeadingElement)Loader.Document.QuerySelectorAll("h1") .Where(item => item.TextContent.Trim().Length > 0) .FirstOrDefault(); if (pageHeader != null) { Name = pageHeader.TextContent.Trim(); } // Получение цены товара IHtmlSpanElement mainPrice = (IHtmlSpanElement)Loader.Document.QuerySelectorAll("span") .Where(item => item.Id != null && item.Id.Trim() == @"main-price") .FirstOrDefault(); if (mainPrice != null) { int mainPriceValue = 0; if (int.TryParse(mainPrice.TextContent.Trim().Replace(" ", string.Empty), out mainPriceValue)) { Price = mainPriceValue; } } // Получение старой цены товара IHtmlDivElement oldPriceElement = (IHtmlDivElement)Loader.Document.QuerySelectorAll("div") .Where(item => item.ClassName != null && item.ClassName.Contains(@"old-price")) .FirstOrDefault(); if (oldPriceElement != null) { Regex rgOldPrice = new Regex(@"^(?<value>\d+)"); Match mtOldPrice = rgOldPrice.Match(oldPriceElement.TextContent.Trim().Replace(" ", string.Empty)); if (mtOldPrice.Success) { int oldPriceValue = 0; if (int.TryParse(mtOldPrice.Groups["value"].ToString(), out oldPriceValue)) { OldPrice = oldPriceValue; } } } // Загрузка списка технических характеристик IHtmlElement techSpecSection = (IHtmlElement)Loader.Document.QuerySelectorAll("section") .Where(item => item.ClassName != null && item.ClassName.Contains(@"tech-specs")) .FirstOrDefault(); if (techSpecSection != null) { var techSpecRowsCollection = techSpecSection.QuerySelectorAll("div") .Where(item => item.ClassName != null && item.ClassName.Contains(@"row")); foreach (IHtmlDivElement techSpecRow in techSpecRowsCollection) { IHtmlSpanElement techSpecName = (IHtmlSpanElement)techSpecRow.QuerySelectorAll("span") .FirstOrDefault(); IHtmlParagraphElement techSpecValue = (IHtmlParagraphElement)techSpecRow.QuerySelectorAll("p") .Where(item => item.ClassName != null && item.ClassName.Contains(@"p-style2")) .FirstOrDefault(); if (techSpecName != null && techSpecValue != null && techSpecName.TextContent.Trim().Length > 0 && techSpecValue.TextContent.Trim().Length > 0) { Options.Add(new InventItemOption(techSpecName.TextContent.Trim(), techSpecValue.TextContent.Trim())); } } } // Загрузка описания товара IHtmlDivElement descrElement = (IHtmlDivElement)Loader.Document.QuerySelectorAll("div") .Where(item => item.Id != null && item.Id.Trim() == @"descr") .FirstOrDefault(); if (descrElement != null) { Descr = descrElement.TextContent.Trim(); } // Формирование списка изображений товара IHtmlUnorderedListElement imagesListElement = (IHtmlUnorderedListElement)Loader.Document.QuerySelectorAll("ul") .Where(item => item.ClassName != null && item.ClassName.Contains(@"pagination")) .FirstOrDefault(); if (imagesListElement != null) { var imagesListCallection = imagesListElement.QuerySelectorAll("img"); foreach (IHtmlImageElement imageItem in imagesListCallection) { if (imageItem.Source != null && imageItem.Source.Length > 0) { Images.Add(Site.PrepareUrl(imageItem.Source)); } } } res = true; } RegisterInventItem(); return(res); }