Compare commits

..

No commits in common. "be6d378890a094ca368c8c4005d3822a32cfa891" and "1bb1db73262a12e2185d5ee655ebc0898aef8f43" have entirely different histories.

View file

@ -21,7 +21,7 @@ app.get('/', (req, res) => res.sendFile('index.html', { root: '.' }));
const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g, '_'); const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g, '_');
// Fetch calendar data from URL or file // Fetch calendar data from URL or file
async function fetchCalendarData(calendar) { const fetchCalendarData = async (calendar) => {
const isFilePath = !calendar.url.startsWith('http'); const isFilePath = !calendar.url.startsWith('http');
try { try {
if (isFilePath) { if (isFilePath) {
@ -39,23 +39,23 @@ async function fetchCalendarData(calendar) {
console.error(`Error retrieving calendar from ${calendar.url}:`, error.message); console.error(`Error retrieving calendar from ${calendar.url}:`, error.message);
throw error; throw error;
} }
} };
// Create a top-level VCALENDAR component // Create a top-level VCALENDAR component
function createCalendarComponent(name) { const createCalendarComponent = (name) => {
console.log(`Creating calendar component for: ${name}`); console.log(`Creating calendar component for: ${name}`);
const calendarComponent = new ICAL.Component(['vcalendar', [], []]); const calendarComponent = new ICAL.Component(['vcalendar', [], []]);
calendarComponent.updatePropertyWithValue('prodid', '-//Your Product ID//EN'); calendarComponent.updatePropertyWithValue('prodid', '-//Your Product ID//EN');
calendarComponent.updatePropertyWithValue('version', '2.0'); calendarComponent.updatePropertyWithValue('version', '2.0');
calendarComponent.updatePropertyWithValue('name', name); // calendar name calendarComponent.updatePropertyWithValue('name', name); // calendar name
return calendarComponent; return calendarComponent;
} };
// Add events to the calendar component // Add events to the calendar component
function addEventsToCalendar(calendarComponent, results) { const addEventsToCalendar = (calendarComponent, results) => {
console.log(`Adding events to calendar component.`); console.log(`Adding events to calendar component.`);
results.forEach((result) => { results.forEach((result) => {
// console.log(result.data); console.log(result.data);
const parsed = ICAL.parse(result.data); const parsed = ICAL.parse(result.data);
const component = new ICAL.Component(parsed); const component = new ICAL.Component(parsed);
@ -64,38 +64,23 @@ function addEventsToCalendar(calendarComponent, results) {
const newEvent = new ICAL.Component('vevent'); const newEvent = new ICAL.Component('vevent');
console.log(`Adding event: ${vevent.summary} to calendar.`); console.log(`Adding event: ${vevent.summary} to calendar.`);
// Get start and end dates directly
const startDate = vevent.startDate && ICAL.Time.fromJSDate(vevent.startDate.toJSDate());
const endDate = vevent.endDate && ICAL.Time.fromJSDate(vevent.endDate.toJSDate());
// Log the start and end dates
console.log(`Start Date: ${startDate}`);
console.log(`End Date: ${endDate}`);
// // Check if the dates are valid
// if (!startDate || !endDate || !startDate.isValid() || !endDate.isValid()) {
// console.warn(`Invalid date for event: ${vevent.summary}`);
// return; // Skip or handle accordingly
// }
newEvent.updatePropertyWithValue('uid', vevent.uid); newEvent.updatePropertyWithValue('uid', vevent.uid);
newEvent.updatePropertyWithValue('summary', result.override ? result.prefix : `${result.prefix} ${vevent.summary}`); newEvent.updatePropertyWithValue('summary', result.override ? result.prefix : `${result.prefix} ${vevent.summary}`);
newEvent.updatePropertyWithValue('dtstart', startDate); newEvent.updatePropertyWithValue('dtstart', vevent.startDate.toICALString());
newEvent.updatePropertyWithValue('dtend', endDate); newEvent.updatePropertyWithValue('dtend', vevent.endDate.toICALString());
calendarComponent.addSubcomponent(newEvent); calendarComponent.addSubcomponent(newEvent);
}); });
}); });
} };
// Save calendar data to file // Save calendar data to file
function saveCalendarFile(filename, content) { const saveCalendarFile = (filename, content) => {
const filePath = path.join(MERGED_CALENDARS_DIR, filename); const filePath = path.join(MERGED_CALENDARS_DIR, filename);
console.log(`Saving calendar data to file: ${filePath}`); console.log(`Saving calendar data to file: ${filePath}`);
fs.writeFileSync(filePath, content); fs.writeFileSync(filePath, content);
return filePath; return filePath;
} };
// Merge calendars endpoint // Merge calendars endpoint
app.post('/merge', async (req, res) => { app.post('/merge', async (req, res) => {
@ -135,7 +120,7 @@ app.post('/merge', async (req, res) => {
}); });
// Refresh calendar if outdated // Refresh calendar if outdated
async function refreshCalendarData(calendarName) { const refreshCalendarData = async (calendarName) => {
const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`); const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`);
console.log(`Refreshing calendar data for: ${calendarName}`); console.log(`Refreshing calendar data for: ${calendarName}`);
@ -148,7 +133,7 @@ async function refreshCalendarData(calendarName) {
saveCalendarFile(`${calendarName}.ics`, calendarComponent.toString()); saveCalendarFile(`${calendarName}.ics`, calendarComponent.toString());
console.log('Calendar data refreshed and saved.'); console.log('Calendar data refreshed and saved.');
} };
// Serve the merged calendar file and refresh if older than an hour // Serve the merged calendar file and refresh if older than an hour
app.get('/calendar/:name', async (req, res) => { app.get('/calendar/:name', async (req, res) => {