예제 #1
0
 private void ExecuteUpdateCommand()
 {
     if (_book.Source == BookSources.本地)
     {
         return;
     }
     RingVisibility = Visibility.Visible;
     Task.Factory.StartNew(() =>
     {
         DatabaseHelper.Open();
         var rule    = DatabaseHelper.GetRule(_book.Url);
         var chapter = ChaptersList[_book.Index];
         var html    = new HtmlExpand();
         html.SetUrl(chapter.Url);
         html.Narrow(rule.ChapterBegin, rule.ChapterEnd);
         var content = html.GetText(rule.Replace);
         DatabaseHelper.Update <ChapterItem>(
             "Content = @content",
             $"Id = {chapter.Id}",
             new SQLiteParameter("@content", content));
         DatabaseHelper.Close();
         Application.Current.Dispatcher.Invoke(() =>
         {
             _setConent(content);
         });
         RingVisibility = Visibility.Collapsed;
     });
 }
예제 #2
0
 private void ExecuteUpdateCommand(int index)
 {
     if (index < 0 || index >= BooksList.Count || BooksList[index].Source == BookSources.本地)
     {
         return;
     }
     RingVisibility = Visibility.Visible;
     Task.Factory.StartNew(() =>
     {
         var item     = BooksList[index];
         var conn     = DatabaseHelper.Open();
         var rule     = DatabaseHelper.GetRule(item.Url);
         var chapters = Helper.HttpHelper.GetChapters(item, rule, (HtmlExpand) new HtmlExpand().SetUrl(item.Url));
         var length   = chapters.Count;
         if (item.Count < length)
         {
             var result = Parallel.For((long)item.Count, length, i =>
             {
                 var chapter = chapters[Convert.ToInt32(i)];
                 var html    = new HtmlExpand();
                 html.SetUrl(chapter.Url);
                 LocalHelper.WriteTemp(
                     ((HtmlExpand)html.Narrow(rule.ChapterBegin, rule.ChapterEnd)).GetText(rule.Replace), chapter.Content);
             });
             while (!result.IsCompleted)
             {
                 Thread.Sleep(1000);
             }
             DbTransaction trans = conn.BeginTransaction();
             try
             {
                 for (var i = item.Count; i < length; i++)
                 {
                     var chapter = chapters[i];
                     DatabaseHelper.Insert <ChapterItem>(
                         "Name, Content, BookId, Url",
                         "@Name, @Content, @BookId, @Url",
                         new SQLiteParameter("@Name", chapter.Name),
                         new SQLiteParameter("@Content", LocalHelper.ReadTemp(chapter.Content)),
                         new SQLiteParameter("@BookId", item.Id),
                         new SQLiteParameter("@Url", chapter.Url));
                 }
                 trans.Commit();
             }
             catch (Exception)
             {
                 trans.Rollback();
             }
         }
         DatabaseHelper.Close();
         RingVisibility = Visibility.Collapsed;
     });
 }
예제 #3
0
        private void _begin()
        {
            var item  = new BookItem(Name, Source, Kind, Url);
            var conn  = DatabaseHelper.Open();
            var count = DatabaseHelper.Find <BookItem>("count(Name)", "Name = @name", new SQLiteParameter("@name", item.Name));

            if (Convert.ToInt32(count) > 0)
            {
                DatabaseHelper.Close();
                _showMessage("书名存在,请更改!");
                RingVisibility = Visibility.Collapsed;
                return;
            }
            var chapters = new List <ChapterItem>();

            var watch = new Stopwatch();

            watch.Start();
            LocalHelper.CreateTempDir();
            var rule = new WebRuleItem();

            if (item.Source == BookSources.网络)
            {
                rule = DatabaseHelper.GetRule(item.Url);
                if (rule == null)
                {
                    DatabaseHelper.Close();
                    _showMessage("网站规则不存在,请添加!");
                    RingVisibility = Visibility.Collapsed;
                    watch.Stop();
                    return;
                }
                chapters.AddRange(Helper.HttpHelper.GetBook(ref item, rule));
            }
            else
            {
                chapters.AddRange(LocalHelper.GetChapters(item.Url));
            }
            var id = DatabaseHelper.InsertId <BookItem>(
                "Name, Image, Description, Author, Source, Kind, Url, `Index`, Count, Time",
                "@name,@image,@description,@author,@source,@kind, @url, @index, @count, @time",
                new SQLiteParameter("@name", item.Name),
                new SQLiteParameter("@image", item.Image),
                new SQLiteParameter("@description", item.Description),
                new SQLiteParameter("@author", item.Author),
                new SQLiteParameter("@source", item.Source),
                new SQLiteParameter("@kind", item.Kind),
                new SQLiteParameter("@url", item.Url),
                new SQLiteParameter("@index", item.Index),
                new SQLiteParameter("@count", item.Count),
                new SQLiteParameter("@time", item.Time));

            if (item.Source == BookSources.网络)
            {
                _showMessage("章节下载中", 0);
                var chapterLength = chapters.Count;
                var result        = Parallel.ForEach <ChapterItem>(chapters, chapter =>
                {
                    var i = 0;
                    if (File.Exists(LocalHelper.TempDir + chapter.Content))
                    {
                        return;
                    }
                    OneBegin:
                    var html = new HtmlExpand();
                    try
                    {
                        html.SetUrl(chapter.Url);  // 超时
                        html.Narrow(rule.ChapterBegin, rule.ChapterEnd);
                        LocalHelper.WriteTemp(
                            html.GetText(rule.Replace), chapter.Content);
                    }
                    catch (Exception ex)
                    {
                        if (i < 5)
                        {
                            Thread.Sleep(20000);
                            goto OneBegin;
                        }
                        LocalHelper.WriteLog(
                            $"{ex.Message} 网址 {chapter.Url}");
                    }
                });
                while (!result.IsCompleted)
                {
                    Thread.Sleep(1000);
                }
                _showMessage("章节下载成功,导入中");
            }

            var writer = new StreamWriter($"{LocalHelper.TempDir}{Name}.txt", false, Encoding.UTF8);

            foreach (var chapter in chapters)
            {
                writer.WriteLine(chapter.Name);
                writer.WriteLine();
                writer.WriteLine(LocalHelper.ReadTemp(chapter.Content));
                writer.WriteLine();
                writer.WriteLine();
            }
            writer.Close();


            try
            {
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (var chapter in chapters)
                    {
                        DatabaseHelper.Insert <ChapterItem>(
                            "Name, Content, BookId, Url",
                            "@Name, @Content, @BookId, @Url",
                            new SQLiteParameter("@Name", chapter.Name),
                            new SQLiteParameter("@Content", LocalHelper.ReadTemp(chapter.Content)),
                            new SQLiteParameter("@BookId", id),
                            new SQLiteParameter("@Url", chapter.Url));
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    LocalHelper.WriteLog(ex.Message);
                    _showMessage("章节插入失败,已进行回滚!");
                }
            }
            catch (Exception ex)
            {
                LocalHelper.WriteLog(ex.Message);
            }


            DatabaseHelper.Close();
            _addItem.Execute(item);
            RingVisibility = Visibility.Collapsed;
            Name           = Url = string.Empty;
            watch.Stop();
            _showMessage("执行完成!耗时:" + watch.Elapsed);
        }