forked from ryanmwangi/CalMerger
get rid of the updateMergedCalendars function
This commit is contained in:
parent
7c01cfc964
commit
e2d40b3fc5
76
server.js
76
server.js
|
@ -195,82 +195,6 @@ function saveCalendarData(calendarId, linkGroupName, calendars) {
|
|||
}
|
||||
}
|
||||
|
||||
// Function to update the merged calendar
|
||||
async function updateMergedCalendars(calendarId){
|
||||
try {
|
||||
// Load calendar data from the individual JSON file
|
||||
const calendarFile = `${MERGED_CALENDARS_DIR}/${calendarId}.json`;
|
||||
const calendarData = JSON.parse(fs.readFileSync(calendarFile, 'utf8'));
|
||||
|
||||
// Fetch calendar data for each merged calendar
|
||||
const promises = calendarData.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;
|
||||
});
|
||||
});
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
// Filter out any failed requests
|
||||
const validResults = results.filter((result) => result !== null);
|
||||
|
||||
// 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}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Save merged calendar to file
|
||||
const filename = `${calendarId}.ics`;
|
||||
let icalString = `BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
CALSCALE:GREGORIAN
|
||||
METHOD:PUBLISH
|
||||
`;
|
||||
mergedCal.forEach((event) => {
|
||||
icalString += `BEGIN:VEVENT
|
||||
DTSTART;VALUE=DATETIME:${event.start.toISOString().replace(/-|:|\.\d{3}/g, '')}
|
||||
DTEND;VALUE=DATETIME:${event.end.toISOString().replace(/-|:|\.\d{3}/g, '')}
|
||||
SUMMARY:${event.summary}
|
||||
END:VEVENT
|
||||
`;
|
||||
});
|
||||
icalString += `END:VCALENDAR`;
|
||||
|
||||
// Store the merged calendar URL in a file
|
||||
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
||||
|
||||
console.log(`Merged calendar updated: ${calendarId}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
// Start the server
|
||||
const port = 3000;
|
||||
app.listen(port, () => {
|
||||
|
|
Loading…
Reference in New Issue