protected virtual WebNovelChapter ParseChapter(IElement rootElement, CancellationToken token = default(CancellationToken)) { WebNovelChapter chapter = new WebNovelChapter(); IElement element = rootElement.WhereHasClass(PostClasses).LastOrDefault() ?? rootElement.Descendents<IElement>().FirstOrDefault(p => p.LocalName == "article"); if (element != null) RemoveBloat(element); IElement nextChapterElement = (from e in rootElement?.Descendents<IElement>() ?? new List<IElement>() where e.LocalName == "a" let text = e.Text() let a = NextChapterNames.FirstOrDefault(p => text.IndexOf(p, StringComparison.OrdinalIgnoreCase) >= 0) where a != null orderby NextChapterNames.IndexOf(a) select e).FirstOrDefault(); if (nextChapterElement != null) { chapter.NextChapterUrl = nextChapterElement.GetAttribute("href"); } if (element != null) { chapter.Content = element.InnerHtml; } else { chapter.Content = "No Content"; } return chapter; }
protected virtual WebNovelChapter ParseChapter(IElement rootElement, CancellationToken token = default(CancellationToken)) { IElement articleElement = rootElement.Descendents<IElement>().FirstOrDefault(p => p.LocalName == "article"); IElement element = rootElement.FirstWhereHasClass(PostClasses) ?? articleElement; if (element != null) RemoveBloat(element); IElement chapterNameElement = rootElement.FirstWhereHasClass(TitleClasses); if (element != null && chapterNameElement == null) { chapterNameElement = (from e in element.Descendents<IElement>() where e.LocalName == "h1" || e.LocalName == "h2" || e.LocalName == "h3" || e.LocalName == "h4" select e).FirstOrDefault(); } else { IElement chNameLinkElement = (from e in chapterNameElement.Descendents<IElement>() where e.LocalName == "a" select e).FirstOrDefault(); if (chNameLinkElement != null) chapterNameElement = chNameLinkElement; } IElement nextChapterElement = (from e in articleElement?.Descendents<IElement>() ?? rootElement.Descendents<IElement>() where e.LocalName == "a" let text = e.Text() let a = NextChapterNames.FirstOrDefault(p => text.IndexOf(p, StringComparison.OrdinalIgnoreCase) >= 0) where a != null || (e.HasAttribute("rel") && e.GetAttribute("rel") == "next") let index = NextChapterNames.IndexOf(a) let o = index >= 0 ? index : int.MaxValue orderby o select e).FirstOrDefault(); WebNovelChapter chapter = new WebNovelChapter(); if (nextChapterElement != null) { chapter.NextChapterUrl = nextChapterElement.GetAttribute("href"); } if (element != null) { RemoveNavigation(element); RemoveScriptStyleElements(element); chapter.ChapterName = chapterNameElement?.Text()?.Trim(); chapter.Content = element.InnerHtml; } else { chapter.Content = "No Content"; } return chapter; }