change: use php script

This commit is contained in:
quenousimporte 2024-01-22 14:16:35 +01:00
parent b6c0b5e558
commit c58913f25c
6 changed files with 154 additions and 144 deletions

View File

@ -1,3 +0,0 @@
@echo off
node ics
pause

129
ics.js
View File

@ -1,129 +0,0 @@
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 displayevents(events, func, title, created)
{
var group = {};
events
.filter(func)
.sort( (a,b) => a.DTSTART - b.DTSTART)
.forEach(e => {
var formatteddate = e.DTSTART.toLocaleString('fr-FR', { timeZone: 'Europe/Paris', dateStyle: "full", timeStyle: "short" });
var splitdate = formatteddate.split(" ");
var year = splitdate[3];
var month = splitdate[2];
group[year] = group[year] || {};
group[year][month] = group[year][month] || [];
group[year][month].push(formatteddate + ": " + e.SUMMARY + (created ? ` (modifié le ${e.DTSTAMP.toLocaleString('fr-FR', { timeZone: 'Europe/Paris', dateStyle: "full" })})` : ""));
});
console.log(title);
for (var year in group)
{
console.log(year);
for (var month in group[year])
{
console.log(" " + month);
group[year][month].forEach(e =>
{
console.log(" " + e);
});
}
}
console.log("");
}
function main()
{
var o = ics2json(ics);
displayevents(o.VEVENTS, e => e.DTSTART >= (new Date), "A venir");
var lastweek = new Date();
lastweek.setDate(lastweek.getDate() - settings.recentdays);
displayevents(o.VEVENTS, e => e.DTSTART >= (new Date) && e.DTSTAMP >= lastweek, "Changements récents", true);
}
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();
});

150
ics.php Normal file
View File

@ -0,0 +1,150 @@
<?php
require 'settings.php';
if (isset($_POST['password']))
{
$password = $_POST['password'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, $user . ':' . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if (!$result)
{
die('error');
}
curl_close($curl);
die($result);
}
?>
<html>
<body>
<script type="text/javascript">
function ics2json(input)
{
var id = 0;
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 formatdate(d)
{
return d.toLocaleString('fr-FR', { timeZone: 'Europe/Paris', dateStyle: "full", timeStyle: "short" });
}
function showresult()
{
if (xhr.status == 200)
{
var params = new URLSearchParams(window.location.search);
var recent = parseInt(params.get("recent")) || 7;
var html = "<h1>Evénements à venir</h1>";
html += "<p>En gras: modifié les " + recent + " derniers jours</p>";
var o = ics2json(xhr.responseText);
var lastweek = new Date();
lastweek.setDate(lastweek.getDate() - recent);
var html = "";
var group = {};
o.VEVENTS
.filter(e => e.DTSTART >= (new Date))
.sort( (a,b) => a.DTSTART - b.DTSTART)
.forEach(e => {
var formatteddate = formatdate(e.DTSTART);
var splitdate = formatteddate.split(" ");
var year = splitdate[3];
var month = splitdate[2];
if (!group[year])
{
html += `<h2>${year}</h2>`;
group[year] = {};
}
if (!group[year][month])
{
html += `<h3>${month}</h3>`;
group[year][month] = true;
}
var line = `<li title="modifié le ${formatdate(e.DTSTAMP)}"> ${formatteddate}: ${e.SUMMARY}`;
if (e.DTSTAMP >= lastweek)
{
line = `<b>${line}</b>`;
}
html += line;
});
document.body.innerHTML = html;
}
}
var password = localStorage.getItem("icspassword");
if (!password)
{
password = prompt("password:");
localStorage.setItem("icspassword", password);
}
var xhr = new XMLHttpRequest();
xhr.onload = showresult;
xhr.open("POST", "ics.php");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send("password=" + password);
</script>
</body>
</html>

View File

@ -1,8 +1,3 @@
# ics tool # ics tool
Script to display recently added or modified events in a calendar fetched from a caldav server. Script to display recently added or modified events in a calendar fetched from a caldav server.
Usage:
* Rename settings.json.sample to settings.json
* Edit settings.json
* run `node ics`

View File

@ -1,7 +0,0 @@
{
"host": "caldav.server.example",
"username": "username",
"passw": "pizz@!123",
"path": "/my/calendar/path",
"recentdays": 10
}

4
settings.php.sample.php Normal file
View File

@ -0,0 +1,4 @@
<?php
$url = 'https://caldav.example.com/path/to/calendar';
$user = 'caldavuser';
?>