From a0963842a659233ab7459efa93133d4f2051ae2d Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Tue, 26 May 2020 13:22:58 +0100 Subject: [PATCH] Added support for directly sending epubs --- index.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b90137d..150ee6e 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,9 @@ const server = http.createServer(async (req, res) => { if (website.toString().endsWith(".pdf")) { fn = sendPDF; } + if (website.toString().endsWith(".epub")) { + fn = sendEpub; + } if (await fn(website)) { fs.readFile(__dirname + "/success.html", function (err,data) { @@ -84,6 +87,25 @@ async function sendPDF(website, tries = 0) { } } +async function sendEpub(website, tries = 0) { + try { + const response = await axios.get(website.toString(), { + responseType: 'arraybuffer' + }) + const title = website.toString().substring(website.toString().lastIndexOf("/")+1, website.toString().lastIndexOf(".")) + await sendToRemarkable(title, Buffer.from(response.data, 'binary'), 'epub'); + + return true; + } catch (ex) { + console.log(ex); + if (tries < 5) { + return await sendEpub(website, ++tries); + } else { + return false; + } + } +} + async function sendPage(website, tries = 0) { const browser = await puppeteer.launch({ ignoreHTTPSErrors: true, @@ -208,7 +230,7 @@ async function sendPage(website, tries = 0) { } } -async function sendToRemarkable(title, myPDF) { +async function sendToRemarkable(title, myPDF, fileType = "pdf") { try { // Refresh token let response = await axios.post( @@ -257,7 +279,7 @@ async function sendToRemarkable(title, myPDF) { let zip = new JSZip(); zip.file(`${ID}.content`, JSON.stringify({ extraMetadata: {}, - fileType: 'pdf', + fileType: fileType, lastOpenedPage: 0, lineHeight: -1, margins: 180, @@ -266,7 +288,7 @@ async function sendToRemarkable(title, myPDF) { transform: {}, })); zip.file(`${ID}.pagedata`, []); - zip.file(`${ID}.pdf`, myPDF); + zip.file(`${ID}.${fileType}`, myPDF); const zipContent = await zip.generateAsync({ type: 'nodebuffer' }); // Upload zip