2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2020-07-12 12:45:32 +02:00
|
|
|
* Node Helper: Newsfeed - NewsfeedFetcher
|
2016-03-30 14:49:37 +02:00
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2016-03-30 14:49:37 +02:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2021-02-18 19:14:53 +01:00
|
|
|
const Log = require("logger");
|
2020-05-31 22:12:26 +02:00
|
|
|
const FeedMe = require("feedme");
|
2021-04-24 09:14:08 +02:00
|
|
|
const NodeHelper = require("node_helper");
|
2022-05-27 19:46:28 +02:00
|
|
|
const fetch = require("fetch");
|
2020-05-31 22:12:26 +02:00
|
|
|
const iconv = require("iconv-lite");
|
2022-05-28 22:28:37 +02:00
|
|
|
const stream = require("stream");
|
2016-03-30 14:49:37 +02:00
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-03-30 14:49:37 +02:00
|
|
|
* Responsible for requesting an update on the set interval and broadcasting the data.
|
|
|
|
*
|
2020-08-03 11:36:29 +02:00
|
|
|
* @param {string} url URL of the news feed.
|
|
|
|
* @param {number} reloadInterval Reload interval in milliseconds.
|
|
|
|
* @param {string} encoding Encoding of the feed.
|
|
|
|
* @param {boolean} logFeedWarnings If true log warnings when there is an error parsing a news article.
|
Release 2.22.0 (#2983)
## [2.22.0] - 2023-01-01
Thanks to: @angeldeejay, @buxxi, @dariom, @dWoolridge,
@KristjanESPERANTO, @MagMar94, @naveensrinivasan, @retroflex, @SkySails
and @Tom.
Special thanks to @khassel, @rejas and @sdetweil for taking over most
(if not all) of the work on this release as project collaborators. This
version would not be there without their effort. Thank you!
### Added
- Added test for remoteFile option in compliments module
- Added hourlyWeather functionality to Weather.gov weather provider
- Removed weatherEndpoint definition from weathergov.js (not used)
- Added css class names "today" and "tomorrow" for default calendar
- Added Collaboration.md
- Added new github action for dependency review (#2862)
- Added a WeatherProvider for Open-Meteo
- Added Yr as a weather provider
- Added config options "ignoreXOriginHeader" and
"ignoreContentSecurityPolicy"
### Removed
- Removed usage of internal fetch function of node until it is more
stable
### Updated
- Cleaned up test directory (#2937) and jest config (#2959)
- Wait for all modules to start before declaring the system ready
(#2487)
- Updated e2e tests (moved `done()` in helper functions) and use es6
syntax in all tests
- Updated da translation
- Rework weather module
- Make sure smhi provider api only gets a maximum of 6 digits
coordinates (#2955)
- Use fetch instead of XMLHttpRequest in weatherprovider (#2935)
- Reworked how weatherproviders handle units (#2849)
- Use unix() method for parsing times, fix suntimes on the way (#2950)
- Refactor conversion functions into utils class (#2958)
- The `cors`-method in `server.js` now supports sending and recieving
HTTP headers
- Replace `…` by `…`
- Cleanup compliments module
- Updated dependencies including electron to v22 (#2903)
### Fixed
- Correctly show apparent temperature in SMHI weather provider
- Ensure updatenotification module isn't shown when local is _ahead_ of
remote
- Handle node_helper errors during startup (#2944)
- Possibility to change FontAwesome class in calendar, so icons like
`fab fa-facebook-square` works.
- Fix cors problems with newsfeed articles (as far as possible), allow
disabling cors per feed with option `useCorsProxy: false` (#2840)
- Tests not waiting for the application to start and stop before
starting the next test
- Fix electron tests failing sometimes in github workflow
- Fixed gap in clock module when displayed on the left side with
displayType=digital
- Fixed playwright issue by upgrading to v1.29.1 (#2969)
Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
Co-authored-by: Karsten Hassel <hassel@gmx.de>
Co-authored-by: Malte Hallström <46646495+SkySails@users.noreply.github.com>
Co-authored-by: Veeck <github@veeck.de>
Co-authored-by: veeck <michael@veeck.de>
Co-authored-by: dWoolridge <dwoolridge@charter.net>
Co-authored-by: Johan <jojjepersson@yahoo.se>
Co-authored-by: Dario Mratovich <dario_mratovich@hotmail.com>
Co-authored-by: Dario Mratovich <dario.mratovich@outlook.com>
Co-authored-by: Magnus <34011212+MagMar94@users.noreply.github.com>
Co-authored-by: Naveen <172697+naveensrinivasan@users.noreply.github.com>
Co-authored-by: buxxi <buxxi@omfilm.net>
Co-authored-by: Thomas Hirschberger <47733292+Tom-Hirschberger@users.noreply.github.com>
Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: Andrés Vanegas Jiménez <142350+angeldeejay@users.noreply.github.com>
2023-01-01 18:09:08 +01:00
|
|
|
* @param {boolean} useCorsProxy If true cors proxy is used for article url's.
|
2020-08-03 11:36:29 +02:00
|
|
|
* @class
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
Release 2.22.0 (#2983)
## [2.22.0] - 2023-01-01
Thanks to: @angeldeejay, @buxxi, @dariom, @dWoolridge,
@KristjanESPERANTO, @MagMar94, @naveensrinivasan, @retroflex, @SkySails
and @Tom.
Special thanks to @khassel, @rejas and @sdetweil for taking over most
(if not all) of the work on this release as project collaborators. This
version would not be there without their effort. Thank you!
### Added
- Added test for remoteFile option in compliments module
- Added hourlyWeather functionality to Weather.gov weather provider
- Removed weatherEndpoint definition from weathergov.js (not used)
- Added css class names "today" and "tomorrow" for default calendar
- Added Collaboration.md
- Added new github action for dependency review (#2862)
- Added a WeatherProvider for Open-Meteo
- Added Yr as a weather provider
- Added config options "ignoreXOriginHeader" and
"ignoreContentSecurityPolicy"
### Removed
- Removed usage of internal fetch function of node until it is more
stable
### Updated
- Cleaned up test directory (#2937) and jest config (#2959)
- Wait for all modules to start before declaring the system ready
(#2487)
- Updated e2e tests (moved `done()` in helper functions) and use es6
syntax in all tests
- Updated da translation
- Rework weather module
- Make sure smhi provider api only gets a maximum of 6 digits
coordinates (#2955)
- Use fetch instead of XMLHttpRequest in weatherprovider (#2935)
- Reworked how weatherproviders handle units (#2849)
- Use unix() method for parsing times, fix suntimes on the way (#2950)
- Refactor conversion functions into utils class (#2958)
- The `cors`-method in `server.js` now supports sending and recieving
HTTP headers
- Replace `…` by `…`
- Cleanup compliments module
- Updated dependencies including electron to v22 (#2903)
### Fixed
- Correctly show apparent temperature in SMHI weather provider
- Ensure updatenotification module isn't shown when local is _ahead_ of
remote
- Handle node_helper errors during startup (#2944)
- Possibility to change FontAwesome class in calendar, so icons like
`fab fa-facebook-square` works.
- Fix cors problems with newsfeed articles (as far as possible), allow
disabling cors per feed with option `useCorsProxy: false` (#2840)
- Tests not waiting for the application to start and stop before
starting the next test
- Fix electron tests failing sometimes in github workflow
- Fixed gap in clock module when displayed on the left side with
displayType=digital
- Fixed playwright issue by upgrading to v1.29.1 (#2969)
Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
Co-authored-by: Karsten Hassel <hassel@gmx.de>
Co-authored-by: Malte Hallström <46646495+SkySails@users.noreply.github.com>
Co-authored-by: Veeck <github@veeck.de>
Co-authored-by: veeck <michael@veeck.de>
Co-authored-by: dWoolridge <dwoolridge@charter.net>
Co-authored-by: Johan <jojjepersson@yahoo.se>
Co-authored-by: Dario Mratovich <dario_mratovich@hotmail.com>
Co-authored-by: Dario Mratovich <dario.mratovich@outlook.com>
Co-authored-by: Magnus <34011212+MagMar94@users.noreply.github.com>
Co-authored-by: Naveen <172697+naveensrinivasan@users.noreply.github.com>
Co-authored-by: buxxi <buxxi@omfilm.net>
Co-authored-by: Thomas Hirschberger <47733292+Tom-Hirschberger@users.noreply.github.com>
Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: Andrés Vanegas Jiménez <142350+angeldeejay@users.noreply.github.com>
2023-01-01 18:09:08 +01:00
|
|
|
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings, useCorsProxy) {
|
2020-07-12 12:45:32 +02:00
|
|
|
let reloadTimer = null;
|
|
|
|
let items = [];
|
|
|
|
|
|
|
|
let fetchFailedCallback = function () {};
|
|
|
|
let itemsReceivedCallback = function () {};
|
|
|
|
|
2016-03-30 14:49:37 +02:00
|
|
|
if (reloadInterval < 1000) {
|
|
|
|
reloadInterval = 1000;
|
|
|
|
}
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-03-30 14:49:37 +02:00
|
|
|
/* private methods */
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-04-12 11:06:59 +02:00
|
|
|
* Request the new items.
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
2021-03-16 21:15:19 +01:00
|
|
|
const fetchNews = () => {
|
2016-03-30 14:49:37 +02:00
|
|
|
clearTimeout(reloadTimer);
|
|
|
|
reloadTimer = null;
|
2016-04-12 11:06:59 +02:00
|
|
|
items = [];
|
|
|
|
|
2020-07-12 12:45:32 +02:00
|
|
|
const parser = new FeedMe();
|
2016-04-12 11:06:59 +02:00
|
|
|
|
2021-03-16 21:15:19 +01:00
|
|
|
parser.on("item", (item) => {
|
2020-07-12 12:45:32 +02:00
|
|
|
const title = item.title;
|
|
|
|
let description = item.description || item.summary || item.content || "";
|
|
|
|
const pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
|
|
|
|
const url = item.url || item.link || "";
|
2016-05-10 13:35:13 +02:00
|
|
|
|
2016-05-12 12:30:45 +02:00
|
|
|
if (title && pubdate) {
|
2020-07-12 12:45:32 +02:00
|
|
|
const regex = /(<([^>]+)>)/gi;
|
2017-11-26 11:52:01 +01:00
|
|
|
description = description.toString().replace(regex, "");
|
2016-05-10 13:35:13 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
items.push({
|
2016-05-10 13:35:13 +02:00
|
|
|
title: title,
|
2016-04-20 14:39:38 +02:00
|
|
|
description: description,
|
2016-05-10 13:35:13 +02:00
|
|
|
pubdate: pubdate,
|
Release 2.22.0 (#2983)
## [2.22.0] - 2023-01-01
Thanks to: @angeldeejay, @buxxi, @dariom, @dWoolridge,
@KristjanESPERANTO, @MagMar94, @naveensrinivasan, @retroflex, @SkySails
and @Tom.
Special thanks to @khassel, @rejas and @sdetweil for taking over most
(if not all) of the work on this release as project collaborators. This
version would not be there without their effort. Thank you!
### Added
- Added test for remoteFile option in compliments module
- Added hourlyWeather functionality to Weather.gov weather provider
- Removed weatherEndpoint definition from weathergov.js (not used)
- Added css class names "today" and "tomorrow" for default calendar
- Added Collaboration.md
- Added new github action for dependency review (#2862)
- Added a WeatherProvider for Open-Meteo
- Added Yr as a weather provider
- Added config options "ignoreXOriginHeader" and
"ignoreContentSecurityPolicy"
### Removed
- Removed usage of internal fetch function of node until it is more
stable
### Updated
- Cleaned up test directory (#2937) and jest config (#2959)
- Wait for all modules to start before declaring the system ready
(#2487)
- Updated e2e tests (moved `done()` in helper functions) and use es6
syntax in all tests
- Updated da translation
- Rework weather module
- Make sure smhi provider api only gets a maximum of 6 digits
coordinates (#2955)
- Use fetch instead of XMLHttpRequest in weatherprovider (#2935)
- Reworked how weatherproviders handle units (#2849)
- Use unix() method for parsing times, fix suntimes on the way (#2950)
- Refactor conversion functions into utils class (#2958)
- The `cors`-method in `server.js` now supports sending and recieving
HTTP headers
- Replace `…` by `…`
- Cleanup compliments module
- Updated dependencies including electron to v22 (#2903)
### Fixed
- Correctly show apparent temperature in SMHI weather provider
- Ensure updatenotification module isn't shown when local is _ahead_ of
remote
- Handle node_helper errors during startup (#2944)
- Possibility to change FontAwesome class in calendar, so icons like
`fab fa-facebook-square` works.
- Fix cors problems with newsfeed articles (as far as possible), allow
disabling cors per feed with option `useCorsProxy: false` (#2840)
- Tests not waiting for the application to start and stop before
starting the next test
- Fix electron tests failing sometimes in github workflow
- Fixed gap in clock module when displayed on the left side with
displayType=digital
- Fixed playwright issue by upgrading to v1.29.1 (#2969)
Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com>
Co-authored-by: Karsten Hassel <hassel@gmx.de>
Co-authored-by: Malte Hallström <46646495+SkySails@users.noreply.github.com>
Co-authored-by: Veeck <github@veeck.de>
Co-authored-by: veeck <michael@veeck.de>
Co-authored-by: dWoolridge <dwoolridge@charter.net>
Co-authored-by: Johan <jojjepersson@yahoo.se>
Co-authored-by: Dario Mratovich <dario_mratovich@hotmail.com>
Co-authored-by: Dario Mratovich <dario.mratovich@outlook.com>
Co-authored-by: Magnus <34011212+MagMar94@users.noreply.github.com>
Co-authored-by: Naveen <172697+naveensrinivasan@users.noreply.github.com>
Co-authored-by: buxxi <buxxi@omfilm.net>
Co-authored-by: Thomas Hirschberger <47733292+Tom-Hirschberger@users.noreply.github.com>
Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: Andrés Vanegas Jiménez <142350+angeldeejay@users.noreply.github.com>
2023-01-01 18:09:08 +01:00
|
|
|
url: url,
|
|
|
|
useCorsProxy: useCorsProxy
|
2016-04-20 14:39:38 +02:00
|
|
|
});
|
2018-06-26 20:01:28 -04:00
|
|
|
} else if (logFeedWarnings) {
|
2020-06-01 16:40:20 +02:00
|
|
|
Log.warn("Can't parse feed item:");
|
|
|
|
Log.warn(item);
|
|
|
|
Log.warn("Title: " + title);
|
|
|
|
Log.warn("Description: " + description);
|
|
|
|
Log.warn("Pubdate: " + pubdate);
|
2016-04-20 14:39:38 +02:00
|
|
|
}
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
2021-03-16 21:15:19 +01:00
|
|
|
parser.on("end", () => {
|
|
|
|
this.broadcastItems();
|
2016-03-30 14:49:37 +02:00
|
|
|
scheduleTimer();
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
2021-03-16 21:15:19 +01:00
|
|
|
parser.on("error", (error) => {
|
|
|
|
fetchFailedCallback(this, error);
|
2016-03-30 14:49:37 +02:00
|
|
|
scheduleTimer();
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
2022-07-21 22:07:36 +02:00
|
|
|
parser.on("ttl", (minutes) => {
|
|
|
|
try {
|
|
|
|
// 86400000 = 24 hours is mentioned in the docs as maximum value:
|
|
|
|
const ttlms = Math.min(minutes * 60 * 1000, 86400000);
|
|
|
|
if (ttlms > reloadInterval) {
|
|
|
|
reloadInterval = ttlms;
|
|
|
|
Log.info("Newsfeed-Fetcher: reloadInterval set to ttl=" + reloadInterval + " for url " + url);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
Log.warn("Newsfeed-Fetcher: feed ttl is no valid integer=" + minutes + " for url " + url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-12 12:45:32 +02:00
|
|
|
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
|
2021-03-02 00:17:13 +01:00
|
|
|
const headers = {
|
2021-12-13 21:24:37 +01:00
|
|
|
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version,
|
2021-03-02 00:17:13 +01:00
|
|
|
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
|
|
|
|
Pragma: "no-cache"
|
2020-07-12 12:45:32 +02:00
|
|
|
};
|
|
|
|
|
2021-03-02 00:17:13 +01:00
|
|
|
fetch(url, { headers: headers })
|
2021-04-24 09:14:08 +02:00
|
|
|
.then(NodeHelper.checkFetchStatus)
|
2021-03-28 14:17:03 +02:00
|
|
|
.then((response) => {
|
2022-05-28 22:28:37 +02:00
|
|
|
let nodeStream;
|
|
|
|
if (response.body instanceof stream.Readable) {
|
|
|
|
nodeStream = response.body;
|
|
|
|
} else {
|
|
|
|
nodeStream = stream.Readable.fromWeb(response.body);
|
|
|
|
}
|
|
|
|
nodeStream.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
2021-03-28 14:17:03 +02:00
|
|
|
})
|
2021-03-02 21:26:25 +01:00
|
|
|
.catch((error) => {
|
2021-05-02 10:24:22 +02:00
|
|
|
fetchFailedCallback(this, error);
|
2017-01-02 21:31:21 +00:00
|
|
|
scheduleTimer();
|
2021-03-02 00:17:13 +01:00
|
|
|
});
|
2016-03-30 14:49:37 +02:00
|
|
|
};
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-03-30 14:49:37 +02:00
|
|
|
* Schedule the timer for the next update.
|
|
|
|
*/
|
2020-07-12 12:45:32 +02:00
|
|
|
const scheduleTimer = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
clearTimeout(reloadTimer);
|
2020-05-11 22:22:32 +02:00
|
|
|
reloadTimer = setTimeout(function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
fetchNews();
|
|
|
|
}, reloadInterval);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* public methods */
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-03-30 14:49:37 +02:00
|
|
|
* Update the reload interval, but only if we need to increase the speed.
|
|
|
|
*
|
2020-08-03 11:36:29 +02:00
|
|
|
* @param {number} interval Interval for the update in milliseconds.
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
this.setReloadInterval = function (interval) {
|
2016-03-30 14:49:37 +02:00
|
|
|
if (interval > 1000 && interval < reloadInterval) {
|
|
|
|
reloadInterval = interval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-03-30 14:49:37 +02:00
|
|
|
* Initiate fetchNews();
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
this.startFetch = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
fetchNews();
|
|
|
|
};
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-06-04 20:32:55 -06:00
|
|
|
* Broadcast the existing items.
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
this.broadcastItems = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
if (items.length <= 0) {
|
2020-06-01 16:40:20 +02:00
|
|
|
Log.info("Newsfeed-Fetcher: No items to broadcast yet.");
|
2016-03-30 14:49:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-01 16:40:20 +02:00
|
|
|
Log.info("Newsfeed-Fetcher: Broadcasting " + items.length + " items.");
|
2021-03-16 21:15:19 +01:00
|
|
|
itemsReceivedCallback(this);
|
2016-03-30 14:49:37 +02:00
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.onReceive = function (callback) {
|
2016-03-30 14:49:37 +02:00
|
|
|
itemsReceivedCallback = callback;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.onError = function (callback) {
|
2016-03-30 14:49:37 +02:00
|
|
|
fetchFailedCallback = callback;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.url = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.items = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
return items;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-12 08:10:43 +02:00
|
|
|
module.exports = NewsfeedFetcher;
|