const fs = require('fs') const cheerio = require('cheerio') const fetch = require('node-fetch') const epub = require('epub-gen') let post = 0 let page = 0 const MAX_VOLUME = 3 const books = [] if (!fs.existsSync('output')) { fs.mkdirSync('output') } const volume = (post) => post <= 69 ? 1 : post <= 168 ? 2 : MAX_VOLUME const metadata = (volume) => ({ title: `L'auberge Vagabonde - Volume ${volume}`, author: ['Maroti', 'ElliVia', 'Pirateaba'], cover: 'https://i.pinimg.com/originals/0b/fd/cf/0bfdcfb42ba3ff0a22f4a7bc52928af4.png', output: `output/L'auberge Vagabonde - Volume ${volume}.epub`, version: 3, lang: 'fr', tocTitle: 'Table des matières', appendChapterTitles: false, content: [], verbose: true, description: "L'histoire d'une fille, d'une auberge et d'un monde plein de niveaux", }) for (let i = 1; i <= MAX_VOLUME; i++) { books.push(metadata(i)) } (async () => { while (true) { const response = await fetch(`https://www.jeunesecrivains.com/t53075p${page++ * 15}-the-wandering-inn-fan-traduction-fantastique-aventure`) const responseHtml = await response.text() const html = cheerio.load(responseHtml) const postBody = html('div.postbody') if (postBody.html() === null) break postBody.each((i, el) => { if (++post === 1) return const title = html('div[align=center]', el) if (title.html() === null) return let text = html('div > div', el) text.attr('style', '') const titleFirst = title.first() titleFirst.find('br').replaceWith(' ') const titleText = titleFirst.text().replace(/\*/g, '') console.log(`${post} - ${titleText}`) books[volume(post) - 1].content.push({ title: titleText, data: text.html(), }) }) } books.map(book => new epub(book)) })()