Compare commits
3 Commits
896738a123
...
fe35fd3665
Author | SHA1 | Date |
---|---|---|
Ryan Mwangi | fe35fd3665 | |
Ryan Mwangi | d43a701051 | |
Ryan Mwangi | e6b63cd85f |
49
server.js
49
server.js
|
@ -167,28 +167,27 @@ 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
|
||||||
const promises = calendarsData.linkGroups.map((linkGroup) => {
|
for (const mergedCalendar of calendarsData.mergedCalendars) {
|
||||||
return Promise.all(linkGroup.links.map((link) => {
|
const promises = mergedCalendar.calendars.map((calendar) => {
|
||||||
return axios.get(link.url)
|
return axios.get(calendar.url)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return {
|
return {
|
||||||
data: response.data,
|
data: response.data,
|
||||||
prefix: link.prefix,
|
prefix: calendar.prefix,
|
||||||
override: link.override,
|
override: calendar.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.flat().filter((result) => result !== null);
|
const validResults = results.filter((result) => result !== null);
|
||||||
|
|
||||||
// Parse calendar data
|
// Parse calendar data
|
||||||
const mergedCal = [];
|
const mergedCal = [];
|
||||||
|
@ -229,17 +228,12 @@ 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);
|
||||||
}
|
}
|
||||||
|
@ -251,13 +245,6 @@ 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