added inline TOC, some fixes and tests

This commit is contained in:
2022-07-05 09:13:25 +02:00
parent d68421acca
commit a93a784c83
2 changed files with 26 additions and 13 deletions
+9 -2
View File
@@ -10,13 +10,16 @@ npm install --save node-html-parser
npm install --save nodemailer 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 # 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 "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 "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 "sendEmail": false, // If the script should send eBooks via email
"emailToAddress": "", // Email where to send your eBooks "emailToAddress": "", // Email where to send your eBooks
"emailFromAddress": "", // Important for Kindle deliveries, make sure you have it added in Kindle settings "emailFromAddress": "", // Important for Kindle deliveries, make sure you have it added in Kindle settings
@@ -46,3 +49,7 @@ At first start it will create an empty config file `./novelConfig.conf`, adjust
# Usage # 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. 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.
+16 -10
View File
@@ -92,13 +92,14 @@ function loadConfig() {
} }
}); });
writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4)); writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4));
writeFile('.', 'novelConfig.bak.conf', JSON.stringify(config, null, 4));
} }
else { else {
writeFile('.', 'novelConfig.conf', JSON.stringify(novelConfigDefault, null, 4)); writeFile('.', 'novelConfig.conf', JSON.stringify(novelConfigDefault, null, 4));
config = novelConfigDefault; config = novelConfigDefault;
} }
console.log(config) console.log(config);
} }
function saveConfig() { function saveConfig() {
@@ -109,11 +110,12 @@ async function convertEbook(dir, file, params = { "cover": false, "authors": fal
let file1Path = cleanPath(`${dir}/${file}.${format}`); let file1Path = cleanPath(`${dir}/${file}.${format}`);
let file2Path = cleanPath(`${dir}/${file}.${config['ebookFormat']}`); let file2Path = cleanPath(`${dir}/${file}.${config['ebookFormat']}`);
let convertParams = ' --use-auto-toc'; 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['cover'] ? ` --cover "${params['cover']}"` : '';
convertParams += params['authors'] ? ` --authors "${params['authors']}"` : ''; convertParams += params['authors'] ? ` --authors "${params['authors']}"` : '';
convertParams += params['title'] ? ` --title "${params['title']}"` : ''; convertParams += params['title'] ? ` --title "${params['title']}"` : '';
console.log(`Converting volume: ${file1Path}`);
exec(`${config['converterPath']} "${file1Path}" "${file2Path}"${convertParams}`, (error, stdout, stderr) => { exec(`${config['converterPath']} "${file1Path}" "${file2Path}"${convertParams}`, (error, stdout, stderr) => {
if (error) { if (error) {
console.log(`error: ${error.message}`); console.log(`error: ${error.message}`);
@@ -138,7 +140,7 @@ async function sendEbook(subject, ebookAttachments) { //(dir, ebook, format = 'e
splicedAttachments.push(ebookAttachments) splicedAttachments.push(ebookAttachments)
} }
for (i = 0; i < splicedAttachments.length; ++i) { for (let i = 0; i < splicedAttachments.length; ++i) {
let message = { let message = {
from: config['emailFromAddress'], from: config['emailFromAddress'],
to: config['emailToAddress'], to: config['emailToAddress'],
@@ -286,7 +288,7 @@ async function main() {
// convertEbook(novelDirConvert, fileName); // convertEbook(novelDirConvert, fileName);
// await sendEbook(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 novel = clone(config['novels'][i]);
let chapters = []; let chapters = [];
let nextChapterURL; 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']); const maxVolume = novel['completed'] ? startVol + 1 + Math.floor(totalChapters / novel['completedVolumeChapterCount']) : startVol + Math.floor(totalChapters / novel['completedVolumeChapterCount']);
let ebookAttachments = []; let ebookAttachments = [];
for (vol = startVol; vol < maxVolume; vol++) {
for (let vol = startVol; vol < maxVolume; vol++) {
let volContent = ''; let volContent = '';
let chap;
for (chap = 0; chap < novel['completedVolumeChapterCount'] && chap < chapters.length; chap++) { for (chap = 0; chap < novel['completedVolumeChapterCount'] && chap < chapters.length; chap++) {
volContent += chapters[chap][0] + '\n'; volContent += chapters[chap][0] + '\n';
} }
@@ -356,21 +360,23 @@ async function main() {
}); });
ebookAttachments.push({ ebookAttachments.push({
name: cleanPath(`${novelFileName}.${config['ebookFormat']}`), filename: cleanPath(`${novelFileName}.${config['ebookFormat']}`),
path: cleanPath(`${novelDir}/${novelFileName}.${config['ebookFormat']}`) path: cleanPath(`${novelDir}/${novelFileName}.${config['ebookFormat']}`)
}); });
chapters.splice(0, chap); chapters.splice(0, chap);
} }
await sendEbook(novel['title'], ebookAttachments);
ebookAttachments = []; //await sendEbook(novel['title'], ebookAttachments);
//ebookAttachments = [];
startVol = novel['lastVolume']; startVol = novel['lastVolume'];
totalChapters = chapters.length; 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 volContent = '';
let chap;
for (chap = 0; chap < novel['volumeChapterCount'] && chap < chapters.length; chap++) { for (chap = 0; chap < novel['volumeChapterCount'] && chap < chapters.length; chap++) {
volContent += chapters[chap][0] + '\n'; volContent += chapters[chap][0] + '\n';
} }
@@ -392,7 +398,7 @@ async function main() {
}); });
ebookAttachments.push({ ebookAttachments.push({
name: cleanPath(`${novelFileName}.${config['ebookFormat']}`), filename: cleanPath(`${novelFileName}.${config['ebookFormat']}`),
path: cleanPath(`${novelDir}/${novelFileName}.${config['ebookFormat']}`) path: cleanPath(`${novelDir}/${novelFileName}.${config['ebookFormat']}`)
}); });