diff --git a/README.md b/README.md index faf3387..8590e3e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,14 @@ Fetches all PDFs for upcoming events, concatenates them and uploads them as atta Based on https://github.com/churchtools/churchtools-js-client/tree/master/examples/Node.js +## Planned Features + +- concatenate songs +- convert uploaded office documents to PDF + +Later as server cronjob + ## Getting Started -- `npm run setup` (npm install, copy and fill in settings.js.example to settings.js) +- `npm run setup` (equivalent to `npm install` and copy settings.js.example to settings.js) +- fill in values in `settings.js` - `npm start` diff --git a/index.js b/index.js index b17f165..c25bb49 100644 --- a/index.js +++ b/index.js @@ -5,8 +5,10 @@ const axiosCookieJarSupport = require('axios-cookiejar-support'); const tough = require('tough-cookie'); const fs = require('fs'); +const cookieJar = new tough.CookieJar(); + function initChurchToolsClient() { - churchtoolsClient.setCookieJar(axiosCookieJarSupport.wrapper, new tough.CookieJar()); + churchtoolsClient.setCookieJar(axiosCookieJarSupport.wrapper, cookieJar); churchtoolsClient.setBaseUrl(SETTINGS.BASEURL); // Logging can be activated to either LOG_LEVEL_NONE (no logging at all, default), // LOG_LEVEL_DEBUG (outputs every request and response including request/response data) @@ -15,7 +17,6 @@ function initChurchToolsClient() { activateLogging(LOG_LEVEL_INFO); } -// function login() { // if we have a login token, we use this if (SETTINGS.TOKEN) { @@ -58,13 +59,27 @@ async function mergeEventSongs(event) { Promise.allSettled(result) ).then(result => result.map(song => song.value.files.find(f => f.name.includes("Akkorde"))) - .map(f => - churchtoolsClient.get(f.fileUrl).then(file => { - filename = `${date}/${f.name}` - fs.createWriteStream(filename).write(file) - return filename - }) - ) + .map(f => { + // https://github.com/churchtools/churchtools-js-client/issues/25 + filename = `${date}/${f.name}` + const url = f.fileUrl + const file = fs.createWriteStream(filename); + https.get( + url, + { + headers: { + Cookie: await cookieJar.getCookieStringSync() + } + }, + (response) => { + response.pipe(file); + file.on("finish", () => { + file.close(); + console.log("Download Completed"); + }); + } + ); + }) ).then(result => Promise.allSettled(result) ).then(result => {