Exemple #1
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);
        }