Compare commits
No commits in common. "fe35fd36650c7fb4231c2559457b85749b86d9db" and "896738a1231eda0b66fa97f7e3501b022f9a7af8" have entirely different histories.
fe35fd3665
...
896738a123
49
server.js
49
server.js
|
@ -167,27 +167,28 @@ async function updateMergedCalendar(){
|
||||||
if (!calendarsData || !calendarsData.linkGroups) {
|
if (!calendarsData || !calendarsData.linkGroups) {
|
||||||
throw new Error('Invalid calendars data structure');
|
throw new Error('Invalid calendars data structure');
|
||||||
}
|
}
|
||||||
|
console.log(calendarsData);
|
||||||
// Fetch calendar data for each link group
|
// Fetch calendar data for each link group
|
||||||
for (const mergedCalendar of calendarsData.mergedCalendars) {
|
const promises = calendarsData.linkGroups.map((linkGroup) => {
|
||||||
const promises = mergedCalendar.calendars.map((calendar) => {
|
return Promise.all(linkGroup.links.map((link) => {
|
||||||
return axios.get(calendar.url)
|
return axios.get(link.url)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return {
|
return {
|
||||||
data: response.data,
|
data: response.data,
|
||||||
prefix: calendar.prefix,
|
prefix: link.prefix,
|
||||||
override: calendar.override,
|
override: link.override,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
});
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
const results = await Promise.all(promises);
|
const results = await Promise.all(promises);
|
||||||
// Filter out any failed requests
|
// Filter out any failed requests
|
||||||
const validResults = results.filter((result) => result !== null);
|
const validResults = results.flat().filter((result) => result !== null);
|
||||||
|
|
||||||
// Parse calendar data
|
// Parse calendar data
|
||||||
const mergedCal = [];
|
const mergedCal = [];
|
||||||
|
@ -228,12 +229,17 @@ END:VEVENT
|
||||||
});
|
});
|
||||||
icalString += `END:VCALENDAR`;
|
icalString += `END:VCALENDAR`;
|
||||||
|
|
||||||
|
fs.writeFileSync(filename, icalString);
|
||||||
|
|
||||||
|
// Generate a unique URL for the merged calendar
|
||||||
|
const mergedCalendarUrl = `http://localhost:3000/${filename}`;
|
||||||
|
|
||||||
// Store the merged calendar URL in a file
|
// Store the merged calendar URL in a file
|
||||||
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
||||||
|
|
||||||
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
|
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
|
||||||
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
@ -245,6 +251,13 @@ cron.schedule('*/3 * * * *', () => {
|
||||||
updateMergedCalendar();
|
updateMergedCalendar();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// serve updated merged calendar to user
|
||||||
|
app.get('/merged-calendar', (req, res) => {
|
||||||
|
const mergedCalendarUrlFile = 'merged_calendar_url.txt';
|
||||||
|
const mergedCalendarUrl = fs.readFileSync(mergedCalendarUrlFile, 'utf8');
|
||||||
|
res.redirect(mergedCalendarUrl);
|
||||||
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|
Loading…
Reference in New Issue