mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
Add fetcher_helper for calendar and newsfeed
This commit is contained in:
parent
a6879e853b
commit
90aa50bb11
12
js/fetcher_helper.js
Normal file
12
js/fetcher_helper.js
Normal file
@ -0,0 +1,12 @@
|
||||
const FetcherHelper = {
|
||||
checkStatus: function (response) {
|
||||
// response.status >= 200 && response.status < 300
|
||||
if (response.ok) {
|
||||
return response;
|
||||
} else {
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = FetcherHelper;
|
@ -5,6 +5,7 @@
|
||||
* MIT Licensed.
|
||||
*/
|
||||
const CalendarUtils = require("./calendarutils");
|
||||
const FetcherHelper = require("fetcher_helper");
|
||||
const Log = require("logger");
|
||||
const ical = require("node-ical");
|
||||
const fetch = require("node-fetch");
|
||||
@ -62,17 +63,7 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
|
||||
}
|
||||
|
||||
fetcher
|
||||
.catch((error) => {
|
||||
fetchFailedCallback(this, error);
|
||||
scheduleTimer();
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.status !== 200) {
|
||||
fetchFailedCallback(this, response.statusText);
|
||||
scheduleTimer();
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.then(FetcherHelper.checkStatus)
|
||||
.then((response) => response.text())
|
||||
.then((responseData) => {
|
||||
let data = [];
|
||||
@ -93,6 +84,10 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
|
||||
}
|
||||
this.broadcastEvents();
|
||||
scheduleTimer();
|
||||
})
|
||||
.catch((error) => {
|
||||
fetchFailedCallback(this, error.message);
|
||||
scheduleTimer();
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -46,7 +46,7 @@ module.exports = NodeHelper.create({
|
||||
|
||||
let fetcher;
|
||||
if (typeof this.fetchers[identifier + url] === "undefined") {
|
||||
Log.log("Create new calendar fetcher for url: " + url + " - Interval: " + fetchInterval);
|
||||
Log.log("Create new calendarfetcher for url: " + url + " - Interval: " + fetchInterval);
|
||||
fetcher = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);
|
||||
|
||||
fetcher.onReceive((fetcher) => {
|
||||
@ -64,7 +64,7 @@ module.exports = NodeHelper.create({
|
||||
|
||||
this.fetchers[identifier + url] = fetcher;
|
||||
} else {
|
||||
Log.log("Use existing calendar fetcher for url: " + url);
|
||||
Log.log("Use existing calendarfetcher for url: " + url);
|
||||
fetcher = this.fetchers[identifier + url];
|
||||
fetcher.broadcastEvents();
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ Module.register("newsfeed", {
|
||||
this.loaded = true;
|
||||
this.error = null;
|
||||
} else if (notification === "FETCH_ERROR") {
|
||||
this.error = `Could not fetch newsfeed ${payload.url}`;
|
||||
this.error = `${payload.error}`;
|
||||
this.scheduleUpdateInterval();
|
||||
} else if (notification === "INCORRECT_URL") {
|
||||
this.error = `Incorrect url: ${payload.url}`;
|
||||
|
@ -5,6 +5,7 @@
|
||||
* MIT Licensed.
|
||||
*/
|
||||
const Log = require("logger");
|
||||
const FetcherHelper = require("fetcher_helper");
|
||||
const FeedMe = require("feedme");
|
||||
const fetch = require("node-fetch");
|
||||
const iconv = require("iconv-lite");
|
||||
@ -84,12 +85,13 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
||||
};
|
||||
|
||||
fetch(url, { headers: headers })
|
||||
.catch((error) => {
|
||||
fetchFailedCallback(this, error);
|
||||
scheduleTimer();
|
||||
.then(FetcherHelper.checkStatus)
|
||||
.then((response) => {
|
||||
response.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
||||
})
|
||||
.then((res) => {
|
||||
res.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
||||
.catch((error) => {
|
||||
fetchFailedCallback(this, error.message);
|
||||
scheduleTimer();
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -44,7 +44,7 @@ module.exports = NodeHelper.create({
|
||||
|
||||
let fetcher;
|
||||
if (typeof this.fetchers[url] === "undefined") {
|
||||
Log.log("Create new news fetcher for url: " + url + " - Interval: " + reloadInterval);
|
||||
Log.log("Create new newsfetcher for url: " + url + " - Interval: " + reloadInterval);
|
||||
fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings);
|
||||
|
||||
fetcher.onReceive(() => {
|
||||
@ -61,7 +61,7 @@ module.exports = NodeHelper.create({
|
||||
|
||||
this.fetchers[url] = fetcher;
|
||||
} else {
|
||||
Log.log("Use existing news fetcher for url: " + url);
|
||||
Log.log("Use existing newsfetcher for url: " + url);
|
||||
fetcher = this.fetchers[url];
|
||||
fetcher.setReloadInterval(reloadInterval);
|
||||
fetcher.broadcastItems();
|
||||
|
@ -87,6 +87,7 @@
|
||||
"socket.io": "^4.1.2"
|
||||
},
|
||||
"_moduleAliases": {
|
||||
"fetcher_helper": "js/fetcher_helper.js",
|
||||
"node_helper": "js/node_helper.js",
|
||||
"logger": "js/logger.js"
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user