public void Parse_All(string url)
        {
            Thread t = new Thread(() =>
            {
                NovelModel novelModel;

                using (var uow = new NovelUnitOfWork())
                {
                    var service = new NovelDomainService(uow);
                    novelModel  = service.GetNovelModelByUrl(url);
                }

                if (novelModel == null)
                {
                    return;
                }

                var todoToal = novelModel.Chapters.Count(x => string.IsNullOrEmpty(x.Content));

                var downloadCount = 0;

                foreach (var chapterModel in novelModel.Chapters.Where(x => string.IsNullOrEmpty(x.Content)))
                {
                    Parse_Chapter(chapterModel);
                    downloadCount++;
                    string message = $"当前进度:{downloadCount}/{todoToal};正在下载 {chapterModel.Title}";
                    InvokeScriptFunction(() =>
                    {
                        Document.InvokeScript("show_message", new[] { message, "success" });
                    });
                }
            });

            t.Start();
        }
Beispiel #2
0
        public void Update_all_novel_chapters(string args)
        {
            using (var uow = new NovelUnitOfWork())
            {
                var novelService = new NovelDomainService(uow);

                var novelList = novelService.GetAll().Include(x => x.Chapters).ToList();

                InvokeScriptFunction("display_novel_list", novelList.Select(x => new NovelDto
                {
                    Id = x.Id,
                    MaxChapterIndex = x.MaxChapterIndex,
                    MenuUrl         = x.MenuUrl,
                    NovelName       = x.NovelName
                }).ToList().ToJson());

                foreach (var novelModel in novelList)
                {
                    if (novelModel.Chapters.IsNullOrEmpty())
                    {
                        novelModel.Chapters = new List <ChapterModel>();
                    }

                    var chapterDtoList = Get_ChapterList(novelModel);

                    if (chapterDtoList.IsNullOrEmpty())
                    {
                        continue;
                    }

                    novelModel.Chapters.AddRange(chapterDtoList.Select(x => new ChapterModel
                    {
                        LastUpdatedTime = DateTime.Now,
                        SortId          = x.SortId,
                        Title           = x.Title,
                        Url             = x.Url
                    }));

                    novelModel.MaxChapterIndex = novelModel.Chapters.Count;

                    InvokeScriptFunction(() =>
                    {
                        Document.InvokeScript(
                            "show_process",
                            new[] { novelModel.Id.ToString(), $"章节更新完成.新增{chapterDtoList.Count}章", "success" });
                    });
                }
            }
        }
Beispiel #3
0
        public void Load_novel_list(string args)
        {
            List <NovelDto> novelList;

            using (var uow = new NovelUnitOfWork())
            {
                var service = new NovelDomainService(uow);
                novelList = service.GetAll().Select(x => new NovelDto
                {
                    Id = x.Id,
                    MaxChapterIndex = x.MaxChapterIndex,
                    MenuUrl         = x.MenuUrl,
                    NovelName       = x.NovelName
                }).ToList();
            }

            InvokeScriptFunction("display_novel_list", novelList.ToJson());
        }
        public void Parse_Chapter(ChapterModel chapterModel)
        {
            Random r = new Random();
            var    delayTimeSpance = r.Next(1, 10);

            Thread.Sleep(delayTimeSpance * 1000);

            try
            {
                var connection  = NSoupClient.Connect(chapterModel.Url);
                var document    = connection.Get();
                var element     = document.GetElementById("content");
                var contentHtml = element.Html();
                var lines       = Regex.Replace(contentHtml, "<br.*?/>", "");
                lines = Regex.Replace(lines, "&nbsp;", " ");

                if (lines.Length <= 0)
                {
                    return;
                }

                using (var uow = new NovelUnitOfWork())
                {
                    var chapterService = new ChapterDomainService(uow);
                    chapterService.Update(chapterModel.Id, x =>
                    {
                        x.Content         = lines;
                        x.LastUpdatedTime = DateTime.Now;
                    });
                }
            }
            catch (Exception e)
            {
                //InvokeScriptFunction("show_message", e.Message);

                InvokeScriptFunction(() =>
                {
                    Document.InvokeScript("show_message", new[] { e.Message, "danger" });
                });
            }
        }
Beispiel #5
0
 public NovelDomainService(NovelUnitOfWork uow) : base(uow)
 {
 }
        public void Parse_Menu_Url(string url)
        {
            var domainUrl = "http://www.biquge.com.tw";

            Document document = null;

            try
            {
                var connect = NSoupClient.Connect(url);
                document = connect.Get();
            }
            catch (Exception ex)
            {
                InvokeScriptFunction(() =>
                {
                    Document.InvokeScript("show_message", new[] { ex.Message, "danger" });
                });
                return;
            }

            var linkArray = document?.GetAllElements().Where(x => x.TagName() == "a").ToList();

            var novelName = document?.GetElementById("info").Children.First.Text();

            CurrentBrowser.Parent.Text = novelName;

            using (var uow = new NovelUnitOfWork())
            {
                uow.BeginTransaction();

                var service    = new NovelDomainService(uow);
                var novelModel = service.GetNovelModelByUrl(url);
                var isUpdate   = novelModel != null;
                if (novelModel == null)
                {
                    novelModel = new NovelModel {
                        Chapters = new List <ChapterModel>()
                    };
                }

                novelModel.MenuUrl   = url;
                novelModel.NovelName = novelName;

                int chapterCount = 0;
                var pattern      = @"/\d+_\d+/\d+.html";

                foreach (Element element in linkArray)
                {
                    var href = element.Attr("href");
                    if (!element.HasText || !Regex.IsMatch(href, pattern))
                    {
                        continue;
                    }

                    chapterCount++;

                    var chapterUrl = $"{domainUrl}{href}";
                    var text       = element.Text();

                    if (novelModel.Chapters.Any(x => x.Url.Equals(chapterUrl, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        continue;
                    }

                    novelModel.Chapters.Add(new ChapterModel
                    {
                        Title           = text,
                        Url             = chapterUrl,
                        SortId          = chapterCount,
                        LastUpdatedTime = DateTime.Now
                    });
                }

                novelModel.MaxChapterIndex = chapterCount;

                AppManagement.Cache.Set(url, novelModel);

                if (!novelModel.Chapters.Any())
                {
                    InvokeScriptFunction(() =>
                    {
                        Document.InvokeScript("show_message", new[] { "没有解析出任何内容", "danger" });
                    });
                }

                var argsStr = JsonConvert.SerializeObject(novelModel.Chapters.Where(x => x.Id <= 0)
                                                          .OrderBy(x => x.SortId).Select(x => new
                {
                    x.Url,
                    x.Title
                }).ToList());

                InvokeScriptFunction("loadMenu", argsStr);

                if (isUpdate)
                {
                    return;
                }

                service.Add(novelModel);
            }
        }
Beispiel #7
0
        public void Export_Novel_By_Thread(string args)
        {
            var novelId = int.Parse(args);

            var dirPath = string.Empty;

            InvokeWindow(() =>
            {
                var dialog = new FolderBrowserDialog();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    dirPath = dialog.SelectedPath;
                }
            });

            if (dirPath.IsNullOrEmpty())
            {
                return;
            }

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            NovelModel novelModel;

            using (var uow = new NovelUnitOfWork())
            {
                var service = new NovelDomainService(uow);
                novelModel = service.GetAll().Include(x => x.Chapters).FirstOrDefault(x => x.Id == novelId);
            }

            if (novelModel == null)
            {
                InvokeScriptFunction(() =>
                {
                    Document.InvokeScript("show_message", new[] { $"不存在编号为{novelId}的小说.", "danger" });
                });
                return;
            }

            var filePath = Path.Combine(dirPath, $"{novelModel.NovelName}.txt");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                using (var sw = new StreamWriter(fileStream))
                {
                    foreach (var chapterModel in novelModel.Chapters.OrderBy(x => x.SortId).ThenBy(x => x.Id))
                    {
                        sw.WriteLine("\n\n");
                        sw.WriteLine(chapterModel.Title);
                        sw.WriteLine("\n\n");
                        sw.WriteLine(chapterModel.Content.IsNullOrEmpty() ? "该章节缺失" : chapterModel.Content);

                        InvokeScriptFunction(() =>
                        {
                            Document.InvokeScript("show_process",
                                                  new[] { args, $"正在导出:{chapterModel.Title}", "success" });
                        });
                    }
                }

            InvokeScriptFunction(() =>
            {
                Document.InvokeScript("show_process", new[] { args, $"导出完成,请浏览:{filePath}", "success" });
            });
        }