From 5a5fcaeeaca972cb0a797e2b1c3e5a13ae035f07 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Wed, 2 Oct 2024 15:21:00 +0300 Subject: [PATCH] fetch calendar data from urls --- server.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index f223c4c..b5c3c9e 100644 --- a/server.js +++ b/server.js @@ -91,18 +91,31 @@ app.get('/:filename', (req, res) => { res.sendFile(filename, { root: '.' }); }); -// Start the server -const port = 3000; -app.listen(port, () => { - console.log(`Server started on port ${port}`); -}); - // Function to update the merged calendar async function updateMergedCalendar(){ try { // Load calendars data from file const calendarsFile = 'calendars.json'; const calendars = JSON.parse(fs.readFileSync(calendarsFile, 'utf8')); + + // Fetch calendar data from URLs + const promises = calendars.map((calendar) => { + return axios.get(calendar.url) + .then((response) => { + return { + data: response.data, + prefix: calendar.prefix, + }; + }) + .catch((error) => { + console.error(error); + return null; + }); + }); + + const results = await Promise.all(promises); + } catch (error) { + console.error(error); } } // Schedule a cron job to update the merged calendar every hour @@ -110,3 +123,9 @@ cron.schedule('0 * * * *', () => { console.log('Updating merged calendar...'); // TO DO: implement the logic to update the merged calendar }); + +// Start the server +const port = 3000; +app.listen(port, () => { + console.log(`Server started on port ${port}`); +});