forked from ryanmwangi/CalMerger
refactor: refactor endpoint to serve refreshed merge calendars
This commit is contained in:
parent
cdc25981b5
commit
b32696f14c
98
server.js
98
server.js
|
@ -108,6 +108,8 @@ app.post('/merge', async (req, res) => {
|
||||||
// Refresh calendar if outdated
|
// Refresh calendar if outdated
|
||||||
const refreshCalendarData = async (calendarName) => {
|
const refreshCalendarData = async (calendarName) => {
|
||||||
const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`);
|
const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`);
|
||||||
|
|
||||||
|
// Read the JSON file to get the source URL and other details
|
||||||
const { calendars } = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8'));
|
const { calendars } = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8'));
|
||||||
|
|
||||||
const results = await Promise.all(calendars.map(fetchCalendarData));
|
const results = await Promise.all(calendars.map(fetchCalendarData));
|
||||||
|
@ -121,109 +123,27 @@ const refreshCalendarData = async (calendarName) => {
|
||||||
// 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) => {
|
||||||
const calendarName = req.params.name;
|
const calendarName = req.params.name;
|
||||||
const icsFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.ics`);
|
const icsFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.ics`);
|
||||||
const jsonFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.json`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if the .ics file exists
|
// Check if the .ics file exists
|
||||||
if (fs.existsSync(icsFilePath)) {
|
if (fs.existsSync(icsFilePath)) {
|
||||||
const stats = fs.statSync(icsFilePath);
|
const stats = fs.statSync(icsFilePath);
|
||||||
const lastModified = new Date(stats.mtime);
|
const isOutdated = new Date() - new Date(stats.mtime) > 60 * 60 * 1000;
|
||||||
const now = new Date();
|
|
||||||
// Check if the file is older than one hour
|
|
||||||
if (now - lastModified > 60 * 60 * 1000) {
|
|
||||||
console .log('Refreshing calendar data...');
|
|
||||||
|
|
||||||
// Read the JSON file to get the source URL and other details
|
if (isOutdated) await refreshCalendarData(calendarName);
|
||||||
const jsonData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));
|
|
||||||
const { calendars } = jsonData;
|
|
||||||
|
|
||||||
// Fetch calendar data for each merged calendar
|
res.setHeader('Content-Type', 'text/calendar');
|
||||||
const promises = calendars.map((calendar) => {
|
res.sendFile(icsFilePath);
|
||||||
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);
|
|
||||||
|
|
||||||
// Create a new iCalendar instance
|
|
||||||
const calendar = icalGenerator({ name: calendarName });
|
|
||||||
|
|
||||||
// Parse calendar data
|
|
||||||
validResults.forEach((result) => {
|
|
||||||
const parsed = ICAL.parse(result.data);
|
|
||||||
const component = new ICAL.Component(parsed);
|
|
||||||
const events = component.getAllSubcomponents('vevent');
|
|
||||||
|
|
||||||
events.forEach((event) => {
|
|
||||||
const vevent = new ICAL.Event(event);
|
|
||||||
const start = vevent.startDate.toJSDate();
|
|
||||||
const end = vevent.endDate.toJSDate();
|
|
||||||
const summary = result.override ? result.prefix : `${result.prefix} ${vevent.summary}`;
|
|
||||||
|
|
||||||
if (vevent.startDate.isDate) {
|
|
||||||
calendar.createEvent({
|
|
||||||
start: start.toISOString().split('T')[0],
|
|
||||||
end: end.toISOString().split('T')[0],
|
|
||||||
summary: summary,
|
|
||||||
allDay: true,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
calendar.createEvent({
|
|
||||||
start: start,
|
|
||||||
end: end,
|
|
||||||
summary: summary,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Save the calendar to a file
|
|
||||||
fs.writeFileSync(icsFilePath, calendar.toString());
|
|
||||||
|
|
||||||
console.log('Calendar data refreshed.');
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return res.status(404).json({ error: 'Calendar not found.' });
|
res.status(404).json({ error: 'Calendar not found.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the contents of the .ics file
|
|
||||||
res.setHeader('Content-Type', 'text/calendar');
|
|
||||||
res.sendFile(icsFilePath);
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error('Error retrieving calendar data:', error);
|
||||||
res.status(500).json({ error: 'Failed to retrieve calendar data.' });
|
res.status(500).json({ error: 'Failed to retrieve calendar data.' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
//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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|
Loading…
Reference in New Issue