Compare commits
8 Commits
5a5fcaeeac
...
0e58a4e12c
Author | SHA1 | Date |
---|---|---|
Ryan Mwangi | 0e58a4e12c | |
Ryan Mwangi | 5681530a27 | |
Ryan Mwangi | 3f00b5558b | |
Ryan Mwangi | 12fafd934f | |
Ryan Mwangi | f539e5d140 | |
Ryan Mwangi | 3438ae9396 | |
Ryan Mwangi | 9b297aa0fd | |
Ryan Mwangi | 35b9284a7a |
13
script.js
13
script.js
|
@ -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);
|
||||
|
50
server.js
50
server.js
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue