Compare commits

...

3 Commits

1 changed files with 6 additions and 46 deletions

View File

@ -92,39 +92,10 @@ END:VEVENT
icalString += `END:VCALENDAR`;
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
// Generate URL for the merged calendar
const mergedCalendarUrl = `${req.protocol}://${req.get('host')}/calendar/${calendarId}`;
// Save the user input and generated ID in calendars.json file
saveCalendarData(calendarId, calendars);
// Save the user input in a calendars.json file
const calendarsFile = 'calendars.json';
let calendarsData = {};
try {
calendarsData = JSON.parse(fs.readFileSync(calendarsFile, 'utf8'));
} catch (error) {
console.error(error);
}
calendars.forEach((calendar) => {
let linkGroup = calendarsData.linkGroups.find((group) => group.name === calendar.linkGroupName);
if (!linkGroup) {
linkGroup = {
name: calendar.linkGroupName,
links: []
};
calendarsData.linkGroups.push(linkGroup);
}
linkGroup.links.push({
url: calendar.url,
prefix: calendar.prefix,
overrideSummary: calendar.override
});
});
fs.writeFileSync(calendarsFile, JSON.stringify(calendarsData, null, 2));
res.json({ url: mergedCalendarUrl });
res.json({ url: `${req.protocol}://${req.get('host')}/calendar/${calendarId}` });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to merge calendars' });
@ -150,25 +121,14 @@ function saveCalendarData(calendarId, calendars) {
});
fs.writeFileSync(CALENDARS_FILE, JSON.stringify(calendarsData, null, 2));
}
// Serve the merged calendar file
app.get('/:filename', (req, res) => {
const filename = req.params.filename;
res.setHeader('Content-Type', 'text/calendar');
res.sendFile(filename, { root: '.' });
});
// Function to update the merged calendar
async function updateMergedCalendar(){
async function updateMergedCalendars(){
try {
// Load calendars data from calendars.json file
const calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
// Check if calendarsData is defined and has the expected structure
if (!calendarsData || !calendarsData.linkGroups) {
throw new Error('Invalid calendars data structure');
}
// Fetch calendar data for each link group
// Fetch calendar data for each merged calendar
for (const mergedCalendar of calendarsData.mergedCalendars) {
const promises = mergedCalendar.calendars.map((calendar) => {
return axios.get(calendar.url)
@ -229,7 +189,7 @@ END:VEVENT
icalString += `END:VCALENDAR`;
// 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}`);
@ -242,7 +202,7 @@ END:VEVENT
// Schedule a cron job to update the merged calendar every hour
cron.schedule('*/3 * * * *', () => {
console.log('Updating merged calendar...');
updateMergedCalendar();
updateMergedCalendars();
});
// Start the server