Compare commits

...

6 Commits

4 changed files with 45 additions and 20 deletions

View File

@ -1,10 +1,10 @@
[ [
{ {
"url": "https://example.com/calendar1.ics", "url": "https://calendar.google.com/calendar/ical/b4c66eb4bb2cc15257d071bab3f935385778b042112ea1aaedada47f3f1a6e3a%40group.calendar.google.com/public/basic.ics",
"prefix": "Calendar 1" "prefix": "work"
}, },
{ {
"url": "https://example.com/calendar2.ics", "url": "https://calendar.google.com/calendar/ical/774518759e4e2dd42aab0c511b02397bb216f3b67dc425d76bae07d1eff529d6%40group.calendar.google.com/public/basic.ics",
"prefix": "Calendar 2" "prefix": "Calendar 2"
} }
] ]

View File

@ -32,6 +32,8 @@
<div id="calendars"> <div id="calendars">
<div class="calendar"> <div class="calendar">
<input type="text" id="prefix-0" placeholder="Prefix"> <input type="text" id="prefix-0" placeholder="Prefix">
<input type="checkbox" id="override-0">
<label for="override-0">Override</label>
<input type="url" id="url-0" placeholder="Calendar URL"> <input type="url" id="url-0" placeholder="Calendar URL">
</div> </div>
</div> </div>
@ -42,5 +44,4 @@
<script src="script.js"></script> <script src="script.js"></script>
</body> </body>
</html> </html>

View File

@ -10,6 +10,8 @@ const form = document.getElementById('merge-form');
newCalendar.className = 'calendar'; newCalendar.className = 'calendar';
newCalendar.innerHTML = ` newCalendar.innerHTML = `
<input type="text" id="prefix-${calendarIndex}" placeholder="Prefix"> <input type="text" id="prefix-${calendarIndex}" placeholder="Prefix">
<input type="checkbox" id="override-${calendarIndex}">
<label for="override-${calendarIndex}">Override</label>
<input type="url" id="url-${calendarIndex}" placeholder="Calendar URL"> <input type="url" id="url-${calendarIndex}" placeholder="Calendar URL">
`; `;
calendars.appendChild(newCalendar); calendars.appendChild(newCalendar);
@ -21,8 +23,9 @@ const form = document.getElementById('merge-form');
const calendarsData = []; const calendarsData = [];
for (let i = 0; i < calendarIndex; i++) { for (let i = 0; i < calendarIndex; i++) {
const prefix = document.getElementById(`prefix-${i}`).value; const prefix = document.getElementById(`prefix-${i}`).value;
const override = document.getElementById(`override-${i}`).checked;
const url = document.getElementById(`url-${i}`).value; const url = document.getElementById(`url-${i}`).value;
calendarsData.push({ prefix, url }); calendarsData.push({ prefix, override, url });
} }
fetch('/merge', { fetch('/merge', {
method: 'POST', method: 'POST',

View File

@ -28,6 +28,7 @@ app.post('/merge', async (req, res) => {
return { return {
data: response.data, data: response.data,
prefix: calendar.prefix, prefix: calendar.prefix,
override: calendar.override,
}; };
}) })
.catch((error) => { .catch((error) => {
@ -46,11 +47,19 @@ app.post('/merge', async (req, res) => {
const calendar = ical.parseICS(result.data); const calendar = ical.parseICS(result.data);
Object.keys(calendar).forEach((key) => { Object.keys(calendar).forEach((key) => {
const event = calendar[key]; const event = calendar[key];
mergedCal.push({ if (result.override) {
start: event.start, mergedCal.push({
end: event.end, start: event.start,
summary: `${result.prefix} ${event.summary}`, end: event.end,
}); summary: result.prefix,
});
} else {
mergedCal.push({
start: event.start,
end: event.end,
summary: `${result.prefix} ${event.summary}`,
});
}
}); });
}); });
@ -61,10 +70,10 @@ app.post('/merge', async (req, res) => {
VERSION:2.0 VERSION:2.0
CALSCALE:GREGORIAN CALSCALE:GREGORIAN
METHOD:PUBLISH METHOD:PUBLISH
BEGIN:VEVENT
`; `;
mergedCal.forEach((event) => { mergedCal.forEach((event) => {
icalString += `DTSTART:${event.start} icalString += `BEGIN:VEVENT
DTSTART:${event.start}
DTEND:${event.end} DTEND:${event.end}
SUMMARY:${event.summary} SUMMARY:${event.summary}
END:VEVENT END:VEVENT
@ -91,6 +100,9 @@ app.get('/:filename', (req, res) => {
res.sendFile(filename, { root: '.' }); res.sendFile(filename, { root: '.' });
}); });
// Store the merged calendar URL in a file
const mergedCalendarUrlFile = 'merged_calendar_url.txt';
// Function to update the merged calendar // Function to update the merged calendar
async function updateMergedCalendar(){ async function updateMergedCalendar(){
try { try {
@ -105,6 +117,7 @@ async function updateMergedCalendar(){
return { return {
data: response.data, data: response.data,
prefix: calendar.prefix, prefix: calendar.prefix,
override: calendar.override,
}; };
}) })
.catch((error) => { .catch((error) => {
@ -123,11 +136,19 @@ async function updateMergedCalendar(){
const calendar = ical.parseICS(result.data); const calendar = ical.parseICS(result.data);
Object.keys(calendar).forEach((key) => { Object.keys(calendar).forEach((key) => {
const event = calendar[key]; const event = calendar[key];
mergedCal.push({ if (result.override) {
start: event.start, mergedCal.push({
end: event.end, start: event.start,
summary: `${result.prefix} ${event.summary}`, end: event.end,
}); summary: result.prefix,
});
} else {
mergedCal.push({
start: event.start,
end: event.end,
summary: `${result.prefix} ${event.summary}`,
});
}
}); });
}); });
@ -149,8 +170,8 @@ END:VEVENT
icalString += `END:VCALENDAR`; icalString += `END:VCALENDAR`;
fs.writeFileSync(filename, icalString); fs.writeFileSync(filename, icalString);
// Generate a unique URL for the merged calendar // Generate a unique URL for the merged calendar
const mergedCalendarUrl = `http://localhost:3000/${filename}`; const mergedCalendarUrl = `${req.protocol}://${req.get('host')}/${filename}`;
console.log(`Merged calendar updated: ${mergedCalendarUrl}`); console.log(`Merged calendar updated: ${mergedCalendarUrl}`);