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) {
|
||||
throw new Error('Invalid calendars data structure');
|
||||
}
|
||||
console.log(calendarsData);
|
||||
|
||||
// Fetch calendar data for each link group
|
||||
const promises = calendarsData.linkGroups.map((linkGroup) => {
|
||||
return Promise.all(linkGroup.links.map((link) => {
|
||||
return axios.get(link.url)
|
||||
.then((response) => {
|
||||
return {
|
||||
data: response.data,
|
||||
prefix: link.prefix,
|
||||
override: link.override,
|
||||
};
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
return null;
|
||||
});
|
||||
}));
|
||||
});
|
||||
for (const mergedCalendar of calendarsData.mergedCalendars) {
|
||||
const promises = mergedCalendar.calendars.map((calendar) => {
|
||||
return axios.get(calendar.url)
|
||||
.then((response) => {
|
||||
return {
|
||||
data: response.data,
|
||||
prefix: calendar.prefix,
|
||||
override: calendar.override,
|
||||
};
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
return null;
|
||||
});
|
||||
});
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
// Filter out any failed requests
|
||||
const validResults = results.flat().filter((result) => result !== null);
|
||||
const validResults = results.filter((result) => result !== null);
|
||||
|
||||
// Parse calendar data
|
||||
const mergedCal = [];
|
||||
|
@ -229,17 +228,12 @@ END:VEVENT
|
|||
});
|
||||
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
|
||||
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
||||
|
||||
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
|
||||
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
@ -251,13 +245,6 @@ cron.schedule('*/3 * * * *', () => {
|
|||
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
|
||||
const port = 3000;
|
||||
app.listen(port, () => {
|
||||
|
|
Loading…
Reference in New Issue