From a93a784c8373d2bbbb344cc2be6bd076e97ab327 Mon Sep 17 00:00:00 2001 From: zjamnik Date: Tue, 5 Jul 2022 09:13:25 +0200 Subject: [PATCH] added inline TOC, some fixes and tests --- README.md | 13 ++++++++++--- WNtoEmail.js | 26 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f5a314e..df53e0c 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,16 @@ npm install --save node-html-parser npm install --save nodemailer ``` +I'm using fetch, which is an experimental feature, it might work differently on different versions. Script written on Node version `v18.4.0` +> ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time + # Config -At first start it will create an empty config file `./novelConfig.conf`, adjust the setting according to comments below +At first start it will create an empty config file `./novelConfig.conf`, adjust the setting according to comments below: ``` { "downloadLocation": "", // New chapter download location "converterPath": "ebook-convert.exe", // Calibre eBook converter, I recommend adding it to you PATH, NOT tested when it's not in PATH - "ebookFormat": "epub", // Desired eBook format, Kindle started supporting epub dso that's default + "ebookFormat": "epub", // Desired eBook format, Kindle started supporting epub so that's default "sendEmail": false, // If the script should send eBooks via email "emailToAddress": "", // Email where to send your eBooks "emailFromAddress": "", // Important for Kindle deliveries, make sure you have it added in Kindle settings @@ -45,4 +48,8 @@ At first start it will create an empty config file `./novelConfig.conf`, adjust ``` # Usage -Just run the script with Node.js. Intended usage is with a Task Scheduler on Windows. There shouldn't be anything OS specific. Cron on Linux should work after modifying `"converterPath"` to an appropriate command, but that's untested. \ No newline at end of file +Just run the script with Node.js. Intended usage is with a Task Scheduler on Windows. There shouldn't be anything OS specific. Cron on Linux should work after modifying `"converterPath"` to an appropriate command, but that's untested. +At present there is no crash resiliency, if the program crashes for any reason `` and `` config will not be cosistent and needs to be corrected. There is a copy of config file created at the start. + +## Send to Kindle +[Kindle help page](https://www.amazon.com/gp/help/customer/display.html?nodeId=GX9XLEVV8G4DB28H) can help you with setup. You need to assign the divice you want to use an email address (there should already be one with some random ID, you can change it something more convinient) and add your email used for sending to the allowed list. \ No newline at end of file diff --git a/WNtoEmail.js b/WNtoEmail.js index 974b1e3..153278c 100644 --- a/WNtoEmail.js +++ b/WNtoEmail.js @@ -92,13 +92,14 @@ function loadConfig() { } }); writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4)); + writeFile('.', 'novelConfig.bak.conf', JSON.stringify(config, null, 4)); } else { writeFile('.', 'novelConfig.conf', JSON.stringify(novelConfigDefault, null, 4)); config = novelConfigDefault; } - console.log(config) + console.log(config); } function saveConfig() { @@ -109,11 +110,12 @@ async function convertEbook(dir, file, params = { "cover": false, "authors": fal let file1Path = cleanPath(`${dir}/${file}.${format}`); let file2Path = cleanPath(`${dir}/${file}.${config['ebookFormat']}`); let convertParams = ' --use-auto-toc'; - //convertParams += format2 == 'epub' ? ' --epub-inline-toc' : ''; + convertParams += config['ebookFormat'] == 'epub' ? ' --epub-inline-toc' : ''; convertParams += params['cover'] ? ` --cover "${params['cover']}"` : ''; convertParams += params['authors'] ? ` --authors "${params['authors']}"` : ''; convertParams += params['title'] ? ` --title "${params['title']}"` : ''; + console.log(`Converting volume: ${file1Path}`); exec(`${config['converterPath']} "${file1Path}" "${file2Path}"${convertParams}`, (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); @@ -138,7 +140,7 @@ async function sendEbook(subject, ebookAttachments) { //(dir, ebook, format = 'e splicedAttachments.push(ebookAttachments) } - for (i = 0; i < splicedAttachments.length; ++i) { + for (let i = 0; i < splicedAttachments.length; ++i) { let message = { from: config['emailFromAddress'], to: config['emailToAddress'], @@ -286,7 +288,7 @@ async function main() { // convertEbook(novelDirConvert, fileName); // await sendEbook(novelDirConvert, fileName); - for (i = 0; i < config['novels'].length; ++i) { + for (let i = 0; i < config['novels'].length; ++i) { let novel = clone(config['novels'][i]); let chapters = []; let nextChapterURL; @@ -332,9 +334,11 @@ async function main() { const maxVolume = novel['completed'] ? startVol + 1 + Math.floor(totalChapters / novel['completedVolumeChapterCount']) : startVol + Math.floor(totalChapters / novel['completedVolumeChapterCount']); let ebookAttachments = []; - for (vol = startVol; vol < maxVolume; vol++) { + + for (let vol = startVol; vol < maxVolume; vol++) { let volContent = ''; + let chap; for (chap = 0; chap < novel['completedVolumeChapterCount'] && chap < chapters.length; chap++) { volContent += chapters[chap][0] + '\n'; } @@ -356,21 +360,23 @@ async function main() { }); ebookAttachments.push({ - name: cleanPath(`${novelFileName}.${config['ebookFormat']}`), + filename: cleanPath(`${novelFileName}.${config['ebookFormat']}`), path: cleanPath(`${novelDir}/${novelFileName}.${config['ebookFormat']}`) }); chapters.splice(0, chap); } - await sendEbook(novel['title'], ebookAttachments); + + //await sendEbook(novel['title'], ebookAttachments); - ebookAttachments = []; + //ebookAttachments = []; startVol = novel['lastVolume']; totalChapters = chapters.length; - for (vol = startVol; vol < startVol + Math.floor(totalChapters / novel['volumeChapterCount']); vol++) { + for (let vol = startVol; vol < startVol + Math.floor(totalChapters / novel['volumeChapterCount']); vol++) { let volContent = ''; + let chap; for (chap = 0; chap < novel['volumeChapterCount'] && chap < chapters.length; chap++) { volContent += chapters[chap][0] + '\n'; } @@ -392,7 +398,7 @@ async function main() { }); ebookAttachments.push({ - name: cleanPath(`${novelFileName}.${config['ebookFormat']}`), + filename: cleanPath(`${novelFileName}.${config['ebookFormat']}`), path: cleanPath(`${novelDir}/${novelFileName}.${config['ebookFormat']}`) });