Modifying the updateMergedCalendar function to read the link group data from the calendars.json file and use it to fetch the calendar data.

This commit is contained in:
Ryan Mwangi 2024-10-14 16:03:40 +03:00
parent c80064f135
commit fcf626036d
1 changed files with 10 additions and 8 deletions

View File

@ -158,29 +158,31 @@ function addLinkToGroup(linkGroup, url, prefix, overrideSummary) {
// Function to update the merged calendar // Function to update the merged calendar
async function updateMergedCalendar(){ async function updateMergedCalendar(){
try { try {
// Load calendars data from file // Load calendars data from calendars.json file
const calendarsFile = 'calendars.json'; const calendarsFile = 'calendars.json';
const calendars = JSON.parse(fs.readFileSync(calendarsFile, 'utf8')); const calendars = JSON.parse(fs.readFileSync(calendarsFile, 'utf8'));
// Fetch calendar data from URLs // Fetch calendar data for each link group
const promises = calendars.map((calendar) => { const promises = calendarsData.linkGroups.map((linkGroup) => {
return axios.get(calendar.url) return Promise.all(linkGroup.links.map((link) => {
return axios.get(link.url)
.then((response) => { .then((response) => {
return { return {
data: response.data, data: response.data,
prefix: calendar.prefix, prefix: link.prefix,
override: calendar.override, override: link.overrideSummary,
}; };
}) })
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
return null; return null;
}); });
}));
}); });
const results = await Promise.all(promises); const results = await Promise.all(promises);
// Filter out any failed requests // Filter out any failed requests
const validResults = results.filter((result) => result !== null); const validResults = results.flat().filter((result) => result !== null);
// Parse calendar data // Parse calendar data
const mergedCal = []; const mergedCal = [];
@ -227,7 +229,7 @@ END:VEVENT
const mergedCalendarUrl = `http://localhost:3000/${filename}`; const mergedCalendarUrl = `http://localhost:3000/${filename}`;
// Store the merged calendar URL in a file // Store the merged calendar URL in a file
fs.writeFileSync(mergedCalendarUrlFile, mergedCalendarUrl); fs.writeFileSync('merged_calendar_url.txt', mergedCalendarUrl);
console.log(`Merged calendar updated: ${mergedCalendarUrl}`); console.log(`Merged calendar updated: ${mergedCalendarUrl}`);