Compare commits

..

No commits in common. "3901a5d89406849426d25b700ee74f4c0aef2018" and "fe35fd36650c7fb4231c2559457b85749b86d9db" have entirely different histories.

1 changed files with 46 additions and 6 deletions

View File

@ -91,11 +91,40 @@ 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);
res.json({ url: `${req.protocol}://${req.get('host')}/calendar/${calendarId}` });
// 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 });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to merge calendars' });
@ -121,14 +150,25 @@ 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 updateMergedCalendars(){
async function updateMergedCalendar(){
try {
// Load calendars data from calendars.json file
const calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
// Fetch calendar data for each merged calendar
// 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
for (const mergedCalendar of calendarsData.mergedCalendars) {
const promises = mergedCalendar.calendars.map((calendar) => {
return axios.get(calendar.url)
@ -189,7 +229,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}`);
@ -202,7 +242,7 @@ END:VEVENT
// Schedule a cron job to update the merged calendar every hour
cron.schedule('*/3 * * * *', () => {
console.log('Updating merged calendar...');
updateMergedCalendars();
updateMergedCalendar();
});
// Start the server