public async void Open(BookDataBrief book) { using (var bs = BookShelf.Create()) { var dc = SearchResult[book]; var existBook = await bs.Books.FirstOrDefaultAsync(b => b.Title == book.Title && b.Author == book.Author && b.PackageFamilyName == dc.PackageFamilyName && b.ExtensionId == dc.ExtensionId); if (existBook is null) { existBook = new Book(book, dc); bs.Books.Add(existBook); } else { existBook.Update(book, dc); } await bs.SaveChangesAsync(); await Navigator.GetForCurrentView().NavigateAsync(typeof(BookPage), existBook.Id.ToString()); } }
public Book(BookDataBrief book, DataSource dataSource) { Title = book.Title; Author = book.Author; ExtensionId = dataSource.ExtensionId; PackageFamilyName = dataSource.PackageFamilyName; ChaptersData = new ObservableList <Chapter>(); Update(book, dataSource); }
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); }
private void parseBookPage(BookDataBrief book, HtmlDocument document) { book.Description = string.Join("\n", document.GetElementbyId("intro_win") .SelectNodes("./div[1]/p").EmptyIfNull() .Select(p => p.GetInnerText())); var booinfo = document.GetElementbyId("bookinfo"); book.WordCount = int.Parse(booinfo.SelectSingleNode("./div[2]/div[2]/ul[1]/li[11]/span[1]").GetInnerText()); book.Tags = booinfo.SelectNodes("./div[2]/div[4]/span[1]/a").EmptyIfNull().Select(n => n.GetInnerText()).ToArray(); }
private void parseBookPage(BookDataBrief book, HtmlDocument document) { var info = document.DocumentNode.SelectSingleNode("./html/body/div[@class='wrap']/div[@class='mainarea']/div[@class='bortable']/div[@class='work']/div[@class='wright']"); book.Description = info.SelectSingleNode("./p[@class='Text']").GetInnerText(); var booinfo = document.GetElementbyId("bookinfo"); book.WordCount = int.Parse(info.SelectSingleNode("./div[@id='box4']/p/small[1]").GetInnerText()); var bt1 = document.GetElementbyId("bt_1"); book.Key = bookIdReg.Match(bt1.Element("a").GetAttributeValue("href", "")).Groups[2].Value; }
public void Update(BookDataBrief book, DataSource dataSource) { Debug.Assert(!(book is Book)); Debug.Assert(book.Title == Title); Debug.Assert(book.Author == Author); Debug.Assert(dataSource.ExtensionId == ExtensionId); Debug.Assert(dataSource.PackageFamilyName == PackageFamilyName); LatestChapter = book.LatestChapter; Tags = book.Tags; AlternativeTitle = book.AlternativeTitle.CoalesceNullOrWhiteSpace(AlternativeTitle); if (Description.CoalesceNullOrEmpty("").Length <= book.Description.CoalesceNullOrWhiteSpace("").Length) { Description = book.Description; } if (!book.CoverData.IsNullOrEmpty()) { CoverData = book.CoverData; } if (book.CoverUri != null) { CoverUri = book.CoverUri; } IsFinished = book.IsFinished; if (book.WordCount >= 0) { WordCount = book.WordCount; } Key = book.Key ?? Key; if (book is BookDataDetailed detailed && !detailed.Chapters.IsNullOrEmpty() && ChaptersData != null) { Description = book.Description.CoalesceNullOrWhiteSpace(Description); var newChpData = new List <Chapter>(); var i = 0; foreach (var item in detailed.Chapters) { i++; newChpData.Add(new Chapter(this, i /* 1-based index */, item)); } ChaptersData.Update(newChpData, (o, n) => o.ChapterId.CompareTo(n.ChapterId), (existChp, newChp) => existChp.Update(newChp)); } }
private void parseBookMeta(BookDataBrief book, HtmlDocument document) { var head = document.DocumentNode.SelectSingleNode("/html/head"); var uri = head.SelectSingleNode("./meta[@property='og:novel:read_url']").GetAttribute("content", ""); book.Key = idReg.Match(uri).Groups[1].Value; book.Title = head.SelectSingleNode("./meta[@property='og:novel:book_name']").GetAttribute("content", ""); book.Author = head.SelectSingleNode("./meta[@property='og:novel:author']").GetAttribute("content", ""); book.CoverUri = head.SelectSingleNode("./meta[@property='og:image']").GetAttribute("content", BaseUri, null); book.IsFinished = head.SelectSingleNode("./meta[@property='og:novel:status']").GetAttribute("content", "").Contains("完结"); book.LatestChapter = new ChapterDataBrief { UpdateTime = DateTime.Parse(head.SelectSingleNode("./meta[@property='og:novel:update_time']").GetAttribute("content", "")), Key = idReg.Match(head.SelectSingleNode("./meta[@property='og:novel:latest_chapter_url']").GetAttribute("content", "")).Groups[1].Value, Title = head.SelectSingleNode("./meta[@property='og:novel:latest_chapter_name']").GetAttribute("content", ""), }; }