restructure calendars.json

This commit is contained in:
Ryan Mwangi 2024-10-19 18:32:43 +03:00
parent 3901a5d894
commit 0e7e3e7804
2 changed files with 23 additions and 15 deletions

View File

@ -1,32 +1,36 @@
{ {
"linkGroups": [ "mergedCalendars": [
{ {
"name": "Group 1", "id": "uniqueId1",
"links": [ "calendars": [
{ {
"linkGroupName": "Group 1",
"url": "https://example.com/calendar1.ics", "url": "https://example.com/calendar1.ics",
"prefix": "Calendar 1", "prefix": "Calendar 1",
"overrideSummary": false "override": false
}, },
{ {
"linkGroupName": "Group 1",
"url": "https://example.com/calendar2.ics", "url": "https://example.com/calendar2.ics",
"prefix": "Calendar 2", "prefix": "Calendar 2",
"overrideSummary": true "override": true
} }
] ]
}, },
{ {
"name": "Group 2", "id": "uniqueId2",
"links": [ "calendars": [
{ {
"linkGroupName": "Group 2",
"url": "https://example.com/calendar3.ics", "url": "https://example.com/calendar3.ics",
"prefix": "Calendar 3", "prefix": "Calendar 3",
"overrideSummary": false "override": false
}, },
{ {
"linkGroupName": "Group 2",
"url": "https://example.com/calendar4.ics", "url": "https://example.com/calendar4.ics",
"prefix": "Calendar 4", "prefix": "Calendar 4",
"overrideSummary": true "override": true
} }
] ]
} }

View File

@ -16,6 +16,11 @@ if (!fs.existsSync(MERGED_CALENDARS_DIR)) {
fs.mkdirSync(MERGED_CALENDARS_DIR); fs.mkdirSync(MERGED_CALENDARS_DIR);
} }
app.get('/script.js', (req, res) => {
res.setHeader('Content-Type', 'application/javascript');
res.sendFile('script.js', { root: '.' });
});
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.sendFile('index.html', { root: '.' }); res.sendFile('index.html', { root: '.' });
}); });
@ -73,7 +78,6 @@ app.post('/merge', async (req, res) => {
}); });
}); });
// Save merged calendar to file with unique identifier // Save merged calendar to file with unique identifier
const filename = `${calendarId}.ics`; const filename = `${calendarId}.ics`;
let icalString = `BEGIN:VCALENDAR let icalString = `BEGIN:VCALENDAR
@ -84,14 +88,14 @@ METHOD:PUBLISH
mergedCal.forEach((event) => { mergedCal.forEach((event) => {
icalString += `BEGIN:VEVENT icalString += `BEGIN:VEVENT
DTSTART;VALUE=DATE:${event.start.toISOString().split('T')[0].replace(/-/g, '')} DTSTART;VALUE=DATE:${event.start.toISOString().split('T')[0].replace(/-/g, '')}
DTEND;VALUE=DATE:${event.end.toISOString().split('T')[0].replace(/-/g, '')} DTEND;VALUE=DATE:${ event.end.toISOString().split('T')[0].replace(/-/g, '')}
SUMMARY:${event.summary} SUMMARY:${event.summary}
END:VEVENT END:VEVENT
`; `;
}); });
icalString += `END:VCALENDAR`; icalString += `END:VCALENDAR`;
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString); fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
// Save the user input and generated ID in calendars.json file // Save the user input and generated ID in calendars.json file
saveCalendarData(calendarId, calendars); saveCalendarData(calendarId, calendars);
@ -191,7 +195,7 @@ END:VEVENT
// Store the merged calendar URL in a file // Store the merged calendar URL in a file
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString); fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
console.log(`Merged calendar updated: ${mergedCalendarUrl}`); console.log(`Merged calendar updated: ${mergedCalendar.id}`);
} }
} catch (error) { } catch (error) {
@ -200,7 +204,7 @@ END:VEVENT
} }
// Schedule a cron job to update the merged calendar every hour // Schedule a cron job to update the merged calendar every hour
cron.schedule('*/3 * * * *', () => { cron.schedule('1 * * * *', () => {
console.log('Updating merged calendar...'); console.log('Updating merged calendar...');
updateMergedCalendars(); updateMergedCalendars();
}); });
@ -209,4 +213,4 @@ cron.schedule('*/3 * * * *', () => {
const port = 3000; const port = 3000;
app.listen(port, () => { app.listen(port, () => {
console.log(`Server started on port ${port}`); console.log(`Server started on port ${port}`);
}); });