public async Task <ActionResult <string> > GenerateHtmlFileEpub4Net(int id) { System.IO.Directory.CreateDirectory("output/chapters"); System.IO.Directory.CreateDirectory("output/final"); System.IO.Directory.CreateDirectory("output/build"); System.IO.Directory.CreateDirectory("output/books"); Book book = _context.Books.First(a => a.Id == id); List <Chapter> chapters = _context.Chapters.Where(a => a.Book == book).OrderBy(a => a.ChapterNumber).ToList(); BookTemplate bookTemplate = _context.BookTemplates.First(a => a.Book == book); List <Epub4Net.Chapter> epubChapters = new List <Epub4Net.Chapter>(); foreach (Chapter chapter in chapters) { using (var writer = System.IO.File.CreateText("output/chapters/" + chapter.ChapterNumber + ".html")) { await writer.WriteAsync(String.Format(bookTemplate.Chapter, chapter.Body)); } epubChapters.Add( new Epub4Net.Chapter( "chapters/" + chapter.ChapterNumber + ".html", chapter.ChapterNumber + ".html", chapter.Title ) ); } var workingDir = System.IO.Directory.GetCurrentDirectory(); System.IO.Directory.SetCurrentDirectory("output"); Epub4Net.Epub epub = new Epub4Net.Epub(book.Name, " - ", epubChapters); epub.Language = "en"; Epub4Net.EPubBuilder ePubBuilder = new Epub4Net.EPubBuilder(new Epub4Net.FileSystemManager("final"), "build"); var result = ePubBuilder.Build(epub); System.IO.Directory.SetCurrentDirectory(workingDir); return(result); }
private void download_btn_Click(object sender, EventArgs e) { if (LightNovelDownloadList.CheckedItems.Count > 0) { List<Webpage> webpages = new List<Webpage>(); foreach (Chapter item in LightNovelDownloadList.CheckedItems) { Webpage newpage = new Webpage(); System.Diagnostics.Debug.WriteLine(item.Link); newpage.html = new System.Net.WebClient().DownloadString(item.Link); item.downloadscheduled = true; } foreach (LightNovel item in LightNovelAvailableField.CheckedItems) { foreach (Chapter chapter in item.Chapter) { System.Net.WebClient client = new System.Net.WebClient(); client.Encoding = System.Text.Encoding.UTF8; String html = client.DownloadString(chapter.Link); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); if (chapter.Link.Contains("japtem")) { var data = from nodes in doc.DocumentNode.Descendants("div") where nodes.Attributes["class"] != null && nodes.Attributes["class"].Value.Contains("post-body entry-content") select nodes; foreach (var innterhtmldata in data) { chapter.html = innterhtmldata.InnerHtml; } foreach (Chapter test in LightNovelDownloadList.CheckedItems) { if (test.Title == chapter.Title) { chapter.downloadscheduled = true; } } } if(chapter.Link.Contains("baka-tsuki")) { var data = doc.DocumentNode.Descendants("div").Where(div => div.Attributes["id"] != null).Where(precisediv => precisediv.Attributes["id"].Value == "content" || precisediv.Attributes["id"].Value == "mw-content-text"); foreach (var dataitems in data) { chapter.html = dataitems.InnerHtml; } foreach (Chapter test in LightNovelDownloadList.CheckedItems) { if (test.Title == chapter.Title) { chapter.downloadscheduled = true; } } } //put this back in after testin phase to lessen the load on webserver //System.Threading.Thread.Sleep(2000); } } System.IO.Directory.CreateDirectory("temp"); string dir = Directory.GetCurrentDirectory(); dir = dir + "\\temp\\"; System.Threading.Thread.Sleep(1000); // loop through Selected Lightnovels here and generate Epub from chapterhtml foreach (LightNovel lightnovel in LightNovelAvailableField.CheckedItems) { //loop chapters List<Epub4Net.Chapter> chapters = new List<Epub4Net.Chapter>(); foreach (Chapter chapter in lightnovel.Chapter) { if (chapter.downloadscheduled) { string filename = chapter.Title.Replace(" ", "_") + ".html"; string file = filename.Replace(":", ""); filename = dir + filename.Replace(":", ""); string enhanced = ""; if (chapter.html.IndexOf("<table") >= 0) { enhanced = chapter.html.Substring(0, chapter.html.IndexOf("<table")); } else { enhanced = chapter.html; } string htmlcode = "<?xml version='1.0' encoding='utf-8'?><html xmlns='http://www.w3.org/1999/xhtml' lang='de' xml:lang='de'><head><title>Unknown</title>< meta http - equiv = 'Content-Type' content = 'text/html; charset=utf-8'/></head><body>" + enhanced + "</body></html>"; System.IO.File.WriteAllText(filename, htmlcode, Encoding.UTF8); chapters.Add(new Epub4Net.Chapter(filename, file, chapter.Title)); } } // TODO:Check why epub is invalid Epub4Net.Epub epub = new Epub4Net.Epub(lightnovel.Title, "LightNovelParser", chapters); epub.BookId = "1"; epub.Language = "en"; epub.Publisher = "LightNovelParser"; epub.Subject = "Entertainment"; Epub4Net.EPubBuilder builder = new Epub4Net.EPubBuilder(); var epubFilePath = builder.Build(epub); } string[] filePaths = Directory.GetFiles(dir); foreach (var path in filePaths) { //File.Delete(path); } MessageBox.Show("finished"); } }