コード例 #1
0
ファイル: HttpHelper.cs プロジェクト: zx648383079/WPF-Reader
 public static List<ChapterItem> GetChapters(BookItem item, WebRuleItem rule, HtmlExpand html)
 {
     var chapters = new List<ChapterItem>();
     var ms =
         html.Narrow(rule.CatalogBegin, rule.CatalogEnd)
             .Matches(@"<a[^<>]+?href=""?(?<href>[^""<>\s]+)[^<>]*>(?<title>[\s\S]+?)</a>");
     foreach (Match match in ms)
     {
         var url = match.Groups["href"].Value;
         chapters.Add(new ChapterItem(match.Groups["title"].Value, UrlHelper.GetAbsolute(item.Url, url),
             LocalHelper.GetSafeFile(url)));
     }
     return chapters;
 }
コード例 #2
0
ファイル: HttpHelper.cs プロジェクト: zx648383079/WPF-Reader
 public static List<ChapterItem> GetBook(ref BookItem item, WebRuleItem rule)
 {
     var html = new HtmlExpand();
     html.SetUrl(item.Url);
     if (!string.IsNullOrWhiteSpace(rule.CoverBegin) || !string.IsNullOrWhiteSpace(rule.CoverEnd))
     {
         item.Image = html.GetCover(rule.CoverBegin, rule.CoverEnd);
     }
     if (!string.IsNullOrWhiteSpace(rule.AuthorBegin) || !string.IsNullOrWhiteSpace(rule.AuthorEnd))
     {
         item.Author = html.GetAuthor(rule.AuthorBegin, rule.AuthorEnd);
     }
     if (!string.IsNullOrWhiteSpace(rule.DescriptionBegin) || !string.IsNullOrWhiteSpace(rule.DescriptionEnd))
     {
         item.Description = html.GetDescription(rule.DescriptionBegin, rule.DescriptionEnd);
     }
     var chapters = GetChapters(item, rule, html);
     item.Count = chapters.Count;
     return chapters;
 }
コード例 #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();
            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));
            LocalHelper.CreateTempDir();
            //if (item.Source == BookSources.网络)
            //{

            //    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);
            //    }
            //}

            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);
        }
コード例 #4
0
 private void ExecuteSaveCommand()
 {
     if (string.IsNullOrWhiteSpace(Url)) return;
     var item = new WebRuleItem();
     item.Name = Name;
     item.Url = UrlHelper.GetWeb(Url);
     item.CatalogBegin = CatalogBegin;
     item.CatalogEnd = CatalogEnd;
     item.ChapterBegin = ChapterBegin;
     item.ChapterEnd = ChapterEnd;
     item.Replace = Replace;
     item.AuthorBegin = AuthorBegin;
     item.AuthorEnd = AuthorEnd;
     item.DescriptionBegin = DescriptionBegin;
     item.DescriptionEnd = DescriptionEnd;
     item.CoverBegin = CoverBegin;
     item.CoverEnd = CoverEnd;
     _addItem.Execute(item);
 }