Ejemplo n.º 1
0
        protected override async Task <SearchResponse> SearchAsync(SearchRequest request)
        {
            var content = new HttpBufferContent(GBEncoding.GetBytes($"s={request.Keyword}").AsBuffer())
            {
                Headers =
                {
                    ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/x-www-form-urlencoded")
                }
            };
            var doc = await PostDoc(searchUri, content);

            var r   = new SearchResponse();
            var box = doc.GetElementbyId("sitembox");

            if (box is null)
            {
                var bf = new BookDataBrief();
                parseBookMeta(bf, doc);
                parseBookPage(bf, doc);
                r.Books.Add(bf);
            }
            else
            {
                foreach (var item in box.Elements("dl"))
                {
                    var title = item.SelectSingleNode("./dd[1]/h3/a");
                    var key   = title.GetAttributeValue("href", "");
                    key = idReg.Match(key).Groups[1].Value;
                    var author     = item.SelectSingleNode("./dd[2]/span[1]");
                    var status     = item.SelectSingleNode("./dd[2]/span[2]");
                    var wordCount  = item.SelectSingleNode("./dd[2]/span[4]");
                    var tags       = item.SelectNodes("./dd[2]/a").EmptyIfNull();
                    var des        = item.SelectSingleNode("./dd[3]");
                    var latestName = item.SelectSingleNode("./dd[4]/a");
                    var latestTime = item.SelectSingleNode("./dd[4]/span");
                    var image      = item.SelectSingleNode("./dt[1]/a/img");
                    r.Books.Add(new BookDataBrief
                    {
                        Title         = title.GetInnerText(),
                        Author        = author.GetInnerText(),
                        Key           = key,
                        CoverUri      = image?.GetAttribute("_src", BaseUri, null),
                        Description   = des.GetInnerText(),
                        WordCount     = int.Parse(wordCount.GetInnerText()),
                        Tags          = tags.Select(n => n.GetInnerText()).ToArray(),
                        IsFinished    = status.GetInnerText().Contains("完结"),
                        LatestChapter = new ChapterDataBrief
                        {
                            UpdateTime = DateTime.Parse(latestTime.GetInnerText()),
                            Key        = latestName.GetAttribute("href", ""),
                            Title      = latestName.GetInnerText(),
                        }
                    });
                }
            }
            return(r);
        }
Ejemplo n.º 2
0
        protected override async Task <SearchResponse> SearchAsync(SearchRequest request)
        {
            var sb = new StringBuilder("https://www.2kxs.com/modules/article/search.php?searchtype=keywords&searchkey=");

            foreach (var item in GBEncoding.GetBytes(request.Keyword))
            {
                sb.Append('%');
                sb.Append(item.ToString("X2"));
            }
            var uri = new Uri(sb.ToString());
            var doc = await GetDoc(uri);

            var r       = new SearchResponse();
            var content = doc.GetElementbyId("content");

            if (content is null)
            {
                var bf = new BookDataDetailed();
                parseBookPage(bf, doc);
                var readPage = await GetDoc(new Uri($"/xiaoshuo/{bf.Key}/", UriKind.Relative));

                parseBookMeta(bf, readPage);
                parseReadPage(bf, readPage);
                r.Books.Add(bf);
            }
            else
            {
                foreach (var item in content.SelectNodes("table/tr[position()>1]"))
                {
                    var title     = item.SelectSingleNode("./td[1]/a");
                    var latestChp = item.SelectSingleNode("./td[2]/a");
                    var author    = item.SelectSingleNode("./td[3]");
                    var wordCount = item.SelectSingleNode("./td[4]").GetInnerText();
                    var wc        = int.Parse(wordCount.Substring(0, wordCount.Length - 1));
                    if (char.IsDigit(wordCount.Last()))
                    {
                        wc *= 10;
                    }
                    else if (char.ToLowerInvariant(wordCount.Last()) == 'k')
                    {
                        wc *= 1000;
                    }
                    else if (char.ToLowerInvariant(wordCount.Last()) == 'm')
                    {
                        wc *= 1000_000;
                    }
                    var lastUpdate = item.SelectSingleNode("./td[5]").GetInnerText();
                    var status     = item.SelectSingleNode("./td[6]").GetInnerText();

                    var key = latestChp.GetAttributeValue("href", "");
                    key = bookIdReg.Match(key).Groups[2].Value;

                    r.Books.Add(new BookDataBrief
                    {
                        Title         = title.GetInnerText(),
                        Author        = author.GetInnerText(),
                        Key           = key,
                        WordCount     = wc,
                        IsFinished    = status == "完成",
                        LatestChapter = new ChapterDataBrief
                        {
                            Key        = "Latest",
                            UpdateTime = DateTime.Parse(lastUpdate),
                            Title      = latestChp.GetInnerText(),
                        }
                    });
                }
            }
            return(r);
        }