예제 #1
0
        protected virtual List <KeyValuePair <string, byte[]> > FixImages(HtmlNode div)
        {
            var imgNodes = div.Descendants("img");// .SelectNodes("//img");
            var images   = new List <KeyValuePair <string, byte[]> >();

            foreach (var node in imgNodes)
            {
                var imagePath = node.GetAttributeValue("data-original", "");
                if (string.IsNullOrEmpty(imagePath))
                {
                    imagePath = node.GetAttributeValue("src", "");
                }

                var imageFile = System.IO.Path.GetFileName(imagePath);

                if (!FileNameSanitizer.IsBadName(imageFile))
                {
                    var    imageBytesTask = webber.DownloadFile(imagePath);
                    byte[] imageBytes     = null;

                    try
                    {
                        imageBytes = imageBytesTask.Result;

                        if (imageBytesTask.Status != System.Threading.Tasks.TaskStatus.Faulted)
                        {
                            images.Add(new KeyValuePair <string, byte[]>(imageFile, imageBytes));
                        }
                        node.SetAttributeValue("src", imageFile); // modify the name in source
                    }
                    catch (System.AggregateException ex)
                    {
                        System.Console.WriteLine(ex);
                        // node.RemoveChild(node);
                    }
                    finally
                    {
                    }
                }
            }

            return(images);
        }
예제 #2
0
        // include 3 files: html (content), opf (main file) and ncx (content file)
        // http://www.aliciaramirez.com/2014/05/how-to-make-a-kindle-ebook-from-scratch/
        public string CreateKindleFiles(string firstpageUrl, bool forNote = false)
        {
            // 1. get the book base on website structure
            book = website.CheckBookDownloaded(firstpageUrl);

            // 2. assign 3 file names
            var title = VietnameseAccentsRemover.RemoveSign4VietnameseString(book.Title);

            title = title.Replace(" ", "_");
            title = FileNameSanitizer.GiveGoodName(title).Trim();

            if (string.IsNullOrEmpty(title))
            {
                return(string.Empty);
            }

            var downloadFolder = "";
            var trashFolder    = "";

            // get os type and set download path and kindle file
            if (Environment.GetEnvironmentVariable("_system_name") == "OSX")
            {
                trashFolder    = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".Trash");
                downloadFolder = Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Downloads");
                KindlegenPath  = "/Users/kiettran/Downloads/kindlegen";
                CalibrePath    = "/Applications/calibre.app/Contents/MacOS/calibredb";
            }
            else // windows
            {
                CalibrePath = @"C:\Program Files (x86)\Calibre2\calibredb.exe";



#if DEBUG
                trashFolder    = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Downloads");
                downloadFolder = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Downloads");
                // Logger logger = NLog.LogManager.GetCurrentClassLogger();
                // logger.Info($"-------------DOWNLOAD FOLDER IS: {downloadFolder} --------------");
                KindlegenPath = @"C:\Kiet\Kinh\kindlegen.exe";
#else
                var today = System.DateTime.Today.ToString("yyyy-MM-dd-ddd");
                trashFolder    = Path.Combine(@"C:\Kiet\Kinh\", today);
                downloadFolder = trashFolder;

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

                // Logger logger = NLog.LogManager.GetCurrentClassLogger();
                // logger.Info($"-------------DOWNLOAD FOLDER IS: {downloadFolder} --------------");
                KindlegenPath = @"C:\Kiet\Kinh\kindlegen.exe";
                // if release
#endif
            }

            // in case we use Website project to reference to it
            if (!string.IsNullOrEmpty(DownloadFolder))
            {
                downloadFolder = DownloadFolder;
            }

            // then download a whole book
            book = website.GetOneWholeHtml(firstpageUrl);

            // clean up double quote in file name
            title        = title.Replace("\"", "_");
            htmlFilename = Path.Combine(trashFolder, title + ".html");

            ncxFilename      = Path.Combine(Path.GetDirectoryName(htmlFilename), Path.GetFileNameWithoutExtension(htmlFilename) + ".ncx");
            opfBookFilename  = Path.Combine(Path.GetDirectoryName(htmlFilename), Path.GetFileNameWithoutExtension(htmlFilename) + ".opf");
            mobiBookFilename = Path.Combine(Path.GetDirectoryName(htmlFilename), Path.GetFileNameWithoutExtension(htmlFilename) + ".mobi");

            var downloadMobiFileName = Path.Combine(downloadFolder, title) + ".mobi";

            // check if mobi file exists, then skip it
            if (File.Exists(downloadMobiFileName))
            {
                return(downloadMobiFileName);
            }

            // 3. create 3 files
            if (!forNote)
            {
                CreateHtmlFile();
                CreateNCXTableOfContent();
            }
            else
            {
                // for note
                CreateHtmlFileFromNoteHtml();
                CreateNCXTableOfContentForNoteHtml();
            }

            CreateOPFBookDetail();

            // 4. Create mobile kindle file
            if (!string.IsNullOrWhiteSpace(KindlegenPath))
            {
                CreateKindleFile();
                AddFileToCalibre();
                // we don't use these files, delete it for now
                htmlFilename    = Path.Combine(trashFolder, htmlFilename);
                ncxFilename     = Path.Combine(trashFolder, ncxFilename);
                opfBookFilename = Path.Combine(trashFolder, opfBookFilename);

                if (File.Exists(htmlFilename))
                {
                    File.Delete(htmlFilename);
                }
                if (File.Exists(ncxFilename))
                {
                    File.Delete(ncxFilename);
                }
                if (File.Exists(opfBookFilename))
                {
                    File.Delete(opfBookFilename);
                }

                foreach (var image in book.Chapters.SelectMany(c => c.Images))
                {
                    var imageFilename = Path.Combine(trashFolder, image.Key);
                    if (File.Exists(imageFilename))
                    {
                        File.Delete(imageFilename);
                    }
                }

                //var trashMobiFileName = Path.Combine(trashFolder, title) + ".mobi";


                if (mobiBookFilename != downloadMobiFileName) // on Windows, we don't need to copy because I didn't check for trash folder
                {
                    if (File.Exists(downloadMobiFileName))
                    {
                        File.Delete(downloadMobiFileName);
                    }
                    if (File.Exists(mobiBookFilename))
                    {
                        File.Move(mobiBookFilename, downloadMobiFileName);
                    }
                }
            }

            return(Path.Combine(downloadFolder, title) + ".mobi");
        }
예제 #3
0
        // include 3 files: html (content), opf (main file) and ncx (content file)
        // http://www.aliciaramirez.com/2014/05/how-to-make-a-kindle-ebook-from-scratch/
        public string CreateKindleFiles(string firstpageUrl, bool forNote = false)
        {
            // 1. get the book base on website structure
            book = website.GetOneWholeHtml(firstpageUrl);
            // 2. assign 3 file names
            var title = VietnameseAccentsRemover.RemoveSign4VietnameseString(book.Title);

            title = title.Replace(" ", "_");
            title = FileNameSanitizer.GiveGoodName(title).Trim();

            if (string.IsNullOrEmpty(title))
            {
                return(string.Empty);
            }

            var downloadFolder = "";
            var trashFolder    = "";

            // get os type and set download path and kindle file
            if (Environment.GetEnvironmentVariable("_system_name") == "OSX")
            {
                trashFolder    = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".Trash");
                downloadFolder = Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Downloads");
                KindlegenPath  = "/Users/kiettran/Downloads/kindlegen";
            }
            else // windows
            {
                trashFolder    = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Downloads");
                downloadFolder = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Downloads");
                KindlegenPath  = @"C:\Kiet\Kinh\kindlegen.exe";
            }

            // in case we use Website project to reference to it
            if (!string.IsNullOrEmpty(DownloadFolder))
            {
                downloadFolder = DownloadFolder;
            }

            htmlFilename = Path.Combine(trashFolder, title) + ".html";

            ncxFilename     = Path.Combine(Path.GetDirectoryName(htmlFilename), Path.GetFileNameWithoutExtension(htmlFilename) + ".ncx");
            opfBookFilename = Path.Combine(Path.GetDirectoryName(htmlFilename), Path.GetFileNameWithoutExtension(htmlFilename) + ".opf");
            // 3. create 3 files
            if (!forNote)
            {
                CreateHtmlFile();
                CreateNCXTableOfContent();
            }
            else
            {
                // for note
                CreateHtmlFileFromNoteHtml();
                CreateNCXTableOfContentForNoteHtml();
            }

            CreateOPFBookDetail();

            // 4. Create mobile kindle file
            if (!string.IsNullOrWhiteSpace(KindlegenPath))
            {
                CreateKindleFile();
                // we don't use these files, delete it for now
                htmlFilename    = Path.Combine(trashFolder, htmlFilename);
                ncxFilename     = Path.Combine(trashFolder, ncxFilename);
                opfBookFilename = Path.Combine(trashFolder, opfBookFilename);

                if (File.Exists(htmlFilename))
                {
                    File.Delete(htmlFilename);
                }
                if (File.Exists(ncxFilename))
                {
                    File.Delete(ncxFilename);
                }
                if (File.Exists(opfBookFilename))
                {
                    File.Delete(opfBookFilename);
                }

                foreach (var image in book.Chapters.SelectMany(c => c.Images))
                {
                    var imageFilename = Path.Combine(trashFolder, image.Key);
                    if (File.Exists(imageFilename))
                    {
                        File.Delete(imageFilename);
                    }
                }

                var trashMobiFileName    = Path.Combine(trashFolder, title) + ".mobi";
                var downloadMobiFileName = Path.Combine(downloadFolder, title) + ".mobi";

                if (trashMobiFileName != downloadMobiFileName) // on Windows, we don't need to copy because I didn't check for trash folder
                {
                    if (File.Exists(downloadMobiFileName))
                    {
                        File.Delete(downloadMobiFileName);
                    }
                    File.Move(trashMobiFileName, downloadMobiFileName);
                }
            }

            return(Path.Combine(downloadFolder, title) + ".mobi");
        }