private async Task _savePageAsync() { var book = await BookHelper.OpenAsync(WebBrowser.Source); if (book != null) { Toast.ShowInfo($"下载成功!共下载了 {book.Count} 章"); return; } book = new Book() { Name = WebBrowser.DocumentTitle, IsLocal = false, Count = 1, Url = WebBrowser.Source.AbsoluteUri }; var chapter = new BookChapter() { Name = book.Name, Url = WebBrowser.Source.ToString(), }; chapter.Content = BookHelper.HtmlToText(await GetBody()); SqlHelper.Conn.Open(); book.Save(); chapter.BookId = book.Id; chapter.Save(); SqlHelper.Conn.Close(); Toast.ShowInfo("页面保存成功!"); }
/// <summary> /// 直接保存不返回 /// </summary> /// <param name="lines"></param> /// <param name="book"></param> public static void GetBookChapter(IList <string> lines, Book book) { var index = _getChapterTitle(lines); int start; var position = 1; BookChapter chapter; while (index >= 0) { start = index; index = _getChapterTitle(index + 1, lines); chapter = new BookChapter() { Name = lines[start], Position = position, Content = _getChapterContent(lines, start, index - 1), BookId = book.Id }; chapter.Save(); position++; } book.Count = position - 1; book.Save(); }
/// <summary> /// 直接保存不返回 /// </summary> /// <param name="file"></param> /// <param name="book"></param> /// <returns></returns> public static async Task GetBookChapterAsync(StorageFile file, Book book) { var lines = new List <string>(); // 已获取的行 string line; // 当前行 var maxScore = 0; // 最高分数 var maxLine = -1; //最高分数对应的行号 var maxCount = 0; var count = 0; // 当前字数 var lineLength = 0; // 当前行的长度 var index = -1; // 当前行在数组中的序号 var score = 0; // 当前行分数 var lastMaxLine = -1; var position = 1; // 章节总数 BookChapter chapter; // 章节 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); using (var inputStream = await file.OpenReadAsync()) using (var classicStream = inputStream.AsStreamForRead()) using (var streamReader = new StreamReader(classicStream, StorageHelper.GetEncoding(classicStream, Encoding.GetEncoding("gbk")))) { while (streamReader.Peek() >= 0) { line = streamReader.ReadLine(); lines.Add(line); index++; if (string.IsNullOrWhiteSpace(line)) { score += 50; continue; } if (lastMaxLine < 0) { lastMaxLine = index; score = 0; continue; } lineLength = line.Length; count += lineLength; if (lineLength > 50) { score = 0; continue; } if (Pattern.IsMatch(line)) { // 正则匹配 var match = Pattern.Match(line); var a = string.IsNullOrEmpty(match.Groups[2].Value); var b = string.IsNullOrEmpty(match.Groups[3].Value); if (!a && !b) { // 保存当前已搜索的章节 chapter = new BookChapter() { Name = lines[lastMaxLine], Position = position, Content = _getChapterContent(lines, 0, index - 1), BookId = book.Id }; chapter.Save(); position++; lines.RemoveRange(0, index); // 初始化 lastMaxLine = 0; index = 0; maxScore = 0; count = 0; score = 0; continue; } if (count < 500) { score = 0; continue; } if (!b) { maxScore = score + 100; maxLine = index; maxCount = count; } else if (!a) { maxScore = score + 50; maxLine = index; maxCount = count; } else if (lineLength < 10) { maxScore = score + 100; maxLine = index; maxCount = count; } score = 0; continue; } if (count < 500) { score = 0; continue; } score += (300 - Math.Abs(4000 - count) / 100 + 50 - Math.Abs(10 - lineLength) * 5); if (line.IndexOf("“") >= 0 || line.IndexOf("‘") >= 0 || line.IndexOf(":") >= 0) { score -= 20; } if (score > maxScore) { maxScore = score; maxLine = index; score = 0; maxCount = count; } //Log.Info($"i: {index} l: {lineLength} c:{count} s: {score}"); if (count >= 8000) { // 保存当前已搜索的章节 存在问题,下一章过长 chapter = new BookChapter() { Name = lines[lastMaxLine], Position = position, Content = _getChapterContent(lines, 0, maxLine - 1), BookId = book.Id }; chapter.Save(); position++; lines.RemoveRange(0, maxLine); // 初始化 lastMaxLine = 0; index -= maxLine; maxScore = 0; count -= maxCount; continue; } score = 0; } } // 最后保存 chapter = new BookChapter() { Name = lines[lastMaxLine], Position = position, Content = _getChapterContent(lines, 0, lines.Count - 1), BookId = book.Id }; chapter.Save(); book.Count = position; book.Save(); // 原始方法 //var content = await StorageHelper.GetFileTextAsync(file); //GetBookChapter(GetLines(content), book); }