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.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
const CalendarUtils = require("./calendarutils");
|
const CalendarUtils = require("./calendarutils");
|
||||||
|
const FetcherHelper = require("fetcher_helper");
|
||||||
const Log = require("logger");
|
const Log = require("logger");
|
||||||
const ical = require("node-ical");
|
const ical = require("node-ical");
|
||||||
const fetch = require("node-fetch");
|
const fetch = require("node-fetch");
|
||||||
@ -62,17 +63,7 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetcher
|
fetcher
|
||||||
.catch((error) => {
|
.then(FetcherHelper.checkStatus)
|
||||||
fetchFailedCallback(this, error);
|
|
||||||
scheduleTimer();
|
|
||||||
})
|
|
||||||
.then((response) => {
|
|
||||||
if (response.status !== 200) {
|
|
||||||
fetchFailedCallback(this, response.statusText);
|
|
||||||
scheduleTimer();
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
})
|
|
||||||
.then((response) => response.text())
|
.then((response) => response.text())
|
||||||
.then((responseData) => {
|
.then((responseData) => {
|
||||||
let data = [];
|
let data = [];
|
||||||
@ -93,6 +84,10 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
|
|||||||
}
|
}
|
||||||
this.broadcastEvents();
|
this.broadcastEvents();
|
||||||
scheduleTimer();
|
scheduleTimer();
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
fetchFailedCallback(this, error.message);
|
||||||
|
scheduleTimer();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ module.exports = NodeHelper.create({
|
|||||||
|
|
||||||
let fetcher;
|
let fetcher;
|
||||||
if (typeof this.fetchers[identifier + url] === "undefined") {
|
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 = new CalendarFetcher(url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);
|
||||||
|
|
||||||
fetcher.onReceive((fetcher) => {
|
fetcher.onReceive((fetcher) => {
|
||||||
@ -64,7 +64,7 @@ module.exports = NodeHelper.create({
|
|||||||
|
|
||||||
this.fetchers[identifier + url] = fetcher;
|
this.fetchers[identifier + url] = fetcher;
|
||||||
} else {
|
} else {
|
||||||
Log.log("Use existing calendar fetcher for url: " + url);
|
Log.log("Use existing calendarfetcher for url: " + url);
|
||||||
fetcher = this.fetchers[identifier + url];
|
fetcher = this.fetchers[identifier + url];
|
||||||
fetcher.broadcastEvents();
|
fetcher.broadcastEvents();
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ Module.register("newsfeed", {
|
|||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
this.error = null;
|
this.error = null;
|
||||||
} else if (notification === "FETCH_ERROR") {
|
} else if (notification === "FETCH_ERROR") {
|
||||||
this.error = `Could not fetch newsfeed ${payload.url}`;
|
this.error = `${payload.error}`;
|
||||||
this.scheduleUpdateInterval();
|
this.scheduleUpdateInterval();
|
||||||
} else if (notification === "INCORRECT_URL") {
|
} else if (notification === "INCORRECT_URL") {
|
||||||
this.error = `Incorrect url: ${payload.url}`;
|
this.error = `Incorrect url: ${payload.url}`;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
const Log = require("logger");
|
const Log = require("logger");
|
||||||
|
const FetcherHelper = require("fetcher_helper");
|
||||||
const FeedMe = require("feedme");
|
const FeedMe = require("feedme");
|
||||||
const fetch = require("node-fetch");
|
const fetch = require("node-fetch");
|
||||||
const iconv = require("iconv-lite");
|
const iconv = require("iconv-lite");
|
||||||
@ -84,12 +85,13 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
|||||||
};
|
};
|
||||||
|
|
||||||
fetch(url, { headers: headers })
|
fetch(url, { headers: headers })
|
||||||
.catch((error) => {
|
.then(FetcherHelper.checkStatus)
|
||||||
fetchFailedCallback(this, error);
|
.then((response) => {
|
||||||
scheduleTimer();
|
response.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.catch((error) => {
|
||||||
res.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
fetchFailedCallback(this, error.message);
|
||||||
|
scheduleTimer();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ module.exports = NodeHelper.create({
|
|||||||
|
|
||||||
let fetcher;
|
let fetcher;
|
||||||
if (typeof this.fetchers[url] === "undefined") {
|
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 = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings);
|
||||||
|
|
||||||
fetcher.onReceive(() => {
|
fetcher.onReceive(() => {
|
||||||
@ -61,7 +61,7 @@ module.exports = NodeHelper.create({
|
|||||||
|
|
||||||
this.fetchers[url] = fetcher;
|
this.fetchers[url] = fetcher;
|
||||||
} else {
|
} else {
|
||||||
Log.log("Use existing news fetcher for url: " + url);
|
Log.log("Use existing newsfetcher for url: " + url);
|
||||||
fetcher = this.fetchers[url];
|
fetcher = this.fetchers[url];
|
||||||
fetcher.setReloadInterval(reloadInterval);
|
fetcher.setReloadInterval(reloadInterval);
|
||||||
fetcher.broadcastItems();
|
fetcher.broadcastItems();
|
||||||
|
@ -87,6 +87,7 @@
|
|||||||
"socket.io": "^4.1.2"
|
"socket.io": "^4.1.2"
|
||||||
},
|
},
|
||||||
"_moduleAliases": {
|
"_moduleAliases": {
|
||||||
|
"fetcher_helper": "js/fetcher_helper.js",
|
||||||
"node_helper": "js/node_helper.js",
|
"node_helper": "js/node_helper.js",
|
||||||
"logger": "js/logger.js"
|
"logger": "js/logger.js"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user