Compare commits

...

8 Commits

2 changed files with 61 additions and 2 deletions

View File

@ -40,5 +40,16 @@ const form = document.getElementById('merge-form');
result.innerHTML = 'Error merging calendars';
});
});
const refreshInterval = 60 * 60 * 1000; // 1 hour
setInterval(() => {
fetch('/merge')
.then((response) => response.json())
.then((data) => {
result.innerHTML = `Merged calendar URL: <a href="${data.url}">${data.url}</a>`;
})
.catch((error) => {
console.error(error);
result.innerHTML = 'Error merging calendars';
});
}, refreshInterval);

View File

@ -114,14 +114,62 @@ async function updateMergedCalendar(){
});
const results = await Promise.all(promises);
// Filter out any failed requests
const validResults = results.filter((result) => result !== null);
// Parse calendar data
const mergedCal = [];
validResults.forEach((result) => {
const calendar = ical.parseICS(result.data);
Object.keys(calendar).forEach((key) => {
const event = calendar[key];
mergedCal.push({
start: event.start,
end: event.end,
summary: `${result.prefix} ${event.summary}`,
});
});
});
// Save merged calendar to file
const filename = `merged-${Date.now()}.ics`;
let icalString = `BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
`;
mergedCal.forEach((event) => {
icalString += `DTSTART:${event.start}
DTEND:${event.end}
SUMMARY:${event.summary}
END:VEVENT
`;
});
icalString += `END:VCALENDAR`;
fs.writeFileSync(filename, icalString);
// Generate a unique URL for the merged calendar
const mergedCalendarUrl = `http://localhost:3000/${filename}`;
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
} catch (error) {
console.error(error);
}
}
const refreshInterval = 60 * 60 * 1000; // 1 hour
setInterval(() => {
// Fetch new calendar data and update the merged calendar
updateMergedCalendar();
}, refreshInterval);
// Schedule a cron job to update the merged calendar every hour
cron.schedule('0 * * * *', () => {
console.log('Updating merged calendar...');
// TO DO: implement the logic to update the merged calendar
updateMergedCalendar();
});
// Start the server