This commit is contained in:
Karsten Hassel 2021-03-02 00:17:13 +01:00
parent ea6eebd809
commit 53e300bd31
3 changed files with 36 additions and 34 deletions

View File

@ -6,7 +6,8 @@
*/ */
const Log = require("logger"); const Log = require("logger");
const ical = require("node-ical"); const ical = require("node-ical");
const request = require("request"); const fetch = require("node-fetch");
const https = require('https');
/** /**
* Moment date * Moment date
@ -43,22 +44,22 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
const fetchCalendar = function () { const fetchCalendar = function () {
clearTimeout(reloadTimer); clearTimeout(reloadTimer);
reloadTimer = null; reloadTimer = null;
httpsAgent = null;
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]); const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
const opts = { const 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
}; };
if (selfSignedCert) { if (selfSignedCert) {
var agentOptions = { httpsAgent = new https.Agent({
rejectUnauthorized: false rejectUnauthorized: false,
}; });
opts.agentOptions = agentOptions;
} }
// todo: auth,
// https://github.com/devfans/digest-fetch
// https://hackersandslackers.com/making-api-requests-with-nodejs/
if (auth) { if (auth) {
if (auth.method === "bearer") { if (auth.method === "bearer") {
opts.auth = { opts.auth = {
@ -73,25 +74,27 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
} }
} }
request(url, opts, function (err, r, requestData) { fetch(url, { headers: headers, httpsAgent: httpsAgent })
if (err) { .catch(error => {
fetchFailedCallback(self, err); fetchFailedCallback(self, error);
scheduleTimer(); scheduleTimer();
return; })
} else if (r.statusCode !== 200) { .then(response => {
fetchFailedCallback(self, r.statusCode + ": " + r.statusMessage); if (response.status !== 200) {
scheduleTimer(); throw new Error(response.status + ": " + response.statusText);
return;
} }
return response;
})
.then(response => response.text())
.then(responseData => {
let data = []; let data = [];
try { try {
data = ical.parseICS(requestData); data = ical.parseICS(responseData);
} catch (error) { } catch (error) {
fetchFailedCallback(self, error.message); fetchFailedCallback(self, error.message);
scheduleTimer(); scheduleTimer();
return;
} }
Log.debug(" parsed data=" + JSON.stringify(data)); Log.debug(" parsed data=" + JSON.stringify(data));

View File

@ -6,7 +6,7 @@
*/ */
const Log = require("logger"); const Log = require("logger");
const FeedMe = require("feedme"); const FeedMe = require("feedme");
const request = require("request"); const fetch = require("node-fetch");
const iconv = require("iconv-lite"); const iconv = require("iconv-lite");
/** /**
@ -79,22 +79,21 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
}); });
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]); const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
const opts = { const 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/)", "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", Pragma: "no-cache"
Pragma: "no-cache"
},
encoding: null
}; };
request(url, opts) fetch(url, { headers: headers })
.on("error", function (error) { .catch(error => {
fetchFailedCallback(self, error); fetchFailedCallback(self, error);
scheduleTimer(); scheduleTimer();
}) })
.pipe(iconv.decodeStream(encoding)) .then(res => {
.pipe(parser); res.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
});
}; };
/** /**

View File

@ -80,8 +80,8 @@
"iconv-lite": "^0.6.2", "iconv-lite": "^0.6.2",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"moment": "^2.29.1", "moment": "^2.29.1",
"node-fetch": "^2.6.1",
"node-ical": "^0.12.7", "node-ical": "^0.12.7",
"request": "^2.88.2",
"rrule": "^2.6.6", "rrule": "^2.6.6",
"rrule-alt": "^2.2.8", "rrule-alt": "^2.2.8",
"simple-git": "^2.31.0", "simple-git": "^2.31.0",