feat: successfull download and merge
This commit is contained in:
parent
416330abc6
commit
2f153c70e3
1 changed files with 16 additions and 11 deletions
27
index.js
27
index.js
|
@ -45,6 +45,8 @@ async function mergeEventSongs(event) {
|
|||
try {
|
||||
fs.mkdirSync(date)
|
||||
} catch(e) { console.log(e); }
|
||||
//console.debug(await cookieJar.getCookieStringSync(SETTINGS.BASEURL))
|
||||
cookie = await cookieJar.getCookieStringSync(SETTINGS.BASEURL)
|
||||
return churchtoolsClient.get(`/events/${event.id}/agenda`)
|
||||
.then(agenda =>
|
||||
agenda.items
|
||||
|
@ -59,41 +61,44 @@ async function mergeEventSongs(event) {
|
|||
Promise.allSettled(result)
|
||||
).then(result =>
|
||||
result.map(song => song.value.files.find(f => f.name.includes("Akkorde")))
|
||||
.map(async (f) => {
|
||||
.map(f => new Promise((resolve) => {
|
||||
// https://github.com/churchtools/churchtools-js-client/issues/25
|
||||
filename = `${date}/${f.name}`
|
||||
const filename = `${date}/${f.name}`
|
||||
const url = f.fileUrl
|
||||
const file = fs.createWriteStream(filename);
|
||||
const https = require('https');
|
||||
https.get(
|
||||
url,
|
||||
{
|
||||
headers: {
|
||||
Cookie: await cookieJar.getCookieStringSync()
|
||||
Cookie: cookie
|
||||
}
|
||||
},
|
||||
(response) => {
|
||||
response.pipe(file);
|
||||
file.on("finish", () => {
|
||||
file.close();
|
||||
console.log("Download Completed");
|
||||
resolve(filename)
|
||||
});
|
||||
}
|
||||
);
|
||||
})
|
||||
)
|
||||
}))
|
||||
).then(result =>
|
||||
Promise.allSettled(result)
|
||||
).then(result => {
|
||||
//console.debug(result)
|
||||
const PDFMerger = require('pdf-merger-js');
|
||||
const merger = new PDFMerger();
|
||||
return new Promise(async (resolve) => {
|
||||
for(const file of result) {
|
||||
console.log('Adding', file)
|
||||
await merger.add(file.value);
|
||||
}
|
||||
const merged = `${date}/${date}-songs-akkorde.pdf`
|
||||
await merger.save(merged);
|
||||
const mergedPdfBuffer = await merger.saveAsBuffer();
|
||||
//const mergedPdfBuffer = await merger.saveAsBlob();
|
||||
var data = new FormData();
|
||||
data.append("files[]", mergedPdfBuffer)
|
||||
data.append("files[]", fs.createReadStream(merged))
|
||||
resolve(await churchtoolsClient.post(`/files/service/${event.id}`, data))
|
||||
})
|
||||
})
|
||||
|
@ -105,10 +110,10 @@ login().then(() => {
|
|||
console.log(`Hello ${whoAmI.firstName}!`);
|
||||
});
|
||||
return churchtoolsClient.get('/events')
|
||||
/*}).then(events => {
|
||||
// TODO uncomment this block and select desired event id here
|
||||
}).then(events => {
|
||||
// TODO select desired event id here or comment this block to see upcoming events
|
||||
const event = events[1];
|
||||
return mergeEventSongs(event); */
|
||||
return mergeEventSongs(event);
|
||||
}).then(result => {
|
||||
console.log(result)
|
||||
}).catch(error => {
|
||||
|
|
Loading…
Add table
Reference in a new issue