public static Image PublicDomainPoetryRndmToImage()
        {
            //get random page
            Random rnd  = new Random();
            int    page = rnd.Next(1, 500);
            string url  = "http://www.public-domain-poetry.com/listpoetry.php?letter=All&page=" + page.ToString();

            var doc = PoetryBot.LinkToHtmlDoc(url);

            HtmlNodeCollection poemLinks = doc.DocumentNode.SelectNodes("//a"); // get all links
            List <string>      strPoems  = new List <string>();

            foreach (var link in poemLinks)
            {
                var href = link.Attributes["href"].Value;
                if (href.ToString().Contains("php") == false &&
                    href.ToString().Contains("http") == false &&
                    href.ToString().Substring(href.Length - 1) != @"/" &&
                    href.ToString().Any(char.IsDigit))
                {
                    strPoems.Add(href); // get just hrefs
                }
            }

            //get random poem
            int rndPoem = rnd.Next(1, strPoems.Count);

            url = "http://www.public-domain-poetry.com/" + strPoems[rndPoem];
            doc = PoetryBot.LinkToHtmlDoc(url);

            HtmlNode title = doc.DocumentNode.SelectSingleNode("//font[@class='t0']");
            HtmlNode poem  = doc.DocumentNode.SelectSingleNode("//font[@class='t3a']");

            string justTitle  = title.InnerText.Substring(0, title.InnerText.LastIndexOf(" by "));
            string justAuthor = title.InnerText.Substring(title.InnerText.LastIndexOf(" by ") + 1);

            string htmlConcat = @"<div style=""text-indent: -1em; padding-left: 1em;""> <b>" +
                                justTitle.Replace("Public Domain Poetry - ", "").ToUpper() + @"</b> " +
                                justAuthor.ToUpper() + @"<br><br>" +
                                poem.OuterHtml + @"</div>";

            Image image = TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderToImage(htmlConcat);

            return(image);
        }
示例#2
0
        public static Image PoetsorgToImage()
        {
            //get random page
            Random rnd     = new Random();
            int    rndPage = rnd.Next(1, 698);
            string url     = "https://www.poets.org/poetsorg/poems?field_occasion_tid=All&field_poem_themes_tid=All&field_form_tid=All&page=" + rndPage.ToString();
            var    doc     = LinkToHtmlDoc(url, true);

            HtmlNodeCollection poemLinks = doc.DocumentNode.SelectNodes("//a"); // get all links
            List <string>      strPoems  = new List <string>();

            foreach (var link in poemLinks)
            {
                if (link.OuterHtml.Contains("href"))
                {
                    var href = link.Attributes["href"].Value;
                    if (href.ToString().Contains(@"poetsorg/poem/"))
                    {
                        strPoems.Add(href); // get just hrefs
                    }
                }
            }

            //get random poem
            int rndPoem = rnd.Next(1, strPoems.Count);

            url = "https://www.poets.org" + strPoems[rndPoem]; // base link needed
            doc = PoetryBot.LinkToHtmlDoc(url, true);

            HtmlNode poemDiv    = doc.DocumentNode.SelectSingleNode("//div[@id='poem-content'] //div[@class='field-item even']");
            HtmlNode authSpan   = doc.DocumentNode.SelectSingleNode("//div[@id='poem-content'] //span[@class='node-title']");
            HtmlNode titleSpan  = doc.DocumentNode.SelectSingleNode("//div[@id='poem-content'] //h1[@id='page-title']");
            string   htmlConcat =
                @"<div style=""text-indent: -1em; padding-left: 1em;""> <b>" + titleSpan.InnerText.ToString().ToUpper() + @"</b>"
                + " BY " + authSpan.InnerText.ToString().ToUpper() + @"<br>"
                + poemDiv.OuterHtml.ToString() + @"</div>";

            Image image = TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderToImage(htmlConcat);

            //Image image = null;
            return(image);
        }