Exemplo n.º 1
0
        public static WebRuleItem GetRule(string url)
        {
            url = UrlHelper.GetWeb(url);
            var reader = Select <WebRuleItem>("*", "WHERE Url = @url LIMIT 1", new SQLiteParameter("@url", url));

            reader.Read();
            if (reader.HasRows)
            {
                var item = new WebRuleItem(reader);
                reader.Close();
                return(item);
            }
            reader.Close();
            return(null);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the AddRuleViewModel class.
 /// </summary>
 public AddRuleViewModel()
 {
     Messenger.Default.Register <NotificationMessageAction <WebRuleItem> >(this, "rule", m =>
     {
         _addItem = m;
         if (m.Sender != null)
         {
             WebRuleItem item = m.Sender as WebRuleItem;
             Name             = item.Name;
             Url              = item.Url;
             CatalogBegin     = item.CatalogBegin;
             CatalogEnd       = item.CatalogEnd;
             ChapterBegin     = item.ChapterBegin;
             ChapterEnd       = item.ChapterEnd;
             Replace          = item.Replace;
             AuthorBegin      = item.AuthorBegin;
             AuthorEnd        = item.AuthorEnd;
             DescriptionBegin = item.DescriptionBegin;
             DescriptionEnd   = item.DescriptionEnd;
             CoverBegin       = item.CoverBegin;
             CoverEnd         = item.CoverEnd;
         }
     });
 }
Exemplo n.º 6
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);
        }