Undo switch to fetch, use request like ical did

This commit is contained in:
rejas 2020-06-20 08:32:54 +02:00
parent 7d521ed3ce
commit 5d4a575919
3 changed files with 654 additions and 524 deletions

View File

@ -5,9 +5,9 @@
* MIT Licensed. * MIT Licensed.
*/ */
const Log = require("../../../js/logger.js"); const Log = require("../../../js/logger.js");
const fetch = require("node-fetch");
const ical = require("ical"); const ical = require("ical");
const moment = require("moment"); const moment = require("moment");
const request = require("request");
const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents) { const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents) {
const self = this; const self = this;
@ -30,27 +30,32 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
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/)"
}, },
compress: true gzip: true
}; };
if (auth) { if (auth) {
if (auth.method === "bearer") { if (auth.method === "bearer") {
opts.headers.Authorization = `Bearer ${auth.pass}`; opts.auth = {
bearer: auth.pass
};
} else { } else {
let base64data = Buffer.from(`${auth.user}:${auth.pass}`).toString("base64"); opts.auth = {
opts.headers.Authorization = `Basic ${base64data}`; user: auth.user,
pass: auth.pass,
// TODO sendImmediately: auth.method !== "digest"
if (auth.method === "digest") { };
//opts.auth.sendImmediately = false;
}
} }
} }
fetch(url, opts) request(url, opts, function (err, r, requestData) {
.then((response) => response.text()) if (err) {
.then((rawData) => { fetchFailedCallback(self, err);
const data = ical.parseICS(rawData); scheduleTimer();
} else if (r.statusCode !== 200) {
fetchFailedCallback(self, r.statusCode + ": " + r.statusMessage);
scheduleTimer();
} else {
const data = ical.parseICS(requestData);
const newEvents = []; const 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
@ -325,10 +330,7 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
self.broadcastEvents(); self.broadcastEvents();
scheduleTimer(); scheduleTimer();
}) }
.catch((err) => {
fetchFailedCallback(self, err);
scheduleTimer();
}); });
}; };

1136
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -77,7 +77,7 @@
"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.2",
"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",