Compare commits
No commits in common. "3901a5d89406849426d25b700ee74f4c0aef2018" and "fe35fd36650c7fb4231c2559457b85749b86d9db" have entirely different histories.
3901a5d894
...
fe35fd3665
52
server.js
52
server.js
|
@ -91,11 +91,40 @@ END:VEVENT
|
||||||
});
|
});
|
||||||
icalString += `END:VCALENDAR`;
|
icalString += `END:VCALENDAR`;
|
||||||
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
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
|
// Save the user input and generated ID in calendars.json file
|
||||||
saveCalendarData(calendarId, calendars);
|
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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: 'Failed to merge calendars' });
|
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));
|
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
|
// Function to update the merged calendar
|
||||||
async function updateMergedCalendars(){
|
async function updateMergedCalendar(){
|
||||||
try {
|
try {
|
||||||
// Load calendars data from calendars.json file
|
// Load calendars data from calendars.json file
|
||||||
const calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
|
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) {
|
for (const mergedCalendar of calendarsData.mergedCalendars) {
|
||||||
const promises = mergedCalendar.calendars.map((calendar) => {
|
const promises = mergedCalendar.calendars.map((calendar) => {
|
||||||
return axios.get(calendar.url)
|
return axios.get(calendar.url)
|
||||||
|
@ -189,7 +229,7 @@ END:VEVENT
|
||||||
icalString += `END:VCALENDAR`;
|
icalString += `END:VCALENDAR`;
|
||||||
|
|
||||||
// Store the merged calendar URL in a file
|
// 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}`);
|
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
|
||||||
|
|
||||||
|
@ -202,7 +242,7 @@ END:VEVENT
|
||||||
// Schedule a cron job to update the merged calendar every hour
|
// Schedule a cron job to update the merged calendar every hour
|
||||||
cron.schedule('*/3 * * * *', () => {
|
cron.schedule('*/3 * * * *', () => {
|
||||||
console.log('Updating merged calendar...');
|
console.log('Updating merged calendar...');
|
||||||
updateMergedCalendars();
|
updateMergedCalendar();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
|
|
Loading…
Reference in New Issue