Ejemplo n.º 1
0
 private void btnEpub_Click(object sender, EventArgs e)
 {
     using (OpenFileDialog ofd = new OpenFileDialog()
     {
         Filter = "EPUB files|*.epub", ValidateNames = true, Multiselect = false
     })
     {
         if (ofd.ShowDialog() == DialogResult.OK)
         {
             try
             {
                 EpubBook      ebook = EpubReader.ReadBook(ofd.FileName);
                 StringBuilder sb    = new StringBuilder();
                 foreach (EpubChapter chapter in ebook.Chapters)
                 {
                     sb.Append(MainAppForm.PrintChapter(chapter));
                 }
                 rtbxText.Text = sb.ToString();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
     }
 }
Ejemplo n.º 2
0
        public bool ReadFile(string filePath)
        {
            //getEpubFileName();

            if (File.Exists(filePath))
            {
                _filePath = filePath;
                _fileName = Path.GetFileNameWithoutExtension(_filePath);

                if (!Directory.Exists(_library))
                {
                    Directory.CreateDirectory(_library);
                }
                _tempPath = Path.Combine(_library, _fileName);

                unZipFile();
                Clear();

                epubBook             = EpubReader.ReadBook(_filePath);
                _baseMenuXmlDiretory = epubBook.Schema.ContentDirectoryPath;
                //get menu
                foreach (EpubContentFile epubContent in epubBook.ReadingOrder)
                {
                    _menuItems.Add(GetPath(epubContent.FileName));
                }
                //get table of contents
                getTableContent(epubBook.Navigation);
            }
            else
            {
                return(false);
            }
            return(true);
        }
        public static bool FindTextInEpubFile(string fileFullPath, string text, ref CancellationTokenSource cts)
        {
            try
            {
                // Opens a book and reads all of its content into memory
                var epubBook = EpubReader.ReadBook(fileFullPath);

                // Enumerating the whole text content of the book in the order of reading
                foreach (var textContentFile in epubBook.ReadingOrder)
                {
                    if (cts.IsCancellationRequested)
                    {
                        break;
                    }

                    // HTML of current text content file
                    string htmlContent = textContentFile.Content;

                    if (htmlContent.Contains(text))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(false);
        }
Ejemplo n.º 4
0
        public ReadEpubFile(string fileName)
        {
            FileName = fileName;
            epubBook = EpubReader.ReadBook(fileName);
            Title    = epubBook.Title;
            Author   = epubBook.Author;
            var cntList = new List <Content>();

            foreach (var chapter in epubBook.Chapters)
            {
                var cnt = new Content();
                cnt.Title       = chapter.Title;
                cnt.HtmlContent = chapter.HtmlContent;
                var cntSubChapterList = new List <Content>();
                //TODO: more than 2  - recursive
                foreach (var item in chapter.SubChapters)
                {
                    var cntSubChapter = new Content();
                    cntSubChapter.Title       = item.Title;
                    cntSubChapter.HtmlContent = item.HtmlContent;
                    cntSubChapterList.Add(cntSubChapter);
                }
                cntList.Add(cnt);
            }
            content = cntList.ToArray();
        }
Ejemplo n.º 5
0
        public bool ReadFile()
        {
            getEpubFileName();

            if (_filePath != "")
            {
                unZipFile();
                Clear();

                epubBook             = EpubReader.ReadBook(_filePath);
                _baseMenuXmlDiretory = epubBook.Schema.ContentDirectoryPath;
                //get menu
                foreach (EpubContentFile epubContent in epubBook.ReadingOrder)
                {
                    _menuItems.Add(GetPath(epubContent.FileName));
                }
                //get table of contents
                getTableContent(epubBook.Navigation);
            }
            else
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 6
0
 public override void Update()
 {
     Thread.Sleep(3000);
     System.Diagnostics.Debug.WriteLine(string.Format("kitap ebook ekleme başlıyor.... ID : {0}", EbookNo));
     try
     {
         string path = string.Format("~/Files/{0}/epub_{0}.epub", EbookNo);
         using (var fs = new FileStream(HostingEnvironment.MapPath(path), FileMode.Open))
         {
             EpubBook epubBook = EpubReader.ReadBook(fs);
             foreach (EpubTextContentFile textContentFile in epubBook.ReadingOrder)
             {
                 // HTML of current text content file
                 string htmlContent = textContentFile.Content;
                 HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
                 htmlDocument.LoadHtml(htmlContent);
                 // Insert to db
                 using (VBSContext db = new VBSContext())
                 {
                     BookPage bp = new BookPage();
                     bp.BookId      = BookId;
                     bp.HtmlContent = htmlContent;
                     bp.Description = htmlDocument.DocumentNode.InnerText.Replace("\n", "<br/>");
                     db.BookPages.Add(bp);
                     db.SaveChanges();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.InnerException.Message);
     }
 }
Ejemplo n.º 7
0
        public void LireLesInfo()
        {
            EpubBook epubBook = EpubReader.ReadBook(chemin); // Lis les informations de base d'un fichier EPUB

            auteur = epubBook.Author;
            titre  = epubBook.Title;
            if (epubBook.AuthorList.Count() != 0)
            {
                auteurs = epubBook.AuthorList[0];
            }
            else
            {
                auteurs = "Pas de plusieurs auteurs.";
            }

            byte[] coverImageContent = epubBook.CoverImage;
            if (coverImageContent != null)
            {
                using (MemoryStream memoryStream = new MemoryStream(coverImageContent))
                {
                    picture = Image.FromStream(memoryStream);
                }
            }

            EpubPackage epubPackage = epubBook.Schema.Package;

            if (epubPackage.Metadata.Dates.Count() != 0)
            {
                date = epubPackage.Metadata.Dates[0].Date;
            }

            if (epubPackage.Metadata.Languages.Count() != 0)
            {
                langage = epubPackage.Metadata.Languages[0];
            }

            if (epubPackage.Metadata.Identifiers.Count() != 0)
            {
                isbn = epubPackage.Metadata.Identifiers[0].Identifier;
                //isbn = epubPackage.Metadata.Identifiers[0].Id;
            }

            if (epubPackage.Metadata.Subjects.Count() != 0)
            {
                genre = epubPackage.Metadata.Subjects[0];
            }

            if (epubPackage.Metadata.Publishers.Count() != 0)
            {
                editeur = epubPackage.Metadata.Publishers[0];
            }
            if (epubPackage.Metadata.Rights.Count() != 0)
            {
                droits = epubPackage.Metadata.Rights[0];
            }

            descri = epubPackage.Metadata.Description;
        }
Ejemplo n.º 8
0
        public static void Run(string filePath)
        {
            EpubBook book = EpubReader.ReadBook(filePath);

            foreach (EpubChapter chapter in book.Chapters)
            {
                PrintChapter(chapter);
            }
        }
Ejemplo n.º 9
0
        public static void Run(string filePath)
        {
            EpubBook book = EpubReader.ReadBook(filePath);

            foreach (EpubTextContentFile textContentFile in book.ReadingOrder)
            {
                PrintTextContentFile(textContentFile);
            }
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Upload(UploadBookViewModel m)
        {
            var f     = m.BookFile;
            var fName = m.BookName;
            var user  = await _userManager.GetUserAsync(User);

            var folder = Path.Combine(_env.WebRootPath, "bookRepo", user.NormalizedUserName);

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

            var filePath = Path.Combine(folder, f.FileName);

            if (f.Length > 0)
            {
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await f.CopyToAsync(fileStream);
                }
            }

            EpubBook epubBook = EpubReader.ReadBook(filePath);

            byte[] coverImageContent = epubBook.CoverImage;
            var    coverPath         = Path.Combine(_env.WebRootPath, "bookCovers", Guid.NewGuid().ToString() + ".jpg");

            if (coverImageContent != null)
            {
                using (MemoryStream coverImageStream = new MemoryStream(coverImageContent))
                {
                    Image coverImage = Image.FromStream(coverImageStream);
                    coverImage = (Image)(new Bitmap(coverImage, new Size(140, 180)));
                    coverImage.Save(coverPath, ImageFormat.Jpeg);
                }
            }
            else
            {
                coverPath = null;
            }

            Book newBook = new Book
            {
                Id                 = Guid.NewGuid(),
                BookName           = fName,
                BookCoverImagePath = coverPath,
                Path               = filePath,
                UploadedDateTime   = DateTime.Now,
                UploadedByUser     = user,
            };

            _context.BookDbSet.Add(newBook);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 11
0
        public EpubItem(string path)
            : base(path)
        {
            this.FileFormat = FileFormat.Epub;

            var epubDoc = EpubReader.ReadBook(path);

            Author = epubDoc.Author;
            Title  = epubDoc.Title;
        }
Ejemplo n.º 12
0
        //public void AddOrUpdate<T>(this IBarrel barrel, string key, T value, TimeSpan timeSpan)
        //{
        //    if (barrel.Exists(key))
        //        barrel.Empty(key);
        //    barrel.Add<T>(key, value, timeSpan);
        //}

        public EpubBook ExtractEpubFromId(string assetId)
        {
            var asset    = Barrel.Current.Get <EpubAsset>(assetId);
            var epubBook = EpubReader.ReadBook(asset.Path);

            var tempFolder = BookEnvironment.GetTempLocalPathForEpub();

            ExtractAllFilesFromEpub(epubBook, tempFolder);

            return(epubBook);
        }
Ejemplo n.º 13
0
        public static bool ReadFile(string filePath)
        {
            if (File.Exists(filePath)) //check if any file is chosen
            {
                //Start read new file
                epub.filePath = filePath;
                epub.fileName = Path.GetFileNameWithoutExtension(epub.filePath);
                //Create "Hidden" library folder if not have yet
                if (!Directory.Exists(epub.library))
                {
                    DirectoryInfo di = Directory.CreateDirectory(epub.library);
                    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
                }
                epub.tempPath = Path.Combine(epub.library, epub.fileName);
                //Check valid epub type
                try
                {
                    epubBook = EpubReader.ReadBook(epub.filePath);
                }
                catch
                {
                    MessageBox.Show("Invalid epub file! Please choose another file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(false);
                }
                //Set up new epub file folder: stylesheet, bookmark, status
                epub.baseMenuXmlDiretory = Path.Combine(epub.tempPath, epubBook.Schema.ContentDirectoryPath);
                SetupNewEpub();
                Clear();
                //get stylesheet, status path

                epub.stylesheetPath    = Path.Combine(epub.baseMenuXmlDiretory, getStylesheetFileName());
                epub.readingStatusPath = Path.Combine(epub.baseMenuXmlDiretory, epub.statusFileName);
                //get menu
                foreach (EpubContentFile epubContent in epubBook.ReadingOrder)
                {
                    menuItems.Add(GetPath(epubContent.FileName));
                }
                //get table of contents
                getTableContent(epubBook.Navigation);
                setTableContent();
                //get epub.bookmarks
                getBookmarkFromFile(Path.GetFullPath(Path.Combine(epub.baseMenuXmlDiretory, epub.bookmarkFileName)));
                //get reading status
                getReadingStatus();
            }
            else
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 14
0
        public void ImportEpub(string filepath)
        {
            try
            {
                var book = EpubReader.ReadBook(filepath);
                if (book == null)
                {
                    LogTo.Error("Failed to import Epub. Book was null.");
                    return;
                }

                var parentId = CreateBookFolder(book);
                var parentEl = Svc.SM.Registry.Element[parentId];
                if (parentId < 1 || parentEl == null)
                {
                    LogTo.Debug("Failed to create book folder.");
                    return;
                }

                var unzipped = UnzipEpub(filepath);
                if (!Directory.Exists(unzipped))
                {
                    LogTo.Debug("Failed to unzip Epub - the unzipped folder does not exist.");
                    return;
                }

                var imageFolder = GetImagesFolder(book, unzipped);
                var htmlFolder  = GetHtmlFolder(book, unzipped);
                if (htmlFolder == null)
                {
                    LogTo.Debug("Failed to get the unzipped path to html.");
                    return;
                }

                foreach (var chapter in book.ReadingOrder)
                {
                    if (ImportChapter(parentEl, book, chapter, htmlFolder.FullName, imageFolder.FullName) == -1)
                    {
                        LogTo.Debug("Failed to import chapter.");
                        break;
                    }
                }
            }
            catch (IOException e)
            {
                LogTo.Debug($"Failed to import {filepath} with IO Exception {e}");
            }
        }
Ejemplo n.º 15
0
        ///////// FORM /////////
        public FRM_Visualiser(Livres _livre)
        {
            livre = _livre;
            InitializeComponent();
            EPubReader = new ClassEpubRead(livre.full_chemin_livre);
            EpubBook epubBook = EpubReader.ReadBook(livre.full_chemin_livre);

            foreach (EpubTextContentFile textContentFile in epubBook.ReadingOrder)
            {
                string htmlContent = textContentFile.Content;
                RTB_Test.Text = htmlContent;
            }
            if_fullscreen         = 0;
            displayText           = RTB_Test.Text;
            WB_Pages.DocumentText = "<html><div style='font-size:" + textSize + "px;width:" + (WB_Pages.Width - 55) + "px; margin-left:15px; margin-right:auto;'>" + displayText + "</div></html>";
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Конвертация файла из epub в pdf
        /// </summary>
        /// <param name="temp">Путь к временной папке для хранения промежуточных данных. После выхода из функции папка будет удалена</param>
        /// <param name="source">Путь к файлу</param>
        /// <param name="destination">Путь к сконверченному файлу</param>
        /// <param name="wkArgs">Аргументы для запуска wkhtmltopdf</param>
        /// <returns></returns>
        protected override bool ConvertInternal(string temp, string source, string destination, string wkArgs)
        {
            ZipFile.ExtractToDirectory(source, temp, true);
            var readBook = EpubReader.ReadBook(source);

            if (readBook.ReadingOrder.Count == 0)
            {
                throw new Exception("No pages for convert");
            }

            var parts = readBook.ReadingOrder
                        .Where(t => !string.Equals(Path.GetFileName(t.FileName), "TOC.xhtml", StringComparison.InvariantCultureIgnoreCase))
                        .Select(t => Path.Combine(temp, readBook.Schema.ContentDirectoryPath, t.FileName)).ToList();

            return(parts.Count != 0 && GeneratePdf(parts[0], parts.Skip(1).ToList(), destination, wkArgs));
        }
        protected override void OnAppearing()
        {
            var      assembly = typeof(epubreaderPage).GetTypeInfo().Assembly;
            Stream   stream   = assembly.GetManifestResourceStream("epubreader.Resources.conde.epub");
            EpubBook epubBook = EpubReader.ReadBook(stream);

            this.titleTxt.Text  = epubBook.Title;
            this.authorTxt.Text = epubBook.Author;

            var chaptersList = "";

            foreach (EpubChapter chapter in epubBook.Chapters)
            {
                // Title of chapter
                chaptersList += chapter.Title + " ";
            }

            this.chapterList.Text = chaptersList;
        }
Ejemplo n.º 18
0
        public static void ReadEpub(string path, string metaDirectory = null)
        {
            var directory = !string.IsNullOrWhiteSpace(metaDirectory) ?
                            metaDirectory :
                            Path.GetDirectoryName(path);
            var filename = Path.GetFileNameWithoutExtension(path);
            var metaCsv  = CreateMetaCSVFile(Path.Combine(directory, filename + "_meta.csv"));
            var imgsCsv  = CreateImgsCSVFile(Path.Combine(directory, filename + "_imgs.csv"));
            var info     = new FileInfo(path);
            var size     = info.Length;

            var book    = EpubReader.ReadBook(path);
            var content = book.Content;
            // All images in the book (file name is the key)
            var images      = content.Images;
            var imagesCount = images.Count;

            foreach (var imageContent in images.Values)
            {
                // Creating Image class instance from the content
                using (var stream = new MemoryStream(imageContent.Content))
                {
                    var image = Image.FromStream(stream);
                    imgsCsv.WriteLine($"{image.Width}, {image.Height}, {Image.GetPixelFormatSize(image.PixelFormat)}");
                }
            }

            // All XHTML files in the book (file name is the key)
            Dictionary <string, EpubTextContentFile> htmlFiles = content.Html;
            long wordCount = 0;

            // Entire HTML content of the book
            foreach (var htmlFile in htmlFiles.Values)
            {
                var html = htmlFile.Content;
                wordCount += WordCount(StripHTML(html));
            }

            metaCsv.WriteLine($"{wordCount}, {size}, {filename + ".epub"}, {imagesCount}");
            metaCsv.Close();
            imgsCsv.Close();
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Чтение книги с устройства
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async void LoadButtonClicked(object sender, EventArgs e)
        {
            try
            {
                //Допустимые форматы
                string[] types = new string[] { ".epub" };

                //Открываем проводник
                FileData fileData = await CrossFilePicker.Current.PickFile(allowedTypes : types);

                //Если ничего не выбрано, то просто возвращаем  null
                if (fileData == null)
                {
                    return;
                }

                //Создаем объект EpubBook
                EpubBook newBook = EpubReader.ReadBook(new MemoryStream(fileData.DataArray));

                //Добавляем в словарь
                books.Add(newBook.Title + Environment.NewLine + newBook.Author, newBook);

                //Берем путь и сохраняем его для того, чтобы после закрытия программы все книги отобразились вновь
                string path = fileData.FilePath;
                using (StreamWriter sw = new StreamWriter(filenameForBooks, true, System.Text.Encoding.Default))
                {
                    sw.WriteLine(path);
                }

                foreach (string book in Books.books.Keys)
                {
                    Goal.bookPicker.Items.Add(book);
                }
                //Создаём кнопку с книгой
                LoadNewBook(newBook);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка при выборе файла: " + ex.ToString());
            }
        }
Ejemplo n.º 20
0
        private void Epub()
        {
            nom = Path.GetFileName(ch.full_chemin_livre); //Nom fichier epub
            string extention  = System.IO.Path.GetFileNameWithoutExtension(nom) + ".zip";
            string pathString = biblio.CurrentPath + @"\copies\";

            nchemin = pathString + extention;
            if (File.Exists(pathString + nom))
            {
                //FRM_OriginalModifier Popup = new FRM_OriginalModifier();
                //Popup.ShowDialog();
                //charge = Popup.Choix();
                if (charge == 1)
                {
                    epubBook = EpubReader.ReadBook(ch.full_chemin_livre);
                }
                else
                {
                    epubBook = EpubReader.ReadBook(pathString + nom);
                }
            }
        }
Ejemplo n.º 21
0
        async void Button_Clicked(System.Object sender, System.EventArgs e)
        {
            var fileData = await CrossFilePicker.Current.PickFile();

            if (fileData == null)
            {
                return; // user canceled file picking
            }
            epubBook = EpubReader.ReadBook(fileData.FilePath);
            //SaveImages();
            Item.Text = epubBook.Title;

            try
            {
                var coverFileName = "";
                if (Device.RuntimePlatform == "UWP")
                {
                    coverFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "tempCover.jpg");
                }
                else
                {
                    coverFileName = Path.Combine(baseUrl, "tempCover.jpg");
                }
                File.WriteAllBytes(coverFileName, epubBook.CoverImage ?? epubBook.Content.Images.FirstOrDefault().Value.Content);
                this.CoverImage.Source = coverFileName;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            this.OnPropertyChanged("Item");

            //SaveHtmlCssFiles();
            contentService.ExtractAllFilesFromEpub(epubBook, BookEnvironment.GetTempLocalPathForEpub());

            renderHtml();
        }
Ejemplo n.º 22
0
        public string UploadFile(HttpPostedFileBase file, string category, string language)
        {
            var epubBook     = EpubReader.ReadBook(file.InputStream);
            var bookCategory = this.categories.EnsureCategory(category);
            var book         = this.epubParser.ParseEpubBook(epubBook, bookCategory);

            book.Language = language;

            string resultMessage;

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    MemoryStream target = new MemoryStream();
                    file.InputStream.CopyTo(target);
                    byte[] data = target.ToArray();

                    book.BackUpFile = data;

                    resultMessage = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    resultMessage = "ERROR:" + ex.Message.ToString();
                }
            }
            else
            {
                resultMessage = "You have not specified a file.";
            }

            this.books.Save();

            return(resultMessage);
        }
Ejemplo n.º 23
0
        private void Load_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Epub Files (*.epub)|*.epub"
            };
            var result = openFileDialog.ShowDialog();

            if (result == true)
            {
                // Opens a book and reads all of its content into memory
                epubBook = EpubReader.ReadBook(openFileDialog.FileName);

                // COMMON PROPERTIES

                // Book's title
                string title = epubBook.Title;

                // Book's authors (comma separated list)
                string author = epubBook.Author;

                // Book's authors (list of authors names)
                List <string> authors = epubBook.AuthorList;

                // Book's cover image (null if there is no cover)
                byte[] coverImageContent = epubBook.CoverImage;
                if (coverImageContent != null)
                {
                    using (MemoryStream coverImageStream = new MemoryStream(coverImageContent.ToArray()))
                    {
                        // Assign the Source property of your image
                        try
                        {
                            File.Delete(openFileDialog.FileName + ".jpg");
                            File.WriteAllBytes(openFileDialog.FileName + ".jpg", coverImageStream.ToArray());
                        }
                        catch
                        {
                            File.WriteAllBytes(openFileDialog.FileName + ".jpg", coverImageStream.ToArray());
                        }
                        BitmapImage imageSource = new BitmapImage(new Uri(@"C:/Users/shish/Downloads/1.jpg", UriKind.Absolute));
                        Image1.Source = imageSource;
                    }
                }
                Info.Text       = "Title: " + title + "\n" + "Author: " + author + "\n";
                Info.FontWeight = FontWeights.Bold;

                // CHAPTERS

                // Enumerating chapters
                foreach (EpubChapter chapter in epubBook.Chapters)
                {
                    // Title of chapter
                    string chapterTitle = chapter.Title;

                    // HTML content of current chapter
                    string chapterHtmlContent = chapter.HtmlContent;

                    // Nested chapters
                    List <EpubChapter> subChapters = chapter.SubChapters;

                    //PrintChapter(chapter);

                    //Chapters.Inlines.Add(new Run(chapterTitle + "\n"));

                    Chapters.Items.Add(chapterTitle);
                }
                // CONTENT

                // Book's content (HTML files, stlylesheets, images, fonts, etc.)
                EpubContent bookContent = epubBook.Content;


                // IMAGES

                // All images in the book (file name is the key)
                Dictionary <string, EpubByteContentFile> images = bookContent.Images;

                EpubByteContentFile firstImage = images.Values.First();

                // Content type (e.g. EpubContentType.IMAGE_JPEG, EpubContentType.IMAGE_PNG)
                EpubContentType contentType = firstImage.ContentType;

                // MIME type (e.g. "image/jpeg", "image/png")
                string mimeContentType = firstImage.ContentMimeType;

                // Creating Image class instance from the content
                using (MemoryStream imageStream = new MemoryStream(firstImage.Content))
                {
                    System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
                }


                // HTML & CSS

                // All XHTML files in the book (file name is the key)
                Dictionary <string, EpubTextContentFile> htmlFiles = bookContent.Html;

                // All CSS files in the book (file name is the key)
                Dictionary <string, EpubTextContentFile> cssFiles = bookContent.Css;

                // Entire HTML content of the book
                foreach (EpubTextContentFile htmlFile in htmlFiles.Values)
                {
                    string htmlContent = htmlFile.Content;
                }

                // All CSS content in the book
                foreach (EpubTextContentFile cssFile in cssFiles.Values)
                {
                    string cssContent = cssFile.Content;
                }


                // OTHER CONTENT

                // All fonts in the book (file name is the key)
                Dictionary <string, EpubByteContentFile> fonts = bookContent.Fonts;

                // All files in the book (including HTML, CSS, images, fonts, and other types of files)
                Dictionary <string, EpubContentFile> allFiles = bookContent.AllFiles;


                // ACCESSING RAW SCHEMA INFORMATION

                // EPUB OPF data
                EpubPackage package = epubBook.Schema.Package;

                // Enumerating book's contributors
                foreach (EpubMetadataContributor contributor in package.Metadata.Contributors)
                {
                    string contributorName = contributor.Contributor;
                    string contributorRole = contributor.Role;
                }

                // EPUB NCX data
                EpubNavigation navigation = epubBook.Schema.Navigation;

                // Enumerating NCX metadata
                foreach (EpubNavigationHeadMeta meta in navigation.Head)
                {
                    string metadataItemName    = meta.Name;
                    string metadataItemContent = meta.Content;
                }
            }
        }
Ejemplo n.º 24
0
 private void EpubToChapters(string ebookPath)
 {
     book         = EpubReader.ReadBook(ebookPath);
     contentFiles = book.ReadingOrder.ToList();
     Chapters     = new List <Chapter>();
     for (int i = 0; i < contentFiles.Count; i++)
     {
         Chapter  chapter     = new Chapter();
         bool     foundBody   = false;
         bool     firstLine   = true;
         string[] contentText = contentFiles[i].Content
                                .Replace("\r\n", "\n")
                                .Replace("</p>", "\n")
                                .Split('\n');
         foreach (string s in contentText)
         {
             string s1 = s;
             if (!foundBody)
             {
                 int pos = s1.IndexOf("<body>");
                 if (pos < 0)
                 {
                     pos = s1.IndexOf("<body ");
                 }
                 if (pos >= 0)
                 {
                     s1        = s1.Substring(pos); // remove everything before <body>
                     foundBody = true;
                 }
             }
             if (foundBody)
             {
                 s1 = s1.Replace("<i>", " _i1_ ");
                 s1 = Regex.Replace(s1, "<i [^>]*>", " _i1_ ");
                 s1 = s1.Replace("</i>", " _i0_ ");
                 s1 = s1.Replace("<em>", " _i1_ ");
                 s1 = Regex.Replace(s1, "<em [^>]*>", " _i1_ ");
                 s1 = s1.Replace("</em>", " _i0_ ");
                 s1 = Regex.Replace(s1, "<br[^>]*>", "\n_p_ ");
                 s1 = Regex.Replace(s1, "<[^>]*>", "").Trim();
                 s1 = Regex.Replace(s1, "   *", " ");
                 if (s1.Length > 0)
                 {
                     if (!firstLine)
                     {
                         s1 = "_p_ " + s1;
                     }
                     while (s1.Contains("\n"))
                     {
                         chapter.Paragraphs.Add(s1.Substring(0, s1.IndexOf("\n")));
                         s1 = s1.Substring(s1.IndexOf("\n") + 1);
                     }
                     chapter.Paragraphs.Add(s1);
                     if (firstLine)
                     {
                         chapter.Paragraphs.Add("");
                     }
                     firstLine = false;
                 }
             }
         }
         if (chapter.Paragraphs.Count > 0)
         {
             Chapters.Add(chapter);
         }
     }
 }
Ejemplo n.º 25
0
        private async void ImportFiles()
        {
            string path = Application.persistentDataPath + _sourceFolder;

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (IOException ex)
            {
                Debug.Log(ex.Message);
                return;
            }

            var files = FileBrowserHelpers.GetEntriesInDirectory(path);

            foreach (var file in files)
            {
                string fileName  = string.Empty;
                string filePath  = string.Empty;
                string imagePath = string.Empty;

                string   text      = string.Empty;
                string   finalText = string.Empty;
                byte[]   imageData = null;
                FileData newBook   = null;

                if (!file.IsDirectory)
                {
                    switch (file.Extension)
                    {
                    case ".fb2":
                        fileName = FileBrowserHelpers.GetFilename(file.Path);
                        text     = FileBrowserHelpers.ReadTextFromFile(file.Path);

                        FB2File fb2File = await new FB2Reader().ReadAsync(text);
                        var     lines   = await _fB2SampleConverter.ConvertAsync(fb2File);

                        finalText = _fB2SampleConverter.GetLinesAsText();

                        imageData = _fB2SampleConverter.GetCoverImageData();

                        if (imageData != null)
                        {
                            imagePath = Application.persistentDataPath + _targetFolder + fileName + ".jpg";;

                            try
                            {
                                File.WriteAllBytes(imagePath, imageData);

                                Debug.Log("Image is saved. Path: " + imagePath);
                            }
                            catch
                            {
                                Debug.LogError("Loading image is error!");
                            }

                            imagePath = imagePath.Replace("/", "\\");
                        }

                        filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\");

                        newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book);

                        SaveFile(newBook, finalText);

                        FileBrowserHelpers.DeleteFile(file.Path);
                        break;

                    case ".epub":
                        EpubBook epubFile = EpubReader.ReadBook(file.Path);

                        fileName = epubFile.Title + " (" + epubFile.Author + ")";

                        foreach (EpubTextContentFile textContentFile in epubFile.ReadingOrder)
                        {
                            HtmlDocument htmlDocument = new HtmlDocument();
                            htmlDocument.LoadHtml(textContentFile.Content);

                            StringBuilder sb = new StringBuilder();
                            foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()"))
                            {
                                sb.AppendLine(node.InnerText.Replace("\n", "").Replace("\r", ""));
                            }

                            finalText += sb.ToString();
                        }

                        imageData = epubFile.CoverImage;

                        var imageName = string.Empty;

                        if (imageData == null)
                        {
                            imageData = epubFile.Content.Images.FirstOrDefault().Value.Content;
                            imageName = epubFile.Content.Images.FirstOrDefault().Key;
                        }
                        else
                        {
                            imageName = epubFile.Content.Cover.FileName;
                        }

                        if (imageData != null)
                        {
                            imagePath = Application.persistentDataPath + _targetFolder + imageName;

                            try
                            {
                                File.WriteAllBytes(imagePath, imageData);

                                Debug.Log("Image is saved. Path: " + imagePath);
                            }
                            catch
                            {
                                Debug.LogError("Loading image is error!");
                            }

                            imagePath = imagePath.Replace("/", "\\");
                        }

                        filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\");

                        newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book);

                        SaveFile(newBook, finalText);

                        FileBrowserHelpers.DeleteFile(file.Path);
                        break;

                    case ".pdf":
                        var document = PdfiumViewer.PdfDocument.Load(file.Path);
                        var title    = document.GetInformation().Title;

                        if (FilesData.Files.Any(x => x.Name == title))
                        {
                            return;
                        }

                        fileName = FileBrowserHelpers.GetFilename(file.Path);
                        filePath = Application.persistentDataPath + _targetFolder + fileName;

                        imagePath = Application.persistentDataPath + _targetFolder + fileName.Remove(fileName.Length - 4) + ".png";

                        var image = document.Render(0, 72, 72, false);
                        image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);

                        imagePath = imagePath.Replace("/", "\\");
                        filePath  = filePath.Replace("/", "\\");

                        document.Dispose();

                        FileBrowserHelpers.MoveFile(file.Path, filePath);

                        newBook = new FileData(title, filePath, imagePath, FileData.FileType.PdfFile);

                        SaveFile(newBook);

                        break;
                    }
                }
                else
                {
                    FileBrowserHelpers.DeleteDirectory(file.Path);
                }
            }
        }
Ejemplo n.º 26
0
        private void EpubToChapters(string ebookPath)
        {
            book         = EpubReader.ReadBook(ebookPath);
            contentFiles = book.ReadingOrder.ToList();
            Chapters     = new List <Chapter>();
            for (int i = 0; i < contentFiles.Count; i++)
            {
                Chapter        chapter      = new Chapter();
                bool           foundBody    = false;
                bool           firstLine    = true;
                bool           secondLine   = false;
                bool           inTable      = false;
                bool           hasTableRows = false;
                int            tableDepth   = 0;
                bool           inComment    = false;
                bool           inBlockquote = false;
                string         imageFile;
                StringBuilder  currline  = new StringBuilder();
                Stack <string> spanStack = new Stack <string>();

                // split all the items into html tags and raw text
                string[] contentText = contentFiles[i].Content
                                       .Replace("\r", "")
                                       .Replace("</span>\n<span", "</span> <span")
                                       .Replace("\n", " ")                // fixes paragraphs across multiple lines
                                       .Replace(">", ">\n")
                                       .Replace("<", "\n<")
                                       .Split('\n');
                foreach (string s in contentText)
                {
                    if (s.Length == 0)
                    {
                        continue;
                    }
                    string s2;
                    if (s.Trim().StartsWith("<"))
                    {
                        s2 = s.Trim();
                    }
                    else
                    {
                        s2 = s;
                    }
                    if (s2.Contains("&nbsp;"))
                    {
                        s2 = s2.Replace("&nbsp;", " ");
                    }
                    if (s2.Contains("&#160;"))
                    {
                        s2 = s2.Replace("&#160;", " ");
                    }
                    if (s2.Contains($"{(char)160}"))
                    {
                        s2 = s2.Replace((char)160, ' ');
                    }
                    if (s2.Length == 0)
                    {
                        continue;
                    }
                    if (s2.Contains("_"))
                    {
                        s2 = s2.Replace("_", "&#95;");
                    }
                    if (inComment)
                    {
                        if (s2.Contains("-->"))
                        {
                            inComment = false;
                            int pos2 = s2.IndexOf("-->");
                            if (pos2 + 3 >= s2.Length)
                            {
                                continue;
                            }
                            s2 = s2.Substring(pos2 + 3);
                            if (s2.Length == 0)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (s2.Contains("<!--"))
                    {
                        inComment = true;
                        int pos  = s2.IndexOf("<!--");
                        int pos2 = s2.IndexOf("-->", pos);
                        if (pos2 > pos)
                        {
                            s2        = s2.Remove(pos, pos2 - pos + 3);
                            inComment = false;
                        }
                        else
                        {
                            s2 = s2.Substring(0, pos);
                        }
                        if (s2.Length == 0)
                        {
                            continue;
                        }
                    }
                    if (!s2.StartsWith("<"))
                    {
                        if (foundBody)
                        {
                            string s3 = FixText(s2);
                            if (currline.ToString().EndsWith("_t_") && s3.StartsWith(" "))
                            {
                                // change spaces to another _t_
                                currline.Append("_t_");
                                currline.Append(s2.TrimStart());
                            }
                            else
                            {
                                currline.Append(s3);
                            }
                        }
                        continue;
                    }
                    string tag = GetTag(s2);
                    switch (tag)
                    {
                    case "body":
                        foundBody = true;
                        break;

                    case "/body":
                        foundBody = false;
                        break;

                    case "blockquote":
                        if (inBlockquote)
                        {
                            // avoid nested blockquotes
                            continue;
                        }
                        if (inTable)
                        {
                            continue;
                        }
                        inBlockquote = true;
                        if (!currline.ToString().EndsWith("_t_"))
                        {
                            currline.Append("_t_");
                        }
                        break;

                    case "/blockquote":
                        if (!inBlockquote)
                        {
                            // avoid nested blockquotes
                            continue;
                        }
                        if (inTable)
                        {
                            continue;
                        }
                        inBlockquote = false;
                        if (!secondLine || currline.Length > 0)
                        {
                            chapter.Paragraphs.Add("_p_" + currline.ToString().Trim());
                            secondLine = false;
                        }
                        currline.Clear();
                        break;

                    case "/p":
                    case "/div":
                    case "br":
                    case "/li":
                    case "hr":
                    case "/ul":
                    case "/ol":
                    case "/h1":
                    case "/h2":
                    case "/h3":
                    case "/h4":
                    case "/h5":
                    case "/h6":
                        if (inTable)
                        {
                            continue;
                        }
                        if (firstLine)
                        {
                            string s4 = currline.ToString();
                            s4 = s4.Replace("_b1_", "");
                            s4 = s4.Replace("_b0_", "");
                            s4 = s4.Replace("_i1_", "");
                            s4 = s4.Replace("_i0_", "");
                            s4 = s4.Replace("_u1_", "");
                            s4 = s4.Replace("_u0_", "");
                            s4 = s4.Replace("_t_", "");
                            s4 = s4.Trim();
                            if (s4.Length == 0)
                            {
                                continue;
                            }
                            if (s4.StartsWith("_"))
                            {
                                chapter.Paragraphs.Add("###");
                                chapter.Paragraphs.Add("");
                                chapter.Paragraphs.Add("_p_" + s4);
                                secondLine = false;
                            }
                            else if (char.IsDigit(s4[0]) || s4.ToUpper().StartsWith("CHAPTER"))
                            {
                                chapter.Paragraphs.Add("###" + s4);
                                chapter.Paragraphs.Add("");
                                secondLine = true;
                            }
                            else if (s4.Length > 100)     // too long for chapter
                            {
                                chapter.Paragraphs.Add("###");
                                chapter.Paragraphs.Add("");
                                chapter.Paragraphs.Add("_p_" + s4);
                                secondLine = false;
                            }
                            else
                            {
                                chapter.Paragraphs.Add("###" + s4);
                                chapter.Paragraphs.Add("");
                                secondLine = true;
                            }

                            firstLine = false;
                        }
                        else if (tag == "hr")
                        {
                            if (currline.Length > 0)
                            {
                                chapter.Paragraphs.Add(currline.ToString().TrimEnd());
                            }
                            chapter.Paragraphs.Add("_p_---");
                            secondLine = false;
                        }
                        else if (tag == "/li" && currline.Length > 0)
                        {
                            chapter.Paragraphs.Add("_p__t_" + currline.ToString().Trim());
                            secondLine = false;
                        }
                        else if (!secondLine || currline.Length > 0)
                        {
                            chapter.Paragraphs.Add("_p_" + currline.ToString().Trim());
                            secondLine = false;
                        }
                        currline.Clear();
                        break;

                    case "image":
                        imageFile = s2.Substring(s2.IndexOf("href=\"") + 6);
                        imageFile = imageFile.Substring(0, imageFile.IndexOf("\""));
                        if (imageFile != "cover.jpeg")
                        {
                            currline.Append("_image:");
                            currline.Append(imageFile);
                            currline.Append("_");
                        }
                        break;

                    case "img":
                        imageFile = s2.Substring(s2.IndexOf("src=\"") + 5);
                        imageFile = imageFile.Substring(0, imageFile.IndexOf("\""));
                        if (imageFile != "cover.jpeg")
                        {
                            currline.Append("_image:");
                            currline.Append(imageFile);
                            currline.Append("_");
                            if (s2.IndexOf("alt=\"") >= 0)
                            {
                                string altTag = s2.Substring(s2.IndexOf("alt=\"") + 5);
                                altTag = altTag.Substring(0, altTag.IndexOf("\"")).Trim();
                                if (altTag.Length > 0)
                                {
                                    currline.Append("_imagealt:");
                                    currline.Append(altTag);
                                    currline.Append("_");
                                }
                            }
                        }
                        break;

                    case "span":
                        if (s2.Contains("\"bold\""))
                        {
                            spanStack.Push("b");
                            currline.Append("_b1_");
                        }
                        else if (s2.Contains("\"italic\""))
                        {
                            spanStack.Push("i");
                            currline.Append("_i1_");
                        }
                        else if (s2.Contains("\"underline\""))
                        {
                            spanStack.Push("u");
                            currline.Append("_u1_");
                        }
                        else
                        {
                            spanStack.Push("");
                        }
                        break;

                    case "/span":
                        string spanPop = spanStack.Pop();
                        if (spanPop == "b")
                        {
                            currline.Append("_b0_");
                        }
                        else if (spanPop == "i")
                        {
                            currline.Append("_i0_");
                        }
                        else if (spanPop == "u")
                        {
                            currline.Append("_u0_");
                        }
                        break;

                    case "li":
                        int pos = s2.IndexOf("value=\"");
                        if (pos >= 0)
                        {
                            pos += 7;
                            int pos2 = s2.IndexOf("\"", pos);
                            currline.Append(s2.Substring(pos, pos2 - pos));
                            currline.Append(": ");
                        }
                        else
                        {
                            currline.Append("* ");     // no list value found
                        }
                        break;

                    case "table":
                        inTable = true;
                        if (tableDepth == 0)
                        {
                            hasTableRows = false;
                        }
                        tableDepth++;
                        if (!hasTableRows)
                        {
                            break;
                        }
                        if (currline.Length > 0)
                        {
                            chapter.Paragraphs.Add(currline.ToString());
                            currline.Clear();
                        }
                        chapter.Paragraphs.Add("_p__table1_");
                        break;

                    case "/table":
                        tableDepth--;
                        if (tableDepth <= 0)
                        {
                            tableDepth = 0;
                            inTable    = false;
                        }
                        if (!hasTableRows)
                        {
                            break;
                        }
                        if (currline.Length > 0)
                        {
                            chapter.Paragraphs.Add(currline.ToString());
                            currline.Clear();
                        }
                        chapter.Paragraphs.Add("_p__table0_");
                        break;

                    case "tr":
                        if (!inTable)
                        {
                            Console.WriteLine("<tr> outside table error");
                            break;
                        }
                        if (!hasTableRows)
                        {
                            if (currline.Length > 0)
                            {
                                chapter.Paragraphs.Add(currline.ToString());
                                currline.Clear();
                            }
                            for (int trI = 0; trI < tableDepth; trI++)
                            {
                                chapter.Paragraphs.Add("_p__table1_");
                            }
                            hasTableRows = true;
                        }
                        currline.Append("_p__tr1_");
                        break;

                    case "/tr":
                        if (!inTable)
                        {
                            Console.WriteLine("</tr> outside table error");
                            break;
                        }
                        currline.Append("_tr0_");
                        chapter.Paragraphs.Add(currline.ToString());
                        currline.Clear();
                        break;

                    case "caption":
                        if (!inTable)
                        {
                            Console.WriteLine("<caption> outside table error");
                            break;
                        }
                        currline.Append("_p__caption1_");
                        break;

                    case "/caption":
                        if (!inTable)
                        {
                            Console.WriteLine("</caption> outside table error");
                            break;
                        }
                        currline.Append("_caption0_");
                        chapter.Paragraphs.Add(currline.ToString());
                        currline.Clear();
                        break;

                    case "tt":
                        currline.Append("_code1_");
                        break;

                    case "/tt":
                        currline.Append("_code0_");
                        break;

                    case "em":
                        currline.Append("_i1_");
                        break;

                    case "/em":
                        currline.Append("_i0_");
                        break;

                    case "strong":
                        currline.Append("_b1_");
                        break;

                    case "/strong":
                        currline.Append("_b0_");
                        break;

                    case "i":
                    case "/i":
                    case "b":
                    case "/b":
                    case "s":
                    case "/s":
                    case "u":
                    case "/u":
                    case "sup":
                    case "/sup":
                    case "sub":
                    case "/sub":
                    case "small":
                    case "/small":
                        // on-off tag pairs
                        currline.Append("_");
                        if (tag.StartsWith("/"))
                        {
                            currline.Append(tag.Substring(1));
                            currline.Append("0");     // off
                        }
                        else
                        {
                            currline.Append(tag);
                            currline.Append("1");     // on
                        }
                        currline.Append("_");
                        break;

                    case "th":
                    case "/th":
                    case "td":
                    case "/td":
                        if (!inTable)
                        {
                            Console.WriteLine($"<{tag}> outside table error");
                            break;     // ignore all these if not in table
                        }
                        // on-off tag pairs
                        if (currline.Length == 0)
                        {
                            currline.Append("_p_");
                        }
                        currline.Append("_");
                        if (tag.StartsWith("/"))
                        {
                            currline.Append(tag.Substring(1));
                            currline.Append("0");     // off
                        }
                        else
                        {
                            currline.Append(tag);
                            currline.Append("1");     // on
                        }
                        currline.Append("_");
                        break;

                    case "div":
                    case "a":
                    case "/a":
                    case "ul":
                    case "ol":
                    case "svg":
                    case "/svg":
                    case "h1":
                    case "h2":
                    case "h3":
                    case "h4":
                    case "h5":
                    case "h6":
                    case "col":
                    case "colgroup":
                    case "/colgroup":
                    case "section":
                    case "/section":
                    case "big":
                    case "/big":
                    case "nav":
                    case "/nav":
                        // ignore all these
                        break;

                    case "p":
                        break;

                    default:
                        if (foundBody)
                        {
                            currline.Append("<");
                            currline.Append(tag);
                            currline.Append(">");
                        }
                        break;
                    }
                }
                if (currline.ToString().Trim().Length > 0)
                {
                    if (firstLine)
                    {
                        string s4 = currline.ToString().Trim();
                        s4 = s4.Replace("_b1_", "");
                        s4 = s4.Replace("_b0_", "");
                        s4 = s4.Replace("_i1_", "");
                        s4 = s4.Replace("_i0_", "");
                        s4 = s4.Replace("_u1_", "");
                        s4 = s4.Replace("_u0_", "");
                        s4 = s4.Replace("_t_", "");
                        s4 = s4.Trim();
                        if (s4.StartsWith("_"))
                        {
                            chapter.Paragraphs.Add("###");
                            chapter.Paragraphs.Add("");
                            chapter.Paragraphs.Add("_p_" + s4);
                        }
                        else if (s4.Contains("_"))
                        {
                            chapter.Paragraphs.Add("###" + s4.Substring(0, s4.IndexOf("_")).Trim());
                            chapter.Paragraphs.Add("");
                            chapter.Paragraphs.Add("_p_" + s4.Substring(s4.IndexOf("_")).Trim());
                        }
                        else
                        {
                            chapter.Paragraphs.Add("###" + s4);
                            chapter.Paragraphs.Add("");
                        }
                    }
                    else
                    {
                        chapter.Paragraphs.Add(currline.ToString().Trim());
                    }
                }
                if (chapter.Paragraphs.Count > 0 &&
                    !chapter.Paragraphs[0].ToLower().Contains("contents"))
                {
                    while (chapter.Paragraphs.Count >= 3 &&
                           chapter.Paragraphs[2] == "_p_")
                    {
                        chapter.Paragraphs.RemoveAt(2);
                    }
                    while (chapter.Paragraphs.Count > 0 &&
                           (chapter.Paragraphs[chapter.Paragraphs.Count - 1].Length == 0 ||
                            chapter.Paragraphs[chapter.Paragraphs.Count - 1] == "_p_")
                           )
                    {
                        chapter.Paragraphs.RemoveAt(chapter.Paragraphs.Count - 1);
                    }
                    if (chapter.Paragraphs.Count > 0)
                    {
                        if (chapter.Paragraphs[0].Contains("_b"))
                        {
                            chapter.Paragraphs[0] = chapter.Paragraphs[0].Replace("_b1_", "").Replace("_b0_", "");
                        }
                        if (chapter.Paragraphs[0].Contains("_i"))
                        {
                            // could be _image_, but will be ignored
                            chapter.Paragraphs[0] = chapter.Paragraphs[0].Replace("_i1_", "").Replace("_i0_", "");
                        }
                        if (chapter.Paragraphs[0].Contains("_u"))
                        {
                            chapter.Paragraphs[0] = chapter.Paragraphs[0].Replace("_u1_", "").Replace("_u0_", "");
                        }
                        for (int para = chapter.Paragraphs.Count - 1; para > 1; para--)
                        {
                            if (chapter.Paragraphs[para] == "_p_" && chapter.Paragraphs[para - 1] == "_p_")
                            {
                                chapter.Paragraphs.RemoveAt(para);
                            }
                        }
                        Chapters.Add(chapter);
                    }
                }
            }
        }
Ejemplo n.º 27
0
        public (HtmlToFlowDocument.Dom.FlowDocument Document, Dictionary <string, string> FontDictionary) OpenEbook(string fileName)
        {
            var epubBook = EpubReader.ReadBook(fileName);

            _bookContent = epubBook.Content;

            Dictionary <string, EpubTextContentFile> htmlFiles = _bookContent.Html;
            Dictionary <string, EpubTextContentFile> cssFiles  = _bookContent.Css;
            var readingOrder = epubBook.ReadingOrder;

            // ----------------- handle fonts ------------------------------
            var fontDictionary = new Dictionary <string, string>(); // Key is the font name, value is the absolute path to the font file
            var fontPath       = Path.Combine(_instanceStorageService.InstanceStoragePath, "Fonts");

            Directory.CreateDirectory(fontPath);

            foreach (var entry in _bookContent.Fonts)
            {
                var fontName     = entry.Key;
                var bytes        = entry.Value;
                var fontFileName = Path.GetFileName(entry.Value.FileName);
                fontFileName = Path.Combine(fontPath, fontFileName);
                using (var stream = new FileStream(fontFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    var byteArray = bytes.Content;
                    stream.Write(byteArray, 0, byteArray.Length);
                }
                fontDictionary.Add(fontName, fontFileName);
            }

            // -------------------------------------------------------------

            string GetStyleSheet(string name, string htmlFileNameReferencedFrom)
            {
                EpubTextContentFile cssFile;
                // calculate absolute name with reference to htmlFileNameReferencedFrom
                var absoluteName = HtmlToFlowDocument.CssStylesheets.GetAbsoluteFileNameForFileRelativeToHtmlFile(name, htmlFileNameReferencedFrom);

                if (cssFiles.TryGetValue(absoluteName, out cssFile))
                {
                    return(cssFile.Content);
                }

                // if this could not resolve the name, then try to go to parent directories
                while (htmlFileNameReferencedFrom.Contains("/"))
                {
                    var idx = htmlFileNameReferencedFrom.LastIndexOf("/");
                    htmlFileNameReferencedFrom = htmlFileNameReferencedFrom.Substring(0, idx - 1);
                    absoluteName = HtmlToFlowDocument.CssStylesheets.GetAbsoluteFileNameForFileRelativeToHtmlFile(name, htmlFileNameReferencedFrom);
                    if (cssFiles.TryGetValue(absoluteName, out cssFile))
                    {
                        return(cssFile.Content);
                    }
                }

                // if this was not successful, then try it with the name alone
                if (cssFiles.TryGetValue(name, out cssFile))
                {
                    return(cssFile.Content);
                }

                return(null);
                // throw new ArgumentException($"CssFile {name} was not found!", nameof(name));
            }

            // Entire HTML content of the book
            var converter = new HtmlToFlowDocument.Converter()
            {
                AttachSourceAsTags = true
            };
            var flowDocument = new HtmlToFlowDocument.Dom.FlowDocument();

            foreach (EpubTextContentFile htmlFile in readingOrder)
            {
                string htmlContent = htmlFile.Content;
                var    textElement = converter.ConvertXHtml(htmlContent, false, GetStyleSheet, htmlFile.FileName); // create sections
                flowDocument.AppendChild(textElement);                                                             // and add them to the flow document
            }
            Settings.BookSettings.BookFileName = fileName;
            return(flowDocument, fontDictionary);
        }
Ejemplo n.º 28
0
        static void Main(string[] args)
        {
            string directory = AppDomain.CurrentDomain.BaseDirectory;

            string epubDirectory   = string.Format(@"{0}\{1}", Directory.GetParent(directory).Parent.Parent.FullName, "epubs");
            string targetDirectory = string.Format(@"{0}\{1}", Directory.GetParent(directory).Parent.Parent.FullName, "books");

            if (Directory.Exists(epubDirectory))
            {
                Console.WriteLine("Directorio existe");
                string[] bookPaths = Directory.GetFiles(epubDirectory, "*.epub");
                if (bookPaths.Length > 0)
                {
                    List <Book> books = new List <Book>();

                    Console.WriteLine("Existen Libros en el directorio");
                    foreach (string str in bookPaths)
                    {
                        EpubBook book     = EpubReader.ReadBook(str);
                        Book     readBook = new Book()
                        {
                            Author = book.Author,
                            Title  = book.Title,
                            Files  = new List <Pages>()
                        };

                        foreach (KeyValuePair <string, EpubTextContentFile> file in book.Content.Html)
                        {
                            addFile(file, readBook);
                        }

                        books.Add(readBook);
                    }

                    for (int i = 0; i < books.Count; i++)
                    {
                        string name = (i + 1).ToString();
                        if (!Directory.Exists(targetDirectory))
                        {
                            Directory.CreateDirectory(targetDirectory);
                        }
                        string filename  = string.Format(@"{0}.xml", name);
                        string subfolder = string.Format(@"{0}\{1}\", targetDirectory, name);

                        using (StreamWriter sw = File.CreateText(string.Format(@"{0}\{1}", targetDirectory, filename)))
                        {
                            sw.WriteLine("<book>");
                            sw.WriteLine("<title>" + books[i].Title + "</title>");
                            sw.WriteLine("<author>" + books[i].Author + "</author>");
                            sw.WriteLine("<pages>" + (books[i].Files.Count + 1).ToString() + "</pages>");
                            sw.Write("</book>");
                        };

                        Directory.CreateDirectory(subfolder);

                        for (int j = 0; j < books[i].Files.Count; j++)
                        {
                            string pagename = (j + 1).ToString();
                            using (StreamWriter sw = File.CreateText(string.Format("{0}{1}.txt", subfolder, pagename)))
                            {
                                sw.Write(books[i].Files[j].textcontent);
                            };

                            using (StreamWriter sw = File.CreateText(string.Format("{0}{1}.html", subfolder, pagename)))
                            {
                                sw.Write(books[i].Files[j].htmlcontent);
                            };
                        }
                    }
                }
            }
            Console.WriteLine("Proceso Finalizado");
            Console.ReadKey();
        }
Ejemplo n.º 29
0
        public async Task ProcessMessageAsync(SQSEvent.SQSMessage message)
        {
            ParseEpubContract parseEpubContract =
                JsonConvert.DeserializeObject <ParseEpubContract>(message.Body);

            string bucketName = Environment.GetEnvironmentVariable("ALEXA_READER_BUCKET");
            string filePath   = $"{parseEpubContract.FolderName}/{parseEpubContract.FileName}";

            Stream fileStream = AwsService.S3.GetObject(filePath, bucketName);

            // Opens a book and reads all of its content into memory
            EpubBook epubBook = EpubReader.ReadBook(fileStream);

            Book book = new Book();

            book.Uuid         = Guid.NewGuid().ToString();
            book.Title        = epubBook.Title;
            book.Chapters     = new List <Chapter>();
            book.Owner        = parseEpubContract.User;
            book.EpubFilePath = filePath;

            List <string> chaptersToSkip = new List <string>
            {
                "index",
                "preface",
                "glossary",
                "quick glossary"
            };

            List <EpubNavigationItem> validChapters = epubBook.Navigation
                                                      .Where(c => !chaptersToSkip.Contains(c.Title.ToLower()))
                                                      .ToList();

            List <ConvertTextToSpeechContract> convertTextToSpeechContracts =
                new List <ConvertTextToSpeechContract>();

            foreach (EpubNavigationItem epubChapter in validChapters)
            {
                Chapter chapter = new Chapter();
                chapter.Uuid        = Guid.NewGuid().ToString();
                chapter.Title       = epubChapter.Title;
                chapter.Subchapters = new List <Subchapter>();

                // Nested chapters
                List <EpubNavigationItem> subChapters = epubChapter.NestedItems;

                foreach (var subChapter in subChapters)
                {
                    EpubTextContentFile content = subChapter.HtmlContentFile;

                    string stripped = StripHTML(content.Content);

                    string id            = Guid.NewGuid().ToString();
                    string audioFilePath = $"{parseEpubContract.FolderName}/{id}.mp3";

                    chapter.Subchapters.Add(new Subchapter
                    {
                        AudioFilePath       = audioFilePath,
                        Uuid                = id,
                        CurrentTimePosition = 0
                    });

                    convertTextToSpeechContracts.Add(new ConvertTextToSpeechContract
                    {
                        TextContent         = stripped,
                        AudioFilePathToSave = audioFilePath,
                        Owner = parseEpubContract.User
                    });
                }

                chapter.CurrentSubchapterId = chapter.Subchapters.FirstOrDefault()?.Uuid;
                book.Chapters.Add(chapter);
            }

            book.CurrentChapterId = book.Chapters.FirstOrDefault()?.Uuid;

            string queueUrl       = Environment.GetEnvironmentVariable("CONVERSION_QUEUE_URL");
            string messageGroupId = Guid.NewGuid().ToString();

            List <SendMessageBatchRequestEntry> messages = new List <SendMessageBatchRequestEntry>();

            Action <ConvertTextToSpeechContract> addMessageToSend = (contract) =>
            {
                string messageBody            = JsonConvert.SerializeObject(contract);
                string messageDeduplicationId = Guid.NewGuid().ToString();

                messages.Add(new SendMessageBatchRequestEntry
                {
                    Id                     = messageDeduplicationId,
                    MessageBody            = messageBody,
                    MessageGroupId         = messageGroupId,
                    MessageDeduplicationId = messageDeduplicationId
                });
            };

            convertTextToSpeechContracts
            .Take(convertTextToSpeechContracts.Count - 1)
            .ToList()
            .ForEach(contract => addMessageToSend(contract));

            ConvertTextToSpeechContract last = convertTextToSpeechContracts.Last();

            last.NotifyOwner = true;
            addMessageToSend(last);

            List <List <SendMessageBatchRequestEntry> > messageGroups = SplitList(messages, 10).ToList();

            messageGroups.ForEach(messageGroup =>
            {
                AwsService.SQS.SendMessageBatch(messageGroup, queueUrl);
            });

            var synteshisRequest = new SynthesizeSpeechRequest
            {
                Engine       = Engine.Neural,
                OutputFormat = "mp3",
                //SampleRate = "8000",
                Text         = txtContent,
                TextType     = "text",
                VoiceId      = VoiceId.Joanna,
                LanguageCode = LanguageCode.EnUS
            };

            var client = new AmazonPollyClient(RegionEndpoint.USEast1);

            var task = client.SynthesizeSpeechAsync(synteshisRequest);

            task.Wait();
            var response = task.Result;

            //Console.WriteLine($"Synthetized {response.RequestCharacters} caracthers");

            //// COMMON PROPERTIES

            //// Book's title
            //string title = epubBook.Title;

            //// Book's authors (comma separated list)
            //string author = epubBook.Author;

            //// Book's authors (list of authors names)
            //List<string> authors = epubBook.AuthorList;

            //// Book's cover image (null if there is no cover)
            //byte[] coverImageContent = epubBook.CoverImage;
            //if (coverImageContent != null)
            //{
            //    using (MemoryStream coverImageStream = new MemoryStream(coverImageContent))
            //    {
            //        Image coverImage = Image.FromStream(coverImageStream);
            //    }
            //}

            //// TABLE OF CONTENTS

            //// Enumerating chapters
            //foreach (EpubNavigationItem chapter in epubBook.Navigation)
            //{
            //    // Title of chapter
            //    string chapterTitle = chapter.Title;

            //    // Nested chapters
            //    List<EpubNavigationItem> subChapters = chapter.NestedItems;
            //}

            //// READING ORDER

            //// Enumerating the whole text content of the book in the order of reading
            //foreach (EpubTextContentFile textContentFile in book.ReadingOrder)
            //{
            //    // HTML of current text content file
            //    string htmlContent = textContentFile.Content;
            //}


            //// CONTENT

            //// Book's content (HTML files, stlylesheets, images, fonts, etc.)
            //EpubContent bookContent = epubBook.Content;


            //// IMAGES

            //// All images in the book (file name is the key)
            //Dictionary<string, EpubByteContentFile> images = bookContent.Images;

            //EpubByteContentFile firstImage = images.Values.First();

            //// Content type (e.g. EpubContentType.IMAGE_JPEG, EpubContentType.IMAGE_PNG)
            //EpubContentType contentType = firstImage.ContentType;

            //// MIME type (e.g. "image/jpeg", "image/png")
            //string mimeType = firstImage.ContentMimeType;

            //// Creating Image class instance from the content
            //using (MemoryStream imageStream = new MemoryStream(firstImage.Content))
            //{
            //    Image image = Image.FromStream(imageStream);
            //}

            //// Cover metadata
            //if (bookContent.Cover != null)
            //{
            //    string coverFileName = bookContent.Cover.FileName;
            //    EpubContentType coverContentType = bookContent.Cover.ContentType;
            //    string coverMimeType = bookContent.Cover.ContentMimeType;
            //}

            //// HTML & CSS

            //// All XHTML files in the book (file name is the key)
            //Dictionary<string, EpubTextContentFile> htmlFiles = bookContent.Html;

            //// All CSS files in the book (file name is the key)
            //Dictionary<string, EpubTextContentFile> cssFiles = bookContent.Css;

            //// Entire HTML content of the book
            //foreach (EpubTextContentFile htmlFile in htmlFiles.Values)
            //{
            //    string htmlContent = htmlFile.Content;
            //}

            //// All CSS content in the book
            //foreach (EpubTextContentFile cssFile in cssFiles.Values)
            //{
            //    string cssContent = cssFile.Content;
            //}


            //// OTHER CONTENT

            //// All fonts in the book (file name is the key)
            //Dictionary<string, EpubByteContentFile> fonts = bookContent.Fonts;

            //// All files in the book (including HTML, CSS, images, fonts, and other types of files)
            //Dictionary<string, EpubContentFile> allFiles = bookContent.AllFiles;


            //// ACCESSING RAW SCHEMA INFORMATION

            //// EPUB OPF data
            //EpubPackage package = epubBook.Schema.Package;

            //// Enumerating book's contributors
            //foreach (EpubMetadataContributor contributor in package.Metadata.Contributors)
            //{
            //    string contributorName = contributor.Contributor;
            //    string contributorRole = contributor.Role;
            //}

            //// EPUB 2 NCX data
            //Epub2Ncx epub2Ncx = epubBook.Schema.Epub2Ncx;

            //// Enumerating EPUB 2 NCX metadata
            //foreach (Epub2NcxHeadMeta meta in epub2Ncx.Head)
            //{
            //    string metadataItemName = meta.Name;
            //    string metadataItemContent = meta.Content;
            //}

            //// EPUB 3 navigation
            //Epub3NavDocument epub3NavDocument = epubBook.Schema.Epub3NavDocument;

            //// Accessing structural semantics data of the head item
            //StructuralSemanticsProperty? ssp = epub3NavDocument.Navs.First().Type;
        }
Ejemplo n.º 30
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: ");
                Console.WriteLine("  " + System.AppDomain.CurrentDomain.FriendlyName + " <FILENAME> <APIKEY>");
                Environment.Exit(1);
            }
            string bookfile = args[0];
            string apiKey   = args[1];

            Console.WriteLine("Analyzing book: " + bookfile);
            EpubBook epubBook = EpubReader.ReadBook(bookfile);

            string title  = epubBook.Title;
            string author = epubBook.Author;

            Console.WriteLine("Book title: " + title);
            Console.WriteLine();

            double bookScore   = 0.0;
            int    numChapters = 0;

            foreach (EpubChapter chapter in epubBook.Chapters)
            {
                var request = new SentimentRequest();

                string chapterTitle = chapter.Title;

                AppendChapter(ref request, chapter);

                foreach (EpubChapter subChapter in chapter.SubChapters)
                {
                    AppendChapter(ref request, subChapter);
                }

                var client   = new SentimentClient(apiKey);
                var response = client.GetSentiment(request);

                foreach (Microsoft.ProjectOxford.Text.Core.DocumentError e in response.Errors)
                {
                    Console.WriteLine("Errors: " + e.Message);
                }

                double score     = 0.0;
                int    numScores = 0;

                foreach (SentimentDocumentResult r in  response.Documents)
                {
                    score += r.Score;
                    numScores++;
                }

                score /= numScores;

                Console.WriteLine(numChapters + ": " + chapterTitle + ", score: " + score);

                bookScore += score;
                numChapters++;
            }

            bookScore /= numChapters;

            Console.WriteLine();
            Console.WriteLine("Average book sentiment: " + bookScore);
        }