Beispiel #1
0
        protected override void InitiateDocument(LnParameters lnParam, string language)
        {
            this.lnParameters    = lnParam;
            this.currentLanguage = language;

            pdf = new PdfDocument(PageSize.A4);
            foreach (string author in lnParam.authors)
            {
                pdf.AddAuthor(author);
            }
            pdf.AddCreationDate();
            pdf.AddLanguage(language);
            pdf.AddTitle(DocumentTitle);
            pdf.AddCreator(Globale.PUBLISHER);
            pdfChapters = new List <Chapter>();
        }
Beispiel #2
0
        protected override void InitiateDocument(LnParameters lnParam, string language)
        {
            this.lnParameters    = lnParam;
            this.currentLanguage = language;
            this.nbChapInEpub    = 1;

            epub = new Document();
            foreach (string author in lnParam.authors)
            {
                epub.AddAuthor(author);
            }
            epub.AddPublisher(Globale.PUBLISHER);
            epub.AddLanguage(language);
            epub.AddTitle(DocumentTitle);

            if (!string.IsNullOrEmpty(lnParam.urlCover))
            {
                AddCover();
            }
        }
Beispiel #3
0
        private static LnParameters BuildDynamicLn()
        {
            LnParameters ln = new LnParameters();

            ln.name = consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskLnName_EmptyToStop);
            if (string.IsNullOrEmpty(ln.name))
            {
                return(ln);
            }
            ln.urlCover = consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskCoverUrl);
            string authors = consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskCsvAuthor);

            if (!string.IsNullOrEmpty(authors))
            {
                foreach (string author in authors.Split(','))
                {
                    ln.authors.Add(author.Trim());
                }
            }

            do
            {
                UrlParameter up = new UrlParameter();

                up.language = consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskLnVersionLanguage);
                up.url      = consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskLnVersionChapterUrl);
                int.TryParse(consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskFirstChapterNumber), out up.firstChapterNumber);
                int.TryParse(consoleTools.AskInformation(LightNovelSniffer_CLI_Strings.AskLastChapterNumber), out up.lastChapterNumber);

                if (!string.IsNullOrEmpty(up.language) && !string.IsNullOrEmpty(up.url))
                {
                    ln.urlParameters.Add(up);
                }
                else
                {
                    consoleTools.Log(LightNovelSniffer_CLI_Strings.LogNoUrlOrLanguageForThisVersion);
                }
            } while (consoleTools.Ask(LightNovelSniffer_CLI_Strings.AskAddAnotherLnVersion));

            return(ln);
        }
Beispiel #4
0
        private static void GetNovel(LnParameters ln)
        {
            foreach (UrlParameter up in ln.urlParameters)
            {
                if (consoleTools.Ask(string.Format(LightNovelSniffer_CLI_Strings.AskRetrieveLnLanguage, up.language)))
                {
                    if (string.IsNullOrEmpty(up.url))
                    {
                        up.url = consoleTools.AskUrl(string.Format(LightNovelSniffer_CLI_Strings.AskLnUrl, ln.name));
                    }

                    if (!string.IsNullOrEmpty(up.url))
                    {
                        webCrawler.DownloadChapters(ln, up.language);
                    }
                    else
                    {
                        consoleTools.Log(string.Format(LightNovelSniffer_CLI_Strings.LogNoLnUrlStopProcess, up.language));
                    }
                }
            }
        }
Beispiel #5
0
        public void VerifLn()
        {
            Assert.IsNotNull(Globale.LN_TO_RETRIEVE);
            Assert.AreEqual(Globale.LN_TO_RETRIEVE.Count, 1);

            LnParameters ln = Globale.LN_TO_RETRIEVE.First();

            Assert.AreEqual(ln.name, "lnName");

            Assert.IsNotNull(ln.authors);
            Assert.AreEqual(ln.authors.Count, 2);
            Assert.AreEqual(ln.authors[0], "author 1");
            Assert.AreEqual(ln.authors[1], "author 2");

            Assert.AreEqual(ln.urlCover, "http://google.com/image.jpg");

            Assert.IsNotNull(ln.urlParameters);
            Assert.AreEqual(ln.urlParameters.Count, 1);
            Assert.AreEqual(ln.urlParameters[0].url, "http://google.com/Novel/chapter-{0}");
            Assert.AreEqual(ln.urlParameters[0].language, "EN");
            Assert.AreEqual(ln.urlParameters[0].firstChapterNumber, 1);
            Assert.AreEqual(ln.urlParameters[0].lastChapterNumber, 10);
        }
Beispiel #6
0
 public EPubFile(LnParameters lnParam, string language)
 {
     InitiateDocument(lnParam, language);
 }
        public void DownloadChapters(LnParameters ln, string language)
        {
            output.Log(string.Format(LightNovelSniffer_Strings.LogBeginLnLanguage, ln.name.ToUpper(), language.ToUpper()));
            UrlParameter urlParameter = ln.GetUrlParameter(language);
            int          i            = urlParameter.firstChapterNumber;
            PdfFile      pdf          = new PdfFile(ln, language);
            EPubFile     epub         = new EPubFile(ln, language);
            string       baseUrl      = urlParameter.url;
            IParser      parser       = new ParserFactory().GetParser(baseUrl);

            if (parser == null)
            {
                output.Log(LightNovelSniffer_Strings.LogNoParserAvailable);
                return;
            }

            List <LnChapter> lnChapters = new List <LnChapter>();

            int chapterOnErrorCountBeforeStop = 0;

            while (urlParameter.lastChapterNumber <= -1 || i <= urlParameter.lastChapterNumber)
            {
                try
                {
                    var currentUrl = String.Format(baseUrl, i);
                    output.Progress(
                        string.Format(LightNovelSniffer_Strings.LogRetrieveChapterProgress
                                      , i
                                      , (urlParameter.lastChapterNumber > 0
                                ? urlParameter.lastChapterNumber.ToString()
                                : "?"))
                        );

                    byte[] pageContent = null;

                    try
                    {
                        pageContent = new WebClient().DownloadData(currentUrl);
                    }
                    catch (WebException e)
                    {
                        if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotFound)
                        {
                            throw new NotExistingChapterException();
                        }
                    }

                    HtmlDocument page = new HtmlDocument();
                    page.LoadHtml(Encoding.UTF8.GetString(pageContent));

                    LnChapter lnChapter = parser.Parse(page);
                    if (lnChapter == null || lnChapter.paragraphs.Count == 0)
                    {
                        throw new NotExistingChapterException();
                    }

                    lnChapter.chapNumber = i;
                    lnChapters.Add(lnChapter);
                    chapterOnErrorCountBeforeStop = 0;
                }
                catch (NotExistingChapterException)
                {
                    if (!Globale.INTERACTIVE_MODE)
                    {
                        output.Log(LightNovelSniffer_Strings.LogNotExistingChapter);
                    }
                    chapterOnErrorCountBeforeStop++;
                    if ((!Globale.INTERACTIVE_MODE && chapterOnErrorCountBeforeStop > Globale.MAX_CHAPTER_ON_ERROR_COUNT_BEFORE_STOP) ||
                        (Globale.INTERACTIVE_MODE && !input.Ask(string.Format(LightNovelSniffer_Strings.LogChapterDoesntExist_AskForNext, i))))
                    {
                        break;
                    }
                } catch (System.Exception e)
                {
                    if (!Globale.INTERACTIVE_MODE)
                    {
                        output.Log(string.Format(LightNovelSniffer_Strings.LogErrorProcessingUrlByParser, string.Format(baseUrl, i), parser.GetType()));
                    }
                    chapterOnErrorCountBeforeStop++;
                    if ((!Globale.INTERACTIVE_MODE && chapterOnErrorCountBeforeStop > Globale.MAX_CHAPTER_ON_ERROR_COUNT_BEFORE_STOP) ||
                        (Globale.INTERACTIVE_MODE && !input.Ask(string.Format(LightNovelSniffer_Strings.LogChapterParserException_AskForNext, i))))
                    {
                        break;
                    }
                }
                i++;
            }

            if (lnChapters.Count == 0)
            {
                output.Log(LightNovelSniffer_Strings.LogNoChapterAvailableAtThisUrl);
                return;
            }

            pdf.AddChapters(lnChapters);
            epub.AddChapters(lnChapters);

            output.Log(LightNovelSniffer_Strings.LogOpeningPdfFile);
            pdf.SaveDocument();
            output.Log(LightNovelSniffer_Strings.LogClosingPdfFile);

            output.Log(LightNovelSniffer_Strings.LogOpeningEpubFile);
            epub.SaveDocument();
            output.Log(LightNovelSniffer_Strings.LogClosingEpubFile);

            output.Log(string.Format(LightNovelSniffer_Strings.LogEndLnLanguage, ln.name.ToUpper(), language.ToUpper()));
        }
        public void DownloadChapters(LnParameters ln, string language)
        {
            output.Log(string.Format(Resources.LightNovelSniffer_Strings.LogBeginLnLanguage, ln.name.ToUpper(), language.ToUpper()));
            UrlParameter urlParameter = ln.GetUrlParameter(language);
            int          i            = urlParameter.firstChapterNumber;
            PdfFile      pdf          = new PdfFile(ln, language);
            EPubFile     epub         = new EPubFile(ln, language);
            string       baseUrl      = urlParameter.url;
            IParser      parser       = new ParserFactory().GetParser(baseUrl);

            if (parser == null)
            {
                output.Log(Resources.LightNovelSniffer_Strings.LogNoParserAvailable);
                return;
            }

            List <LnChapter> lnChapters = new List <LnChapter>();

            while (urlParameter.lastChapterNumber <= -1 || i <= urlParameter.lastChapterNumber)
            {
                try
                {
                    var currentUrl = String.Format(baseUrl, i);
                    output.Progress(
                        string.Format(Resources.LightNovelSniffer_Strings.LogRetrieveChapterProgress
                                      , i
                                      , (urlParameter.lastChapterNumber > 0
                                ? urlParameter.lastChapterNumber.ToString()
                                : "?"))
                        );
                    HtmlDocument page = new HtmlDocument();
                    page.LoadHtml(
                        Encoding.UTF8.GetString(
                            new WebClient().DownloadData(
                                currentUrl)));

                    LnChapter lnChapter = parser.Parse(page);
                    if (lnChapter == null || lnChapter.paragraphs.Count == 0)
                    {
                        throw new NotExistingChapterException();
                    }

                    lnChapter.chapNumber = i;
                    lnChapters.Add(lnChapter);
                }
                catch (NotExistingChapterException)
                {
                    if (!Globale.INTERACTIVE_MODE ||
                        !input.Ask(
                            string.Format(
                                Resources.LightNovelSniffer_Strings.LogChapterDoesntExist_AskForNext,
                                i)))
                    {
                        break;
                    }
                } catch (System.Exception e)
                {
                    throw new ParserException(string.Format(Resources.LightNovelSniffer_Strings.LogErrorProcessingUrlByParser, string.Format(baseUrl, i), parser.GetType()), e);
                }
                i++;
            }

            if (lnChapters.Count == 0)
            {
                output.Log(Resources.LightNovelSniffer_Strings.LogNoChapterAvailableAtThisUrl);
                return;
            }

            pdf.AddChapters(lnChapters);
            epub.AddChapters(lnChapters);

            output.Log(Resources.LightNovelSniffer_Strings.LogOpeningPdfFile);
            pdf.SaveDocument();
            output.Log(Resources.LightNovelSniffer_Strings.LogClosingPdfFile);

            output.Log(Resources.LightNovelSniffer_Strings.LogOpeningEpubFile);
            epub.SaveDocument();
            output.Log(Resources.LightNovelSniffer_Strings.LogClosingEpubFile);

            output.Log(string.Format(Resources.LightNovelSniffer_Strings.LogEndLnLanguage, ln.name.ToUpper(), language.ToUpper()));
        }
 protected abstract void InitiateDocument(LnParameters ln, string language);