const AP = require("article-parser"); const tempFile = require("tempfile"); const nodePandoc = require("node-pandoc-promise"); const fs = require("fs"); const { DownloaderHelper } = require("node-downloader-helper"); const getArticle = async url => { try { const article = await AP.extract(url); return article; } catch (err) { console.trace(err); } }; module.exports = (url, epubPath) => { getArticle(url).then(res => { let xml = `${res.title} ${res.published} en-US ${res.author}`; let html = tempFile(".html"); let metadata = tempFile(".xml"); fs.writeFileSync(html, res.content); fs.writeFileSync(metadata, xml); const imageUrl = res.image; const dl = new DownloaderHelper(res.image, "/tmp", { fileName: "epub-to-image.jpg" }); dl.start(); dl.on("end", () => { nodePandoc(html, [ "-o", epubPath, `--epub-cover-image=/tmp/epub-to-image.jpg`, `--epub-metadata=${metadata}` ]) .then(res => { console.log(`Generated EPUB file at ${epubPath}`); }) .catch(err => { console.error("Oh No: ", err); }); }); }); };