Install latest ical version and use it

This commit is contained in:
rejas 2020-06-17 21:17:26 +02:00
parent 6d3308621f
commit 6d60baa2d6
3 changed files with 279 additions and 261 deletions

View File

@ -5,7 +5,8 @@
* MIT Licensed. * MIT Licensed.
*/ */
const Log = require("../../../js/logger.js"); const Log = require("../../../js/logger.js");
const ical = require("./vendor/ical.js"); const fetch = require("node-fetch");
const ical = require("ical");
const moment = require("moment"); const moment = require("moment");
var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents) { var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents) {
@ -20,57 +21,50 @@ var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntr
/* fetchCalendar() /* fetchCalendar()
* Initiates calendar fetch. * Initiates calendar fetch.
*/ */
var fetchCalendar = function () { const fetchCalendar = function () {
clearTimeout(reloadTimer); clearTimeout(reloadTimer);
reloadTimer = null; reloadTimer = null;
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]); const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
var opts = { const opts = {
headers: { headers: {
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)" "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
}, },
gzip: true compress: true
}; };
if (auth) { if (auth) {
if (auth.method === "bearer") { if (auth.method === "bearer") {
opts.auth = { opts.headers.Authorization = `Bearer ${auth.pass}`;
bearer: auth.pass
};
} else { } else {
opts.auth = { let base64data = Buffer.from(`${auth.user}:${auth.pass}`).toString("base64");
user: auth.user, opts.headers.Authorization = `Basic ${base64data}`;
pass: auth.pass
};
// TODO
if (auth.method === "digest") { if (auth.method === "digest") {
opts.auth.sendImmediately = false; //opts.auth.sendImmediately = false;
} else {
opts.auth.sendImmediately = true;
} }
} }
} }
ical.fromURL(url, opts, function (err, data) { fetch(url, opts)
if (err) { .then((response) => response.text())
fetchFailedCallback(self, err); .then((rawData) => {
scheduleTimer(); const data = ical.parseICS(rawData);
return; const newEvents = [];
}
var newEvents = [];
// limitFunction doesn't do much limiting, see comment re: the dates array in rrule section below as to why we need to do the filtering ourselves // limitFunction doesn't do much limiting, see comment re: the dates array in rrule section below as to why we need to do the filtering ourselves
var limitFunction = function (date, i) { const limitFunction = function (date, i) {
return true; return true;
}; };
var eventDate = function (event, time) { const eventDate = function (event, time) {
return event[time].length === 8 ? moment(event[time], "YYYYMMDD") : moment(new Date(event[time])); return event[time].length === 8 ? moment(event[time], "YYYYMMDD") : moment(new Date(event[time]));
}; };
for (var e in data) { for (let k in data) {
var event = data[e]; if (data.hasOwnProperty(k)) {
var event = data[k];
var now = new Date(); var now = new Date();
var today = moment().startOf("day").toDate(); var today = moment().startOf("day").toDate();
var future = moment().startOf("day").add(maximumNumberOfDays, "days").subtract(1, "seconds").toDate(); // Subtract 1 second so that events that start on the middle of the night will not repeat. var future = moment().startOf("day").add(maximumNumberOfDays, "days").subtract(1, "seconds").toDate(); // Subtract 1 second so that events that start on the middle of the night will not repeat.
@ -322,6 +316,7 @@ var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntr
} }
} }
} }
}
newEvents.sort(function (a, b) { newEvents.sort(function (a, b) {
return a.startDate - b.startDate; return a.startDate - b.startDate;
@ -331,6 +326,10 @@ var CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntr
self.broadcastEvents(); self.broadcastEvents();
scheduleTimer(); scheduleTimer();
})
.catch((err) => {
fetchFailedCallback(self, err);
scheduleTimer();
}); });
}; };

21
package-lock.json generated
View File

@ -3235,6 +3235,24 @@
"integrity": "sha512-Ty8UblRWFEcfSuIaajM34LdPXIhbs1ajEX/BBPv24J+enSVaEVY63xQ6lTO9VRYS5LAoghIG0IDJ+p+IPzKUQQ==", "integrity": "sha512-Ty8UblRWFEcfSuIaajM34LdPXIhbs1ajEX/BBPv24J+enSVaEVY63xQ6lTO9VRYS5LAoghIG0IDJ+p+IPzKUQQ==",
"dev": true "dev": true
}, },
"ical": {
"version": "0.8.0",
"resolved": "https://registry.npmjs.org/ical/-/ical-0.8.0.tgz",
"integrity": "sha512-/viUSb/RGLLnlgm0lWRlPBtVeQguQRErSPYl3ugnUaKUnzQswKqOG3M8/P1v1AB5NJwlHTuvTq1cs4mpeG2rCg==",
"requires": {
"rrule": "2.4.1"
},
"dependencies": {
"rrule": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/rrule/-/rrule-2.4.1.tgz",
"integrity": "sha512-+NcvhETefswZq13T8nkuEnnQ6YgUeZaqMqVbp+ZiFDPCbp3AVgQIwUvNVDdMNrP05bKZG9ddDULFp0qZZYDrxg==",
"requires": {
"luxon": "^1.3.3"
}
}
}
},
"iconv-lite": { "iconv-lite": {
"version": "0.5.1", "version": "0.5.1",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.5.1.tgz", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.5.1.tgz",
@ -4714,8 +4732,7 @@
"node-fetch": { "node-fetch": {
"version": "2.6.0", "version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==", "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
"dev": true
}, },
"node-releases": { "node-releases": {
"version": "1.1.53", "version": "1.1.53",

View File

@ -72,10 +72,12 @@
"express-ipfilter": "^1.0.1", "express-ipfilter": "^1.0.1",
"feedme": "latest", "feedme": "latest",
"helmet": "^3.21.2", "helmet": "^3.21.2",
"ical": "^0.8.0",
"iconv-lite": "latest", "iconv-lite": "latest",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"moment": "latest", "moment": "latest",
"node-fetch": "^2.6.0",
"request": "^2.88.0", "request": "^2.88.0",
"rrule": "^2.6.2", "rrule": "^2.6.2",
"rrule-alt": "^2.2.8", "rrule-alt": "^2.2.8",