Beispiel #1
0
        public void InsertOrUpdateBookCatalogs(Book book)
        {
            var result = DbLocalBook.InsertOrUpdateBookCatalogs(AppDataPath.GetLocalBookDbPath(), book.CatalogList);

            if (!result)
            {
                ToastHelper.ShowMessage(book.BookName + "下载失败", false);
                ViewModelInstance.Instance.DownloadCenter.RemoveDownItem(this);
                return;
            }
            var value = DbLocalBook.InsertOrUpdatBook(AppDataPath.GetLocalBookDbPath(), book);

            if (value)
            {
                ToastHelper.ShowMessage(book.BookName + "下载成功,您可在本地书架中阅读");
                ViewModelInstance.Instance.DownloadCenter.RemoveDownItem(this);
                ViewModelInstance.Instance.LocalBookPage.InserOrUpdateBook(book);
            }
        }
        private void LoadTxtFile(StorageFile file)
        {
            Task.Run(async() =>
            {
                try
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        IsLoading = true;
                    });

                    var catalogs = new List <BookCatalog>();
                    var bookId   = Guid.NewGuid().ToString();
                    string txt;
                    using (Stream stream = await file.OpenStreamForReadAsync())
                    {
                        using (StreamReader read = new StreamReader(stream, Encoding.GetEncoding("GBK")))
                        {
                            txt = read.ReadToEnd();
                        }
                    }
                    //@"(第?\w*章\s\w{1,20}[\(【(]?\w{1,20}[\)】)]?\n)"
                    var matches1 = Regex.Matches(txt, @"(?<title>第?\w*章\s\w{1,20}[!]?\s?[--,]?\w{1,20}\(?(?\w{1,20}\)?)?)", RegexOptions.Compiled);
                    var matches  = Regex.Matches(txt, @"(第?\w*章\s\w{0,20}[\(【(]?\w{0,20}[\)】)]?)", RegexOptions.Compiled);

                    if (matches.Count <= 0)
                    {
                        ToastHelper.ShowMessage("智能分章失败,将采用按字数分章节");

                        var list = SplitText(txt);

                        if (list == null || list.Count == 0)
                        {
                            ToastHelper.ShowMessage("文件解析失败");
                            return;
                        }

                        for (int i = 0; i < list.Count; i++)
                        {
                            var temp = new BookCatalog
                            {
                                BookId         = bookId,
                                Index          = i,
                                CatalogName    = $"第{i + 1}章",
                                CatalogContent = list[i],
                                CatalogUrl     = (i + 1).ToString()
                            };
                            catalogs.Add(temp);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < matches.Count; i++)
                        {
                            var currentMatch = matches[i];
                            if (currentMatch == null)
                            {
                                continue;;
                            }

                            if (i == 0 && !string.IsNullOrEmpty(txt.Substring(0, currentMatch.Index)?.Trim()))
                            {
                                var temp = new BookCatalog
                                {
                                    BookId         = bookId,
                                    Index          = 0,
                                    CatalogName    = "前言",
                                    CatalogContent = txt.Substring(0, currentMatch.Index),
                                    CatalogUrl     = "0"
                                };
                                catalogs.Add(temp);
                            }

                            var catalog = new BookCatalog
                            {
                                CatalogName = currentMatch.ToString().Replace("\r", "").Replace("\n", ""),
                                BookId      = bookId,
                                Index       = i + 1
                            };
                            if (i == matches.Count - 1)
                            {
                                catalog.CatalogContent = txt.Substring(currentMatch.Index, txt.Length - currentMatch.Index);
                                catalog.CatalogUrl     = currentMatch.Index.ToString();
                                catalog.CatalogUrl     = (i + 1).ToString();
                            }
                            else
                            {
                                var nextMatch          = matches[i + 1];
                                catalog.CatalogContent = txt.Substring(currentMatch.Index, nextMatch.Index - currentMatch.Index);
                                catalog.CatalogUrl     = (i + 1).ToString();
                            }
                            catalog.CatalogContent = catalog.CatalogContent?.Replace("\r\n\r\n", "\r\n");

                            catalogs.Add(catalog);
                        }
                    }


                    if (catalogs.Count == 0)
                    {
                        ToastHelper.ShowMessage("文件解析失败");
                        return;
                    }

                    var book                 = new Book();
                    book.IsTxt               = true;
                    book.BookId              = bookId;
                    book.BookName            = file.DisplayName;
                    book.LastReadChapterName = catalogs.FirstOrDefault().CatalogName;
                    book.LastReadChapterUrl  = catalogs.FirstOrDefault().CatalogUrl;
                    book.NewestChapterName   = catalogs.LastOrDefault().CatalogName;
                    book.NewestChapterUrl    = catalogs.LastOrDefault().CatalogUrl;
                    book.AuthorName          = "某位大神";
                    book.Description         = "不管三七二十一,就是好看。";
                    book.LyWeb               = "本地TXT文档";

                    DbLocalBook.InsertOrUpdateBookCatalogs(AppDataPath.GetLocalBookDbPath(), catalogs);

                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        this.InserOrUpdateBook(book);
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    ToastHelper.ShowMessage("文件解析失败");
                }
                finally
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        IsLoading = false;
                    });
                }
            });
        }
        public async void StartUpdate()
        {
            try
            {
                var taskCount  = 3;
                var groups     = NeedUpdateCatalogs.Split <BookCatalog>(taskCount);
                var enumerable = groups as IEnumerable <BookCatalog>[] ?? groups.ToArray();
                if (groups == null || !enumerable.Any())
                {
                    return;
                }
                IsUpdating = true;

                var tasks = new Task[enumerable.Length];
                for (var i = 0; i < enumerable.Length; i++)
                {
                    if (IsDeleted)
                    {
                        break;
                    }
                    tasks[i] = await Task.Factory.StartNew(async() =>
                    {
                        var catalogs = enumerable[i];
                        foreach (var bookCatalog in catalogs)
                        {
                            try
                            {
                                if (IsDeleted)
                                {
                                    break;
                                }

                                bookCatalog.CatalogContent =
                                    await AnalisysSourceHelper.GetCatalogContent(bookCatalog.CatalogUrl);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                            finally
                            {
                                lock (_obj)
                                {
                                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                    {
                                        NeedUpdateCount = (int.Parse(NeedUpdateCount) - 1).ToString();
                                    });
                                }
                            }
                        }
                    });
                }

                await Task.Factory.ContinueWhenAll(tasks, (obj) =>
                {
                    DbHelper.AddDbOperator(new Action(() =>
                    {
                        var reslut = DbLocalBook.InsertOrUpdateBookCatalogs(AppDataPath.GetLocalBookDbPath(),
                                                                            NeedUpdateCatalogs);
                    }));

                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        NeedUpdateCount = "";
                    });
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                IsUpdating = false;
            }
        }