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 .QuerySelectorAll("#wrapper-wrap .left .ob-left-box-images")[RandomNthChild()]; IHtmlImageElement img = (IHtmlImageElement)picDiv .QuerySelector(".left-wrap a img:last-child"); IHtmlAnchorElement a = (IHtmlAnchorElement)picDiv .QuerySelector("h2 a"); IHtmlInputElement input = (IHtmlInputElement)picDiv .QuerySelector(".left-wrap input[type=\"hidden\"]"); IHtmlSourceElement src = (IHtmlSourceElement)picDiv .QuerySelector(".left-wrap video > source"); if ((src == null && img == null) || a == null) { throw new NotFoundException( "Either \"img\", \"source\" or \"a\" tag could not be found"); } MemeInfo meme; if (src != null) { meme = new MemeInfo { ViewURI = a.Href, URI = src.Source, Alt = string.Empty, Name = a.TextContent, Type = MediaType.Video }; } else if (input != null) { meme = new MemeInfo { ViewURI = a.Href, URI = input.Value, Alt = img.AlternativeText, Name = a.TextContent, Type = MediaType.Gif }; } else { meme = new MemeInfo { ViewURI = a.Href, URI = img.Source, Alt = img.AlternativeText, Name = a.TextContent, Type = MediaType.Image }; } return(meme); }
public override async Task <MemeInfo> RandomAsync() { PseudoRandomImage image = GetPseudoRandomImage(); WebClient wc; string html; try { wc = new WebClient(); html = await wc.DownloadStringTaskAsync(image.Site); } 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.QuerySelectorAll( "#content-container article .article-content")[image.NthChild]; IHtmlImageElement img = (IHtmlImageElement)picDiv .QuerySelector(".article-image img"); IHtmlAnchorElement a = (IHtmlAnchorElement)picDiv .QuerySelector(".article-title a"); IHtmlSourceElement src = (IHtmlSourceElement)picDiv .QuerySelector(".article-image video > source"); if ((img == null && src == null) || a == null) { throw new NotFoundException( "Either \"img\", \"source\" or \"a\" tag could not be found"); } MemeInfo meme; if (src != null) { meme = new MemeInfo { ViewURI = a.Href, URI = src.Source, Alt = string.Empty, Name = a.TextContent.Trim(), Type = MediaType.Video }; } else { meme = new MemeInfo { ViewURI = a.Href, URI = img.Source, Alt = img.AlternativeText, Name = a.TextContent.Trim(), Type = MediaType.Image }; } return(meme); }