2021-03-06 16:17:34 +01:00
|
|
|
import Parser from "rss-parser";
|
2021-02-14 15:00:33 +01:00
|
|
|
import { ListenerRSSInfos as ListenerInfo } from "./Models/ListenerRSSInfos";
|
2021-02-28 16:37:27 +01:00
|
|
|
import EventEmitter from "events";
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-02-14 15:00:33 +01:00
|
|
|
const DEFAULT_TIMELOOP: number = 5 * 60; // default timeloop is 5 min
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-02-28 16:37:27 +01:00
|
|
|
/**
|
|
|
|
* Emit 'update' when he's making a fetch during the start fun
|
|
|
|
* Emit 'update_err' when the fetch has an issue
|
|
|
|
*/
|
|
|
|
export class ListenerRss extends EventEmitter {
|
2021-02-14 15:00:33 +01:00
|
|
|
name: string = "";
|
|
|
|
address: string = "";
|
|
|
|
timeloop: number = DEFAULT_TIMELOOP; // time in seconds
|
2021-02-14 17:37:03 +01:00
|
|
|
customfields?: { [key: string]: string[] | string };
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// private fields
|
2021-03-06 16:17:34 +01:00
|
|
|
parser: Parser;
|
2021-02-13 13:40:18 +01:00
|
|
|
loopRunning: boolean = false;
|
2021-03-08 09:26:38 +01:00
|
|
|
lastEntriesLinks: string[] = [];
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
|
|
|
* @brief constructor
|
|
|
|
* @param config ListenerRSSInfos interface who's contain the ListenerInfos
|
|
|
|
*/
|
2021-02-14 17:07:23 +01:00
|
|
|
constructor(config: ListenerInfo) {
|
2021-02-28 16:37:27 +01:00
|
|
|
super();
|
|
|
|
|
2021-03-06 16:17:34 +01:00
|
|
|
this.name = config.name;
|
|
|
|
this.address = config.address;
|
|
|
|
this.timeloop =
|
|
|
|
config.timeloop === undefined ? DEFAULT_TIMELOOP : config.timeloop;
|
|
|
|
this.customfields = config.customfields;
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-03-06 16:17:34 +01:00
|
|
|
this.parser = this.generateParser();
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
2021-03-06 16:17:34 +01:00
|
|
|
* @brief Private function. Is useed to initilize the parser object with the customfields var
|
2021-02-25 14:02:02 +01:00
|
|
|
*/
|
2021-03-06 16:17:34 +01:00
|
|
|
generateParser() {
|
|
|
|
const parserConfig = this.customfields && {
|
|
|
|
customFields: {
|
|
|
|
feed: [],
|
|
|
|
item: Object.entries(this.customfields).map(([, value]) => {
|
|
|
|
return Array.isArray(value) ? value[0] : value;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return new Parser(parserConfig);
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
|
|
|
* @brief use the parseURL function from rss-parser with the objects datas
|
|
|
|
* @return return a promise with the received data
|
|
|
|
*/
|
|
|
|
fetchRSS(): Promise<Parser.Output<any>> {
|
2021-03-06 17:32:59 +01:00
|
|
|
return this.parser.parseURL(this.address);
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief call the callback function each looptime
|
|
|
|
* @param callback function who's going to be called with the latest get
|
|
|
|
*/
|
2021-02-28 16:37:27 +01:00
|
|
|
start(): void {
|
2021-02-13 13:40:18 +01:00
|
|
|
this.loopRunning = true;
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
const fun: () => void = () => {
|
2021-02-21 00:12:57 +01:00
|
|
|
this.fetchRSS()
|
2021-03-08 09:26:38 +01:00
|
|
|
.then((obj: { [key: string]: any }) => {
|
|
|
|
this.emit("update", obj);
|
|
|
|
const updatedEntriesLinks = obj.items.map(
|
|
|
|
(item: { link: string }) => item.link
|
|
|
|
);
|
|
|
|
|
|
|
|
const newEntries = obj.items.filter(
|
|
|
|
(item: { link: string }) =>
|
|
|
|
!this.lastEntriesLinks.includes(item.link)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.lastEntriesLinks.length !== 0 && newEntries.length !== 0) {
|
|
|
|
this.emit("newEntries", newEntries);
|
|
|
|
}
|
|
|
|
this.lastEntriesLinks = updatedEntriesLinks;
|
|
|
|
})
|
2021-03-06 17:32:59 +01:00
|
|
|
.catch((err) => this.emit("error", err));
|
2021-02-21 00:12:57 +01:00
|
|
|
};
|
2021-02-25 14:02:02 +01:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
while (this.loopRunning) {
|
|
|
|
await fun();
|
|
|
|
await new Promise((res) => setTimeout(res, this.timeloop * 1000));
|
|
|
|
}
|
|
|
|
})();
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief stop the async function
|
|
|
|
*/
|
2021-02-25 14:02:02 +01:00
|
|
|
stop(): void {
|
2021-02-13 13:40:18 +01:00
|
|
|
this.loopRunning = false;
|
|
|
|
}
|
|
|
|
}
|