Compare commits
3 Commits
1552cc2013
...
37108217d8
Author | SHA1 | Date |
---|---|---|
Ryan Mwangi | 37108217d8 | |
Ryan Mwangi | 3d31d039d8 | |
Ryan Mwangi | e01c54e267 |
17
server.js
17
server.js
|
@ -109,7 +109,7 @@ async function updateMergedCalendar(){
|
|||
// Load calendars data from file
|
||||
const calendarsFile = 'calendars.json';
|
||||
const calendars = JSON.parse(fs.readFileSync(calendarsFile, 'utf8'));
|
||||
|
||||
|
||||
// Fetch calendar data from URLs
|
||||
const promises = calendars.map((calendar) => {
|
||||
return axios.get(calendar.url)
|
||||
|
@ -168,10 +168,15 @@ END:VEVENT
|
|||
`;
|
||||
});
|
||||
icalString += `END:VCALENDAR`;
|
||||
|
||||
fs.writeFileSync(filename, icalString);
|
||||
|
||||
// Generate a unique URL for the merged calendar
|
||||
const mergedCalendarUrl = `${req.protocol}://${req.get('host')}/${filename}`;
|
||||
const mergedCalendarUrl = `http://localhost:3000/${filename}`;
|
||||
|
||||
// Store the merged calendar URL in a file
|
||||
fs.writeFileSync(mergedCalendarUrlFile, mergedCalendarUrl);
|
||||
|
||||
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
|
||||
|
||||
|
||||
|
@ -180,13 +185,19 @@ END:VEVENT
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// Schedule a cron job to update the merged calendar every hour
|
||||
cron.schedule('0 * * * *', () => {
|
||||
console.log('Updating merged calendar...');
|
||||
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