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';
|
2024-10-19 14:06:09 +00:00
|
|
|
import crypto from 'crypto';
|
2024-10-24 12:02:01 +00:00
|
|
|
import path from 'path';
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
|
|
|
2024-10-24 11:21:21 +00:00
|
|
|
const MERGED_CALENDARS_DIR = 'calendar';
|
2024-10-18 12:30:53 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2024-10-19 15:32:43 +00:00
|
|
|
app.get('/script.js', (req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'application/javascript');
|
|
|
|
res.sendFile('script.js', { root: '.' });
|
|
|
|
});
|
|
|
|
|
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-20 23:25:40 +00:00
|
|
|
const { linkGroupName, calendars } = req.body;
|
2024-09-30 12:55:03 +00:00
|
|
|
|
|
|
|
try {
|
2024-10-20 23:25:40 +00:00
|
|
|
// Validate the input
|
2024-10-21 12:33:57 +00:00
|
|
|
if (!linkGroupName || !calendars || !Array.isArray(calendars) || calendars.length === 0) {
|
|
|
|
return res.status(400).json({ error: 'Invalid input. Please provide a linkGroupName and at least one calendar.' });
|
2024-10-01 23:06:52 +00:00
|
|
|
}
|
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-23 12:46:58 +00:00
|
|
|
// Save merged calendar to .ics file with unique identifier
|
2024-10-18 12:33:21 +00:00
|
|
|
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-22 16:22:57 +00:00
|
|
|
DTSTART;VALUE=DATETIME:${event.start.toISOString().replace(/-|:|\.\d{3}/g, '')}
|
|
|
|
DTEND;VALUE=DATETIME:${event.end.toISOString().replace(/-|:|\.\d{3}/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-10-19 15:32:43 +00:00
|
|
|
|
2024-10-23 12:46:58 +00:00
|
|
|
// Save the user input and generated ID in a separate JSON file
|
2024-10-20 23:25:40 +00:00
|
|
|
saveCalendarData(calendarId, linkGroupName, calendars);
|
2024-10-18 13:43:13 +00:00
|
|
|
|
2024-10-19 14:36:27 +00:00
|
|
|
res.json({ url: `${req.protocol}://${req.get('host')}/calendar/${calendarId}` });
|
2024-09-30 12:55:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).json({ error: 'Failed to merge calendars' });
|
|
|
|
}
|
|
|
|
});
|
2024-10-18 13:43:13 +00:00
|
|
|
|
2024-10-24 12:16:44 +00:00
|
|
|
// Serve the merged calendar file and refresh if older than an hour
|
|
|
|
app.get('/calendar/:name', async (req, res) => {
|
2024-10-24 12:18:02 +00:00
|
|
|
const calendarName = req.params.name;
|
2024-10-24 12:18:44 +00:00
|
|
|
const icsFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.ics`);
|
2024-10-24 12:19:15 +00:00
|
|
|
const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`);
|
2024-10-24 12:16:44 +00:00
|
|
|
|
2024-10-24 12:20:52 +00:00
|
|
|
try {
|
|
|
|
// Check if the .ics file exists
|
|
|
|
if (fs.existsSync(icsFilePath)) {
|
|
|
|
const stats = fs.statSync(icsFilePath);
|
|
|
|
const lastModified = new Date(stats.mtime);
|
|
|
|
const now = new Date();
|
2024-10-24 12:21:56 +00:00
|
|
|
// Check if the file is older than one hour
|
|
|
|
if (now - lastModified > 60 * 60 * 1000) {
|
|
|
|
console .log('Refreshing calendar data...');
|
2024-10-24 12:22:50 +00:00
|
|
|
|
|
|
|
// Read the JSON file to get the source URL and other details
|
|
|
|
const jsonData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));
|
|
|
|
const { linkGroupName, calendars } = jsonData;
|
2024-10-24 12:25:02 +00:00
|
|
|
|
|
|
|
// Fetch calendar data for each merged calendar
|
|
|
|
const promises = calendars.map((calendar) => {
|
|
|
|
return axios.get(calendar.url)
|
|
|
|
.then((response) => {
|
|
|
|
return {
|
|
|
|
data: response.data,
|
|
|
|
prefix: calendar.prefix,
|
|
|
|
override: calendar.override,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
2024-10-24 12:28:13 +00:00
|
|
|
|
|
|
|
const results = await Promise.all(promises);
|
|
|
|
// Filter out any failed requests
|
|
|
|
const validResults = results.filter((result) => result !== null);
|
2024-10-24 12:30:01 +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];
|
|
|
|
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-24 12:21:56 +00:00
|
|
|
}
|
2024-10-24 12:20:52 +00:00
|
|
|
}
|
2024-10-24 12:26:50 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).json({ error: 'Failed to retrieve calendar data.' });
|
2024-10-24 12:20:52 +00:00
|
|
|
}
|
2024-10-24 12:16:44 +00:00
|
|
|
});
|
|
|
|
|
2024-10-24 12:08:11 +00:00
|
|
|
// Serve the merged calendar file
|
2024-10-18 13:43:13 +00:00
|
|
|
app.get('/calendar/:id', (req, res) => {
|
|
|
|
const filename = `${req.params.id}.ics`;
|
|
|
|
res.setHeader('Content-Type', 'text/calendar');
|
|
|
|
res.sendFile(filename, { root: MERGED_CALENDARS_DIR });
|
|
|
|
});
|
|
|
|
|
2024-10-23 12:22:01 +00:00
|
|
|
//function to save calendar data to seperate .json files
|
|
|
|
function saveCalendarData(calendarId, linkGroupName, calendars) {
|
|
|
|
const calendarFile = `${MERGED_CALENDARS_DIR}/${calendarId}.json`;
|
|
|
|
const calendarData = {
|
|
|
|
id: calendarId,
|
|
|
|
linkGroupName: linkGroupName,
|
|
|
|
calendars: calendars
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(calendarFile, JSON.stringify(calendarData, null, 2));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error writing to calendar file:', error);
|
|
|
|
}
|
|
|
|
}
|
2024-10-19 14:41:06 +00:00
|
|
|
|
2024-10-02 11:32:35 +00:00
|
|
|
// Function to update the merged calendar
|
2024-10-22 11:29:51 +00:00
|
|
|
async function updateMergedCalendars(calendarId){
|
2024-10-02 11:32:35 +00:00
|
|
|
try {
|
2024-10-23 12:46:58 +00:00
|
|
|
// Load calendar data from the individual JSON file
|
|
|
|
const calendarFile = `${MERGED_CALENDARS_DIR}/${calendarId}.json`;
|
2024-10-23 12:52:43 +00:00
|
|
|
const calendarData = JSON.parse(fs.readFileSync(calendarFile, 'utf8'));
|
2024-10-22 11:29:51 +00:00
|
|
|
|
2024-10-19 14:41:06 +00:00
|
|
|
// Fetch calendar data for each merged calendar
|
2024-10-23 12:52:43 +00:00
|
|
|
const promises = calendarData.calendars.map((calendar) => {
|
2024-10-22 11:29:51 +00:00
|
|
|
return axios.get(calendar.url)
|
|
|
|
.then((response) => {
|
|
|
|
return {
|
|
|
|
data: response.data,
|
|
|
|
prefix: calendar.prefix,
|
|
|
|
override: calendar.override,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
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-19 14:13:45 +00:00
|
|
|
const validResults = results.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
|
2024-10-22 11:29:51 +00:00
|
|
|
const filename = `${calendarId}.ics`;
|
2024-10-02 22:11:20 +00:00
|
|
|
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-22 16:22:57 +00:00
|
|
|
DTSTART;VALUE=DATETIME:${event.start.toISOString().replace(/-|:|\.\d{3}/g, '')}
|
|
|
|
DTEND;VALUE=DATETIME:${event.end.toISOString().replace(/-|:|\.\d{3}/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
|
|
|
|
|
|
|
// Store the merged calendar URL in a file
|
2024-10-19 14:41:06 +00:00
|
|
|
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
2024-10-08 19:04:31 +00:00
|
|
|
|
2024-10-22 11:29:51 +00:00
|
|
|
console.log(`Merged calendar updated: ${calendarId}`);
|
2024-10-02 22:13:58 +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-10-21 23:40:08 +00:00
|
|
|
// Endpoint to refresh the merged calendar
|
|
|
|
app.post('/refresh/:id', async (req, res) => {
|
|
|
|
const calendarId = req.params.id;
|
2024-10-22 11:32:28 +00:00
|
|
|
try {
|
|
|
|
await updateMergedCalendars(calendarId);
|
|
|
|
res.json({ message: `Merged calendar refreshed: ${calendarId}` });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).json({ error: `Failed to refresh merged calendar: ${calendarId}` });
|
|
|
|
}
|
2024-10-21 23:40:08 +00:00
|
|
|
});
|
|
|
|
|
2024-09-30 12:55:03 +00:00
|
|
|
// Schedule a cron job to update the merged calendar every hour
|
2024-10-19 15:32:43 +00:00
|
|
|
cron.schedule('1 * * * *', () => {
|
2024-09-30 12:55:03 +00:00
|
|
|
console.log('Updating merged calendar...');
|
2024-10-22 11:35:11 +00:00
|
|
|
const calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
|
|
|
|
calendarsData.mergedCalendars.forEach((mergedCalendar) => {
|
|
|
|
updateMergedCalendars(mergedCalendar.id);
|
|
|
|
});
|
2024-10-02 11:20:21 +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}`);
|
2024-10-19 15:32:43 +00:00
|
|
|
});
|