public async Task ConvertHtmlToEpub(ContentsInfo contents) { _log.Debug("epub start"); using (var epubStream = File.Create(_outputFile)) using (var epubWriter = await EPubWriter.CreateWriterAsync( epubStream, Path.GetFileNameWithoutExtension(_outputFile), _author, _bookId)) { epubWriter.Publisher = _publisher; foreach (var chapter in contents.ChapterFiles) { await epubWriter.AddChapterAsync( chapter.Name + ".html", Path.GetFileNameWithoutExtension(chapter.Name), chapter.XhtmlContent.ToString()); } await epubWriter.AddResourceAsync( "style.css", "text/css", File.ReadAllBytes(Path.Combine(_resourceLocator.ResourcesPath, "data", "epub", "style.css"))); await epubWriter.WriteEndOfPackageAsync().ContinueWith(_ => { _log.Debug("epub done"); }); } }
private async Task WriteEPub() { try { using (var ePubStream = File.Create(filePath)) { using (var writer = await EPubWriter.CreateWriterAsync( ePubStream, Path.GetFileNameWithoutExtension(filePath), "Generator", baseRoom, leaveOpen: true)) { ISPage firstPage = pageMap.Values.Where(x => x.RoomId.Equals(baseRoom)).First(); await writer.AddChapterAsync(firstPage.RoomId + ".html", firstPage.RoomId, GetEpubPageContents(firstPage)); foreach (ISPage page in pageMap.Values.Where(x => !x.RoomId.Equals(baseRoom))) { await writer.AddChapterAsync(page.RoomId + ".html", page.RoomId, GetEpubPageContents(page)); } // Add a chapter with string content as x-html //await writer.AddChapterAsync( // "FirstChapter.xhtml", // "First Chapter Title", // File.ReadAllText("FirstChapter.xhtml")); // Streaming mode //var chapterStream = writer.GetChapterStream( // "SecondChapter.xhtml", // "Second Chapter - Streaming"); // Fill the chapter-stream // await chapterStream.WriteAsync(...) // Add an image from the disk //await writer.AddResourceAsync("MyLogo.png", "image/png", File.ReadAllBytes("MyLogo.png")); // Streaming mode for images //var imageStream = writer.GetResourceStream("MyBackground.jpg", "image/jpeg"); // Fill the image-stream // await imageStream.WriteAsync(...) // Must be called async at the end to write down the TOC of the ePub await writer.WriteEndOfPackageAsync(); //ePubStream.Flush(); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private static async Task TestEPub() { var ePubStream = File.Create("Test.epub"); using (var writer = await EPubWriter.CreateWriterAsync( ePubStream, "My Title", "Me, The Author", "My Unique Identifier")) { // Optional parameter writer.Publisher = "Demo App"; // Add a chapter with string content as x-html await writer.AddChapterAsync( "FirstChapter.xhtml", "First Chapter Title", File.ReadAllText("FirstChapter.xhtml")); // Streaming mode var chapterStream = writer.GetChapterStream( "SecondChapter.xhtml", "Second Chapter - Streaming"); // Fill the chapter-stream // await chapterStream.WriteAsync(...) // Add an image from the disk await writer.AddResourceAsync("MyLogo.png", "image/png", File.ReadAllBytes("MyLogo.png")); // Streaming mode for images var imageStream = writer.GetResourceStream("MyBackground.jpg", "image/jpeg"); // Fill the image-stream // await imageStream.WriteAsync(...) // Must be called async at the end to write down the TOC of the ePub await writer.WriteEndOfPackageAsync(); } }
private void DownloadChapter(Dictionary <int, string> Chapters, int ID, IHost Host, ComicInfo Info) { string Name = DataTools.GetRawName(Chapters[ID], FileNameMode: true); StatusBar.SecondLabelText = $"{Info.Title} - {string.Format(CurrentLanguage.ChapterName, Name)}"; Status = CurrentLanguage.Loading; int KIndex = Chapters.IndexOfKey(ID); string LName = null; string LastChapterPah = null; if (KIndex + 1 < Chapters.Keys.Count) { LName = DataTools.GetRawName(Chapters.Values.ElementAt(KIndex + 1), FileNameMode: true); } string NName = null; string NextChapterPath = null; if (KIndex > 0) { NName = DataTools.GetRawName(Chapters.Values.ElementAt(KIndex - 1), FileNameMode: true); } string Title = DataTools.GetRawName(Info.Title.Trim(), FileNameMode: true); ChapterTools.MatchLibraryPath(ref Title, Settings.LibraryPath, CurrentInfo.Url, (ReplaceMode)Settings.ReplaceMode, CurrentLanguage); RefreshCoverLink(Title); string TitleDir = Path.Combine(Settings.LibraryPath, Title); if (!Directory.Exists(TitleDir)) { Directory.CreateDirectory(TitleDir); RefreshLibrary(); } if (LName != null) { ChapterTools.GetChapterPath(Languages, CurrentLanguage, TitleDir, LName, out LastChapterPah, false); } ChapterTools.GetChapterPath(Languages, CurrentLanguage, TitleDir, Name, out string ChapterPath, false); if (NName != null) { ChapterTools.GetChapterPath(Languages, CurrentLanguage, TitleDir, NName, out NextChapterPath, false); } string AbsoluteChapterPath = Path.Combine(TitleDir, ChapterPath); if (Settings.SkipDownloaded && File.Exists(AbsoluteChapterPath.TrimEnd('\\', '/') + ".html")) { return; } int PageCount = 1; if (Info.ContentType == ContentType.Comic) { PageCount = Host.GetChapterPageCount(ID); if (Directory.Exists(AbsoluteChapterPath) && Directory.GetFiles(AbsoluteChapterPath, "*").Length < PageCount) { Directory.Delete(AbsoluteChapterPath, true); } if (Settings.SkipDownloaded && Directory.Exists(AbsoluteChapterPath)) { var Pages = (from x in Directory.GetFiles(AbsoluteChapterPath) select Path.GetFileName(x)).ToArray(); ChapterTools.GenerateComicReader(CurrentLanguage, Pages, LastChapterPah, NextChapterPath, TitleDir, ChapterPath, Name); string OnlineData = string.Format(Properties.Resources.UrlFile, Info.Url.AbsoluteUri); File.WriteAllText(Path.Combine(TitleDir, "Online.url"), OnlineData); return; } } if (Info.ContentType == ContentType.Novel) { ChapterPath = Path.Combine(AbsoluteChapterPath, string.Format(CurrentLanguage.ChapterName, Name) + ".epub"); if (Settings.SkipDownloaded && File.Exists(ChapterPath)) { return; } } if (!Directory.Exists(AbsoluteChapterPath)) { Directory.CreateDirectory(AbsoluteChapterPath); } string UrlData = string.Format(Properties.Resources.UrlFile, Info.Url.AbsoluteUri); File.WriteAllText(Path.Combine(TitleDir, "Online.url"), UrlData); switch (Info.ContentType) { case ContentType.Comic: var Decoder = Host.GetDecoder(); List <string> Pages = new List <string>(); foreach (var Data in Host.DownloadPages(ID).CatchExceptions()) { Status = string.Format(CurrentLanguage.Downloading, Pages.Count + 1, PageCount); Application.DoEvents(); bool IsWebP = Data.Length > 12 && BitConverter.ToUInt32(Data, 8) == 0x50424557; try { #if NOASYNCSAVE string PageName = $"{Pages.Count:D3}.png"; string PagePath = Path.Combine(TitleDir, ChapterPath, PageName); Page OutPage = new Page(); OutPage.Data = Data; if ((SaveAs)Settings.SaveAs != SaveAs.RAW) { using (Bitmap Result = Decoder.Decode(Data)) { PageName = $"{Pages.Count:D3}.{GetExtension(Result, out ImageFormat Format)}"; PagePath = OutPage.Path = Path.Combine(TitleDir, ChapterPath, PageName); if (Result.GetImageExtension() == "png" && Settings.APNGBypass) { var OutData = BypassAPNG(Data); OutPage.Data = OutData; } else { using (MemoryStream tmp = new MemoryStream()) { Result.Save(tmp, Format); OutPage.Data = tmp.ToArray(); } } } } if (Settings.ImageClipping) { PostProcessQueue.Enqueue(OutPage); } else { File.WriteAllBytes(OutPage.Path, OutPage.Data); } while (PostProcessQueue.Count > 0) { ThreadTools.Wait(1000, true); } #else string PageName = $"{Pages.Count:D3}.{(IsWebP ? "webp" : "png")}"; string PagePath = Path.Combine(TitleDir, ChapterPath, PageName); Page OutPage = new Page(); OutPage.Data = Data; if ((SaveAs)Settings.SaveAs != SaveAs.RAW) { byte[] ModData = null; if (IsWebP) { ModData = DecodeWebP(Data); } using (MemoryStream Buffer = new MemoryStream()) using (Bitmap Result = Decoder.Decode(ModData ?? Data)) { PageName = $"{Pages.Count:D3}.{GetExtension(Result, out ImageFormat Format)}"; PagePath = Path.Combine(TitleDir, ChapterPath, PageName); if (Result.GetImageExtension() == "png" && Settings.APNGBypass) { using (MemoryStream OutBuffer = new MemoryStream(BypassAPNG(ModData ?? Data))) using (Bitmap Img = new Bitmap(OutBuffer)) { Img.Save(Buffer, Format); OutPage.Data = Buffer.ToArray(); } } else { Result.Save(Buffer, Format); OutPage.Data = Buffer.ToArray(); } } } OutPage.Path = PagePath; if (PostProcessQueue.Count > 5) { const ulong MinMemory = 400000000; while (AvailablePhysicalMemory < MinMemory && PostProcessQueue.Count > 0 || (Settings.MaxPagesBuffer != 0 && PostProcessQueue.Count >= Settings.MaxPagesBuffer)) { ThreadTools.Wait(1000, true); } } PostProcessQueue.Enqueue(OutPage); #endif Pages.Add(PageName); } catch (Exception ex) { if (Program.Debug) { throw; } Console.Error.WriteLine(ex.ToString()); } } if (Settings.ReaderGenerator) { ChapterTools.GenerateComicReader(CurrentLanguage, Pages.ToArray(), LastChapterPah, NextChapterPath, TitleDir, ChapterPath, Name); ChapterTools.GenerateReaderIndex(Languages, CurrentLanguage, Info, TitleDir, Name); } break; case ContentType.Novel: var Chapter = Host.DownloadChapter(ID); AsyncContext.Run(async() => { using (var Epub = await EPubWriter.CreateWriterAsync(File.Create(ChapterPath), Info.Title ?? "Untitled", Chapter.Author ?? "Anon", Chapter.URL ?? "None")) { Chapter.HTML.RemoveNodes("//script"); Chapter.HTML.RemoveNodes("//iframe"); foreach (var Node in Chapter.HTML.DocumentNode.DescendantsAndSelf()) { if (Node.Name == "img" && Node.GetAttributeValue("src", string.Empty) != string.Empty) { string Src = Node.GetAttributeValue("src", string.Empty); string RName = Path.GetFileName(Src.Substring(null, "?", IgnoreMissmatch: true)); string Mime = ResourceHandler.GetMimeType(Path.GetExtension(RName)); if (Node.GetAttributeValue("type", string.Empty) != string.Empty) { Mime = Node.GetAttributeValue("type", string.Empty); } Uri Link = new Uri(Info.Url, Src); if (string.IsNullOrWhiteSpace(RName)) { continue; } byte[] Buffer = await Link.TryDownloadAsync(); if (Buffer == null) { continue; } await Epub.AddResourceAsync(RName, Mime, Buffer); Node.SetAttributeValue("src", RName); } if (Node.Name == "link" && Node.GetAttributeValue("rel", string.Empty) == "stylesheet" && Node.GetAttributeValue("href", string.Empty) != string.Empty) { string Src = Node.GetAttributeValue("href", string.Empty); string RName = Path.GetFileName(Src.Substring(null, "?", IgnoreMissmatch: true)); string Mime = ResourceHandler.GetMimeType(Path.GetExtension(RName)); if (Node.GetAttributeValue("type", string.Empty) != string.Empty) { Mime = Node.GetAttributeValue("type", string.Empty); } Uri Link = new Uri(Info.Url, Src); if (string.IsNullOrWhiteSpace(RName)) { continue; } byte[] Buffer = await Link.TryDownloadAsync(); if (Buffer == null) { continue; } await Epub.AddResourceAsync(RName, Mime, Buffer); Node.SetAttributeValue("href", RName); } } Epub.Publisher = lblTitle.Text; await Epub.AddChapterAsync("chapter.xhtml", Chapter.Title, Chapter.HTML.ToHTML()); await Epub.WriteEndOfPackageAsync(); } }); break; default: throw new Exception("Invalid Content Type"); } }
private async Task TestEPub() { var chapters = JsonConvert.DeserializeObject <List <ChapterInfo> >( File.ReadAllText(_cfg.ChapterJson)) .Select((o, i) => { o.Idx = i; return(o); }).ToList(); BlockingCollection <ChapterContent> processedData = new BlockingCollection <ChapterContent>(); BufferBlock <ChapterInfo> bufferBlock = new BufferBlock <ChapterInfo>(new DataflowBlockOptions() { BoundedCapacity = 100 }); int bl = 0; int total = chapters.Count; int len = total.ToString().Length; ActionBlock <ChapterInfo> processContent = new ActionBlock <ChapterInfo>(chapterInfo => { processedData.Add(_driver.GetChapterContent(chapterInfo)); _cfg.LogQueue.Enqueue($"Parsed: {chapterInfo.Idx} - {chapterInfo.Title} - {chapterInfo.Link}"); }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }); bufferBlock.LinkTo(processContent, new DataflowLinkOptions() { PropagateCompletion = true }); chapters.ForEach((chap) => { bufferBlock.SendAsync(chap).Wait(); }); bufferBlock.Complete(); bufferBlock.Completion.Wait(); processContent.Complete(); processContent.Completion.Wait(); var ePubStream = File.Create($"{_dir}.epub"); using (var writer = await EPubWriter.CreateWriterAsync( ePubStream, Regex.Replace(Regex.Replace($"{_dir}", "[^a-zA-Z0-9]", " "), "\\s+", " "), "tntdb", "08915002", new CultureInfo("vi-VN"))) { // Optional parameter writer.Publisher = "tntdb"; bl = 0; foreach (var chapterContent in processedData.OrderBy(o => o.Idx).ToList()) { Interlocked.Increment(ref bl); _cfg.LogQueue.Enqueue($"Creating chapter {bl.ToString().PadLeft(len, ' ')} / {(total-bl).ToString().PadLeft(len, ' ')} : {chapterContent.FileName}"); // var idx = chapters.IndexOf(chap); writer.AddChapterAsync( chapterContent.FileName, chapterContent.Title, chapterContent.Content).Wait(); } // Add a chapter with string content as x-html await writer.WriteEndOfPackageAsync(); } _cfg.LogQueue.Enqueue("Process done!"); _cfg.Done = true; }