Ejemplo n.º 1
0
        /// <summary>
        /// Save the image locally and then add to the epub manifest
        /// </summary>
        /// <param name="epub">The epub to add the image to</param>
        /// <param name="url">The address the image is located at</param>
        /// <param name="imgName"></param>
        /// <returns></returns>
        public string SaveImage(Epub epub, string url, string imgName)
        {
            WebClient webClient = new WebClient();

            try
            {
                if (!File.Exists(epub.location + EpubStructure.EPUBLOCATION + EpubStructure.CONTENTLOCATION + EpubStructure.IMAGELOCATION + imgName))
                {
                    EpubParser.Retry(1, TimeSpan.FromSeconds(60), () =>
                    {
                        webClient.DownloadFile(url,
                                               epub.location + EpubStructure.EPUBLOCATION + EpubStructure.CONTENTLOCATION + EpubStructure.IMAGELOCATION + imgName);
                    });
                }
                epub.AddToManifest(imgName, EpubStructure.IMAGELOCATION + imgName);
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed to download: " + url + " - " + imgName);
                Logger.LogError(ex.Message);
            }
            finally
            {
                webClient.Dispose();
            }
            return((EpubStructure.IMAGELOCATION + imgName).Replace("\\", "/"));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create the page
        /// Parse throught the website using the specified parser
        /// Save it tot he file structure and add it to the manifest and spine
        /// </summary>
        /// <param name="page">Page to Parse</param>
        public void CreatePage(Page page)
        {
            Logger.LogInfo("CreatePage");
            Logger.LogInfo("Parsing " + page.title + " with " + page.parser + "Parser");

            EpubParser parser   = (EpubParser)Activator.CreateInstance(Type.GetType("EpubCreator." + page.parser + "Parser"));
            string     bodyText = parser.Parse(page.url, epub);

            string pageTitleNoSpaces = GetPageTitleNoSpaces(page.title);

            StreamWriter writer = new StreamWriter(File.Create(epub.location + EpubStructure.EPUBLOCATION +
                                                               EpubStructure.CONTENTLOCATION + pageTitleNoSpaces));

            writer.WriteLine(
                string.Format(EpubStructure.COMMONPAGE,
                              string.Format(EpubStructure.COMMONHEADER, epub.title),
                              string.Format(EpubStructure.COMMONBODY, pageTitleNoSpaces, page.title, bodyText, page.author)
                              ));
            writer.Close();

            epub.AddToManifest(pageTitleNoSpaces, pageTitleNoSpaces);
            epub.AddToSpine(pageTitleNoSpaces);
        }