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",
"prefix": "Calendar 1"
"url": "https://calendar.google.com/calendar/ical/b4c66eb4bb2cc15257d071bab3f935385778b042112ea1aaedada47f3f1a6e3a%40group.calendar.google.com/public/basic.ics",
"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"
}
]

View File

@ -32,6 +32,8 @@
<div id="calendars">
<div class="calendar">
<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">
</div>
</div>
@ -42,5 +44,4 @@
<script src="script.js"></script>
</body>
</html>
</html>

View File

@ -10,6 +10,8 @@ const form = document.getElementById('merge-form');
newCalendar.className = 'calendar';
newCalendar.innerHTML = `
<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">
`;
calendars.appendChild(newCalendar);
@ -21,8 +23,9 @@ const form = document.getElementById('merge-form');
const calendarsData = [];
for (let i = 0; i < calendarIndex; i++) {
const prefix = document.getElementById(`prefix-${i}`).value;
const override = document.getElementById(`override-${i}`).checked;
const url = document.getElementById(`url-${i}`).value;
calendarsData.push({ prefix, url });
calendarsData.push({ prefix, override, url });
}
fetch('/merge', {
method: 'POST',

View File

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