icstool/ics.js

115 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-01-17 14:01:46 +01:00
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();
});