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