2024-09-30 12:55:03 +00:00
|
|
|
import express from 'express';
|
|
|
|
import ical from 'ical';
|
|
|
|
import fs from 'fs';
|
|
|
|
import cron from 'node-cron';
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
|
|
|
2024-10-18 12:30:53 +00:00
|
|
|
const CALENDARS_FILE = 'calendars.json';
|
|
|
|
const MERGED_CALENDARS_DIR = 'merged_calendars';
|
|
|
|
|
|
|
|
// Ensure the merged calendars directory exists
|
|
|
|
if (!fs.existsSync(MERGED_CALENDARS_DIR)) {
|
|
|
|
fs.mkdirSync(MERGED_CALENDARS_DIR);
|
|
|
|
}
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.sendFile('index.html', { root: '.' });
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/merge', async (req, res) => {
|
2024-10-01 23:06:52 +00:00
|
|
|
const { calendars } = req.body;
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
try {
|
2024-10-01 23:06:52 +00:00
|
|
|
//validate the input
|
|
|
|
if (!calendars || !Array.isArray(calendars)) {
|
|
|
|
return res.status(400).json({ error: 'Invalid input' });
|
|
|
|
}
|
2024-10-18 12:30:53 +00:00
|
|
|
// Generate a unique identifier for this set of calendars
|
|
|
|
const calendarId = crypto.randomBytes(16).toString('hex');
|
2024-10-18 12:33:21 +00:00
|
|
|
|
2024-09-30 12:55:03 +00:00
|
|
|
// Fetch calendar data from URLs
|
2024-10-01 23:08:49 +00:00
|
|
|
const promises = calendars.map((calendar) => {
|
|
|
|
return axios.get(calendar.url)
|
|
|
|
.then((response) => {
|
|
|
|
return {
|
|
|
|
data: response.data,
|
|
|
|
prefix: calendar.prefix,
|
2024-10-04 13:08:39 +00:00
|
|
|
override: calendar.override,
|
2024-10-01 23:08:49 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const results = await Promise.all(promises);
|
2024-10-01 23:09:36 +00:00
|
|
|
// Filter out any failed requests
|
|
|
|
const validResults = results.filter((result) => result !== null);
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
// Parse calendar data
|
|
|
|
const mergedCal = [];
|
2024-10-01 23:10:37 +00:00
|
|
|
validResults.forEach((result) => {
|
|
|
|
const calendar = ical.parseICS(result.data);
|
|
|
|
Object.keys(calendar).forEach((key) => {
|
|
|
|
const event = calendar[key];
|
2024-10-04 13:08:39 +00:00
|
|
|
if (result.override) {
|
|
|
|
mergedCal.push({
|
|
|
|
start: event.start,
|
|
|
|
end: event.end,
|
|
|
|
summary: result.prefix,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mergedCal.push({
|
|
|
|
start: event.start,
|
|
|
|
end: event.end,
|
|
|
|
summary: `${result.prefix} ${event.summary}`,
|
|
|
|
});
|
|
|
|
}
|
2024-10-01 23:10:37 +00:00
|
|
|
});
|
|
|
|
});
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
|
2024-10-18 12:33:21 +00:00
|
|
|
// Save merged calendar to file with unique identifier
|
|
|
|
const filename = `${calendarId}.ics`;
|
2024-09-30 12:55:03 +00:00
|
|
|
let icalString = `BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
CALSCALE:GREGORIAN
|
|
|
|
METHOD:PUBLISH
|
|
|
|
`;
|
|
|
|
mergedCal.forEach((event) => {
|
2024-10-04 13:08:39 +00:00
|
|
|
icalString += `BEGIN:VEVENT
|
2024-10-10 12:42:38 +00:00
|
|
|
DTSTART;VALUE=DATE:${event.start.toISOString().split('T')[0].replace(/-/g, '')}
|
|
|
|
DTEND;VALUE=DATE:${event.end.toISOString().split('T')[0].replace(/-/g, '')}
|
2024-09-30 12:55:03 +00:00
|
|
|
SUMMARY:${event.summary}
|
|
|
|
END:VEVENT
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
icalString += `END:VCALENDAR`;
|
2024-10-18 12:36:30 +00:00
|
|
|
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
2024-09-30 12:55:03 +00:00
|
|
|
|
2024-10-01 23:13:03 +00:00
|
|
|
|
2024-10-18 12:36:30 +00:00
|
|
|
// Generate URL for the merged calendar
|
|
|
|
const mergedCalendarUrl = `${req.protocol}://${req.get('host')}/calendar/${calendarId}`;
|
2024-10-15 11:17:07 +00:00
|
|
|
|
2024-10-18 12:43:04 +00:00
|
|
|
// Save the user input and generated ID in calendars.json file
|
|
|
|
saveCalendarData(calendarId, calendars);
|
|
|
|
|
2024-10-15 11:17:07 +00:00
|
|
|
// 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));
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
res.json({ url: mergedCalendarUrl });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).json({ error: 'Failed to merge calendars' });
|
|
|
|
}
|
|
|
|
});
|
2024-10-18 12:43:04 +00:00
|
|
|
//function to save CalendarData to calendars.json
|
|
|
|
function saveCalendarData(calendarId, calendars) {
|
|
|
|
let calendarsData = { mergedCalendars: [] };
|
|
|
|
if (fs.existsSync(CALENDARS_FILE)) {
|
|
|
|
calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
|
|
|
|
}
|
|
|
|
calendarsData.mergedCalendars.push({
|
|
|
|
id: calendarId,
|
|
|
|
calendars: calendars
|
|
|
|
});
|
|
|
|
fs.writeFileSync(CALENDARS_FILE, JSON.stringify(calendarsData, null, 2));
|
|
|
|
}
|
2024-09-30 12:55:03 +00:00
|
|
|
// 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: '.' });
|
|
|
|
});
|
|
|
|
|
2024-10-02 11:32:35 +00:00
|
|
|
// Function to update the merged calendar
|
|
|
|
async function updateMergedCalendar(){
|
|
|
|
try {
|
2024-10-14 13:03:40 +00:00
|
|
|
// Load calendars data from calendars.json file
|
2024-10-02 11:32:35 +00:00
|
|
|
const calendarsFile = 'calendars.json';
|
2024-10-15 12:07:28 +00:00
|
|
|
const calendarsData = JSON.parse(fs.readFileSync(calendarsFile, 'utf8'));
|
2024-10-08 19:04:31 +00:00
|
|
|
|
2024-10-15 12:53:08 +00:00
|
|
|
// Check if calendarsData is defined and has the expected structure
|
|
|
|
if (!calendarsData || !calendarsData.linkGroups) {
|
|
|
|
throw new Error('Invalid calendars data structure');
|
|
|
|
}
|
|
|
|
console.log(calendarsData);
|
2024-10-14 13:03:40 +00:00
|
|
|
// Fetch calendar data for each link group
|
2024-10-15 12:53:08 +00:00
|
|
|
const promises = calendarsData.linkGroups.map((linkGroup) => {
|
|
|
|
return Promise.all(linkGroup.links.map((link) => {
|
2024-10-15 12:07:28 +00:00
|
|
|
return axios.get(link.url)
|
2024-10-02 12:21:00 +00:00
|
|
|
.then((response) => {
|
2024-10-15 12:07:28 +00:00
|
|
|
return {
|
|
|
|
data: response.data,
|
|
|
|
prefix: link.prefix,
|
|
|
|
override: link.override,
|
|
|
|
};
|
2024-10-02 12:21:00 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2024-10-15 12:07:28 +00:00
|
|
|
console.error(error);
|
|
|
|
return null;
|
2024-10-02 12:21:00 +00:00
|
|
|
});
|
2024-10-15 12:07:28 +00:00
|
|
|
}));
|
|
|
|
});
|
2024-10-02 12:21:00 +00:00
|
|
|
|
|
|
|
const results = await Promise.all(promises);
|
2024-10-02 22:09:25 +00:00
|
|
|
// Filter out any failed requests
|
2024-10-14 13:03:40 +00:00
|
|
|
const validResults = results.flat().filter((result) => result !== null);
|
2024-10-02 22:10:27 +00:00
|
|
|
|
|
|
|
// Parse calendar data
|
|
|
|
const mergedCal = [];
|
|
|
|
validResults.forEach((result) => {
|
|
|
|
const calendar = ical.parseICS(result.data);
|
|
|
|
Object.keys(calendar).forEach((key) => {
|
|
|
|
const event = calendar[key];
|
2024-10-04 13:16:21 +00:00
|
|
|
if (result.override) {
|
|
|
|
mergedCal.push({
|
|
|
|
start: event.start,
|
|
|
|
end: event.end,
|
|
|
|
summary: result.prefix,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mergedCal.push({
|
|
|
|
start: event.start,
|
|
|
|
end: event.end,
|
|
|
|
summary: `${result.prefix} ${event.summary}`,
|
|
|
|
});
|
|
|
|
}
|
2024-10-02 22:10:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-02 22:11:20 +00:00
|
|
|
// Save merged calendar to file
|
|
|
|
const filename = `merged-${Date.now()}.ics`;
|
|
|
|
let icalString = `BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
CALSCALE:GREGORIAN
|
|
|
|
METHOD:PUBLISH
|
|
|
|
`;
|
|
|
|
mergedCal.forEach((event) => {
|
2024-10-03 12:05:12 +00:00
|
|
|
icalString += `BEGIN:VEVENT
|
2024-10-15 12:07:28 +00:00
|
|
|
DTSTART;VALUE=DATE:${event.start.toISOString().split('T')[0].replace(/-/g, '')}
|
|
|
|
DTEND;VALUE=DATE:${event.end.toISOString().split('T')[0].replace(/-/g, '')}
|
2024-10-02 22:11:20 +00:00
|
|
|
SUMMARY:${event.summary}
|
|
|
|
END:VEVENT
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
icalString += `END:VCALENDAR`;
|
2024-10-08 19:04:31 +00:00
|
|
|
|
2024-10-02 22:11:20 +00:00
|
|
|
fs.writeFileSync(filename, icalString);
|
|
|
|
|
2024-10-04 13:16:21 +00:00
|
|
|
// Generate a unique URL for the merged calendar
|
2024-10-08 19:22:35 +00:00
|
|
|
const mergedCalendarUrl = `http://localhost:3000/${filename}`;
|
2024-10-08 19:04:31 +00:00
|
|
|
|
|
|
|
// Store the merged calendar URL in a file
|
2024-10-14 13:03:40 +00:00
|
|
|
fs.writeFileSync('merged_calendar_url.txt', mergedCalendarUrl);
|
2024-10-08 19:04:31 +00:00
|
|
|
|
2024-10-02 22:21:57 +00:00
|
|
|
console.log(`Merged calendar updated: ${mergedCalendarUrl}`);
|
2024-10-02 22:13:58 +00:00
|
|
|
|
2024-10-02 22:10:27 +00:00
|
|
|
|
2024-10-02 12:21:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2024-10-02 11:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-02 22:07:55 +00:00
|
|
|
|
2024-09-30 12:55:03 +00:00
|
|
|
// Schedule a cron job to update the merged calendar every hour
|
2024-10-15 12:53:08 +00:00
|
|
|
cron.schedule('*/3 * * * *', () => {
|
2024-09-30 12:55:03 +00:00
|
|
|
console.log('Updating merged calendar...');
|
2024-10-02 22:21:57 +00:00
|
|
|
updateMergedCalendar();
|
2024-10-02 11:20:21 +00:00
|
|
|
});
|
2024-10-02 12:21:00 +00:00
|
|
|
|
2024-10-08 19:13:25 +00:00
|
|
|
// 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);
|
|
|
|
});
|
2024-10-08 19:22:35 +00:00
|
|
|
|
2024-10-02 12:21:00 +00:00
|
|
|
// Start the server
|
|
|
|
const port = 3000;
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Server started on port ${port}`);
|
|
|
|
});
|