Example #1
0
        /// <summary>
        /// 從正文網頁內取得章節標題和內文
        /// </summary>
        public async Task <Chapter> GetChapterContent(ChapterLink chapterLink, CodePage codePage)
        {
            if (chapterLink == null || string.IsNullOrEmpty(chapterLink.Url))
            {
                return(null);
            }

            Chapter chapterGot;

            if (IgnoreDownloaded && !string.IsNullOrEmpty(chapterLink.Content))
            {
                chapterGot = new Chapter(chapterLink.Title, chapterLink.Content);
            }
            else
            {
                string htmlString = await GetPageBodyAsync(chapterLink.Url, codePage);

                HtmlDocument bookDocument = new HtmlDocument();
                bookDocument.LoadHtml(htmlString);

                chapterGot          = GetChapterContent(bookDocument, chapterLink.Url);
                chapterLink.Content = chapterGot.Content;
                if (!string.IsNullOrEmpty(chapterGot.Content))
                {
                    chapterLink.Downloaded = true;  // mark as downloaded
                }
            }
            return(chapterGot);
        }
Example #2
0
        protected override BookLink GetChapterLinks(HtmlDocument htmlDoc, string baseUrl)
        {
            var titleNode  = htmlDoc.DocumentNode.SelectSingleNode("//div[@id='title']");
            var authorNode = htmlDoc.DocumentNode.SelectSingleNode("//div[@id='info']");

            BookLink bookLink = new BookLink
            {
                IndexPage = baseUrl,
                Title     = Utilities.ToTraditional(titleNode?.InnerText),
                Author    = authorNode?.InnerText
            };

            baseUrl = Utilities.TrimEnd(baseUrl.ToLower(), "index.htm");

            List <HtmlNode> tableRows = new List <HtmlNode>();
            var             tables    = htmlDoc.DocumentNode.SelectNodes("//table[@class='css']");

            foreach (var tableNode in tables)
            {
                tableRows.AddRange(tableNode.Descendants("tr"));
            }

            List <IssueLink> issueLinks = new List <IssueLink>();

            foreach (var node in tableRows)
            {
                var titleRow = node.SelectSingleNode(".//td[@class='vcss']");
                if (titleRow != null)
                {
                    IssueLink b = new IssueLink
                    {
                        Title = Utilities.ToTraditional(titleRow.InnerText)
                    };
                    issueLinks.Add(b);
                    continue;
                }

                // 一個章節的 tr 裡面會有一個以上的 ccss 文章超連結
                var chapterRows = node.SelectNodes(".//td[@class='ccss']");
                foreach (var chapterRow in chapterRows)
                {
                    var link = chapterRow.Descendants("a").FirstOrDefault();
                    if (link != null)
                    {
                        ChapterLink chapterLink = new ChapterLink();
                        chapterLink.Title = Utilities.ToTraditional(link.InnerText);
                        chapterLink.Url   = baseUrl + link.Attributes["href"]?.Value;
                        issueLinks.Last()?.ChapterLinks.Add(chapterLink);
                    }
                }
            }

            bookLink.IssueLinks = issueLinks;
            return(bookLink);
        }