Beispiel #1
0
        //public static Volume GetVolume(string id);
        public async static Task <Chapter> GetChapterAsync(string id)
        {
            var chapter = new Chapter();

            chapter.Id = id;
            string novelUrl = ChapterSource + id + ".html";

            using (var client = NewUserHttpClient(UserAgentType.IE11))
            {
                var parsing = client.GetStreamAsync(novelUrl).ContinueWith((stream_task) =>
                {
                    var doc = new HtmlDocument();
                    if (stream_task.IsCompleted && !stream_task.IsFaulted)
                    {
                        doc.Load(stream_task.Result);
                    }
                    else
                    {
                        throw stream_task.Exception;
                    }

                    var nodes = doc.DocumentNode.Descendants();

                    // Naviagtion Proporties
                    {
                        var pathNodes =
                            nodes.First(
                                node =>
                                node.Name == "ul" &&
                                node.Attributes["class"] != null &&
                                node.Attributes["class"].Value.StartsWith("breadcrumb")).Elements("li")
                            .Select(node => node.Element("a"));


                        chapter.ParentSeriesId = RetriveId(pathNodes.First(
                                                               node => node.Attributes["href"].Value.StartsWith("http://lknovel.lightnovel.cn/main/vollist/"))
                                                           .Attributes["href"].Value);

                        var navi = nodes.First(
                            node => node.Attributes["class"] != null && node.Attributes["class"].Value.StartsWith("lk-view-navi"));
                        var naviNodes = navi.Descendants("a");

                        var prev = naviNodes.FirstOrDefault(node => node.InnerText.Contains("上一章"));
                        if (prev != null)
                        {
                            chapter.PrevChapterId = RetriveId(prev.Attributes["href"].Value);
                        }
                        var content = naviNodes.First(node => node.InnerText.Contains("目录"));
                        if (content != null)
                        {
                            chapter.ParentVolumeId = RetriveId(content.Attributes["href"].Value);
                        }
                        var next = naviNodes.FirstOrDefault(node => node.InnerText.Contains("下一章"));
                        if (next != null)
                        {
                            chapter.NextChapterId = RetriveId(next.Attributes["href"].Value);
                        }
                    }

                    var chapterTitleNode = nodes.First(node => node.Name == "h3");
                    if (chapterTitleNode != null)
                    {
                        chapter.Title = RemoveLabel(chapterTitleNode.InnerText);
                    }

                    var lines = from line in nodes
                                where line.Name == "div" &&
                                line.Attributes["class"] != null &&
                                line.Attributes["class"].Value.StartsWith("lk-view-line")
                                select line;

                    chapter.Lines = lines.Select <HtmlNode, Line>(node =>
                    {
                        Line line = new Line(int.Parse(node.Id), LineContentType.TextContent, null);
                        if (!node.HasChildNodes)
                        {
                            line.Content = String.Empty;
                            return(line);
                        }
                        if (node.ChildNodes.Any(elem => elem.Name == "div"))
                        {
                            line.ContentType = LineContentType.ImageContent;
                            var img_url      = (from elem in node.Descendants()
                                                where elem.Name == "img" &&
                                                elem.Attributes["data-cover"] != null
                                                select elem.Attributes["data-cover"].Value).FirstOrDefault();
                            if (img_url != null)
                            {
                                line.Content = AbsoluteUrl(img_url);
                            }
                        }
                        else
                        {
                            var text     = node.InnerText;
                            line.Content = WebUtility.HtmlDecode(text.Trim());
                        }
                        return(line);
                    }).ToList();
                });
                await parsing;
                if (parsing.Exception != null)
                {
                    throw parsing.Exception;
                }
            }
            return(chapter);
        }