2022-10-05 21:41:17 +00:00
|
|
|
import { EPub } from '@lesjoursfr/html-to-epub'
|
|
|
|
import fs from 'fs'
|
|
|
|
import { parse } from 'node-html-parser'
|
|
|
|
|
|
|
|
if (!fs.existsSync('output')) {
|
|
|
|
fs.mkdirSync('output')
|
|
|
|
}
|
|
|
|
|
|
|
|
const cover =
|
|
|
|
'https://i0.wp.com/thefantasyinn.com/wp-content/uploads/2018/08/twi.jpg'
|
|
|
|
|
|
|
|
const css = fs.readFileSync('template.css')
|
|
|
|
|
|
|
|
const fetchPage = async (url) => {
|
|
|
|
const response = await fetch(url)
|
|
|
|
const responseHtml = await response.text()
|
|
|
|
const html = parse(responseHtml)
|
|
|
|
|
|
|
|
const title = html.querySelector('h1.entry-title').text
|
|
|
|
const content = html.querySelector('div.entry-content')
|
2022-10-26 12:42:53 +00:00
|
|
|
|
|
|
|
for (const el of ['a', 'h3', 'hr', 'iframe', 'script', 'div.tiled-gallery']) {
|
|
|
|
content.querySelectorAll(el).map((c) => c.remove())
|
|
|
|
}
|
|
|
|
|
2022-10-05 23:21:14 +00:00
|
|
|
content.querySelectorAll('img').map((c) => {
|
|
|
|
for (const attr in c.attributes) {
|
|
|
|
if (attr !== 'src') {
|
|
|
|
c.removeAttribute(attr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-10-05 21:41:17 +00:00
|
|
|
const data = content.toString()
|
|
|
|
|
|
|
|
console.log(title)
|
|
|
|
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const run = async (url, author, lang) => {
|
|
|
|
const response = await fetch(url)
|
|
|
|
const responseHtml = await response.text()
|
|
|
|
const html = parse(responseHtml)
|
|
|
|
|
|
|
|
const content = html.querySelectorAll('div.entry-content > p')
|
|
|
|
const title = html.querySelector('#site-title > span > a').text
|
|
|
|
let volume = 0
|
|
|
|
|
|
|
|
for (let i = 0; i < content.length; i++) {
|
|
|
|
if (i % 2 === 0) {
|
|
|
|
volume = parseInt(content[i].text.replace('Volume', '').trim())
|
|
|
|
} else if (volume) {
|
|
|
|
const metadatas = {
|
|
|
|
title,
|
|
|
|
author,
|
|
|
|
cover,
|
|
|
|
version: 3,
|
|
|
|
css,
|
|
|
|
lang,
|
|
|
|
verbose: true,
|
|
|
|
content: [],
|
|
|
|
}
|
|
|
|
for (const node of content[i].querySelectorAll('a')) {
|
|
|
|
const link = parse(node.getAttribute('href'))
|
|
|
|
const page = await fetchPage(link)
|
|
|
|
metadatas.content.push(page)
|
|
|
|
}
|
2022-10-05 23:21:14 +00:00
|
|
|
const epub = new EPub(
|
|
|
|
metadatas,
|
|
|
|
`output/${title} - Volume ${volume}.epub`
|
|
|
|
)
|
2022-10-05 21:41:17 +00:00
|
|
|
await epub.render()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run(
|
|
|
|
'https://aubergevagabonde.wordpress.com/sommaire/',
|
|
|
|
['ElliVia', 'Pirateaba'],
|
|
|
|
'fr'
|
|
|
|
)
|
|
|
|
|
|
|
|
run('https://wanderinginn.com/table-of-contents/', ['Pirateaba'], 'en')
|