From d34232b819e03046e641ea6c618ddd3ec1dce52c Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Wed, 17 Jan 2024 14:01:46 +0100 Subject: [PATCH] initial commit --- ics.bat | 3 + ics.js | 114 ++++++++++++++++++++++++++++++++++++++ settings.json.sample.json | 7 +++ 3 files changed, 124 insertions(+) create mode 100644 ics.bat create mode 100644 ics.js create mode 100644 settings.json.sample.json diff --git a/ics.bat b/ics.bat new file mode 100644 index 0000000..5163f4d --- /dev/null +++ b/ics.bat @@ -0,0 +1,3 @@ +@echo off +node ics +pause \ No newline at end of file diff --git a/ics.js b/ics.js new file mode 100644 index 0000000..982672a --- /dev/null +++ b/ics.js @@ -0,0 +1,114 @@ +const https = require('https'); +const fs = require('fs'); + +var settings = null; +var ics = ""; +var id = 0; + +function ics2json(input) +{ + var root = {}; + var curr = root; + input.split("\r\n").forEach(l => + { + var key = l.split(":")[0].split(";")[0]; + var val = l.split(":")[1]; + if (key == "BEGIN") + { + if (curr[val]) + { + val += "_" + (id++); + } + curr[val] = { + parent: curr + }; + curr = curr[val]; + } + else if (key == "END") + { + var parent = curr.parent; + delete curr.parent; + curr = parent; + } + else + { + curr[key] = key.startsWith("DT") ? dt(val) : val; + } + + }); + + root.VCALENDAR.VEVENTS = []; + Object.keys(root.VCALENDAR) + .filter(k => (k == "VEVENT" || k.startsWith("VEVENT_"))) + .forEach(k => + { + root.VCALENDAR.VEVENTS.push(root.VCALENDAR[k]); + delete root.VCALENDAR[k]; + }); + + return root.VCALENDAR; +} + +function dt(s) +{ + return new Date( + s.substr(0,4) + "-" + s.substr(4,2) + "-" + s.substr(6,2) + + "T" + + s.substr(9,2) + ":" + s.substr(11,2) + ":" + s.substr(13,2)); +} + +function formatevent(e) +{ + return e.DTSTART.toLocaleString('fr-FR', { timeZone: 'Europe/Paris' }) + ": " + e.SUMMARY; +} + +function main() +{ + var o = ics2json(ics); + + var lastweek = new Date(); + lastweek.setDate(lastweek.getDate() - settings.recentdays); + + console.log("A venir"); + o.VEVENTS + .filter(e => e.DTSTART >= (new Date)) + .sort( (a,b) => a.DTSTART - b.DTSTART) + .map(formatevent) + .forEach(e => console.log(e)); + + console.log("\nChangements récents (" + settings.recentdays + " jours)"); + o.VEVENTS + .filter(e => e.DTSTART >= (new Date) && e.DTSTAMP >= lastweek) + .sort( (a,b) => a.DTSTART - b.DTSTART) + .map(formatevent) + .forEach(e => console.log(e)); +} + +fs.readFile('settings.json', 'utf8', (err, data) => +{ + settings = JSON.parse(data); + + var options = + { + host: settings.host, + port: 443, + path : settings.path, + headers: { + 'Authorization': 'Basic ' + Buffer.from(settings.username + ':' + settings.passw).toString('base64') + } + }; + + request = https.get(options, function(res) + { + if (res.statusCode != 200) + { + console.log(`statusCode: ${res.statusCode}`) + } + + res.on('data', chunk => ics += chunk ); + res.on('end', main); + }); + + request.on('error', console.error); + request.end(); +}); diff --git a/settings.json.sample.json b/settings.json.sample.json new file mode 100644 index 0000000..9899ed6 --- /dev/null +++ b/settings.json.sample.json @@ -0,0 +1,7 @@ +{ + "host": "caldav.server.example", + "username": "username", + "passw": "pizz@!123", + "path": "/my/calendar/path", + "recentdays": 10 +} \ No newline at end of file