2021-02-14 16:30:23 +01:00
|
|
|
import Parser from "rss-parser/index";
|
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
|
|
|
|
parser: Parser | undefined = undefined;
|
|
|
|
loopRunning: boolean = false;
|
|
|
|
|
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-02-14 17:07:23 +01:00
|
|
|
this.setData(config);
|
2021-02-13 13:40:18 +01:00
|
|
|
this.setParser();
|
|
|
|
}
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
|
|
|
* @brief Private function. Is useed to initilize the parser object with the customfields var
|
|
|
|
*/
|
2021-02-13 13:40:18 +01:00
|
|
|
setParser() {
|
|
|
|
// set parser
|
|
|
|
this.parser = new Parser(
|
|
|
|
this.customfields !== undefined
|
|
|
|
? {
|
|
|
|
customFields: {
|
2021-02-21 00:12:57 +01:00
|
|
|
feed: [],
|
2021-02-14 17:37:03 +01:00
|
|
|
item: Object.entries(this.customfields).map(([, value]) => {
|
2021-02-21 00:12:57 +01:00
|
|
|
return Array.isArray(value) ? value[0] : value;
|
2021-02-13 13:40:18 +01:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {}
|
|
|
|
); // if customfield is set -> let's set the parser with, else let the option empty
|
|
|
|
}
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
|
|
|
* @brief Private function. Initialized the listener with an ListenerRSSInfos interface
|
|
|
|
* @param infos ListenerRSSInfos interface who's contain the ListenerInfos
|
|
|
|
*/
|
2021-02-13 13:40:18 +01:00
|
|
|
setData(infos: ListenerInfo) {
|
|
|
|
// Set data
|
2021-02-14 17:07:23 +01:00
|
|
|
this.name = infos.name;
|
|
|
|
this.address = infos.address;
|
2021-02-13 13:40:18 +01:00
|
|
|
this.timeloop =
|
2021-02-14 17:07:23 +01:00
|
|
|
infos.timeloop === undefined ? DEFAULT_TIMELOOP : infos.timeloop;
|
2021-02-14 17:37:03 +01:00
|
|
|
this.customfields = infos.customfields;
|
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-02-13 13:40:18 +01:00
|
|
|
if (this.parser !== undefined && this.address !== undefined) {
|
|
|
|
return this.parser.parseURL(this.address).catch((err) => {
|
|
|
|
throw new Error("bad address or no access : " + err);
|
|
|
|
});
|
|
|
|
} else throw new Error("listener must be first initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-02-28 16:37:27 +01:00
|
|
|
.then((obj: { [key: string]: any }) => this.emit("update", obj))
|
|
|
|
.catch((err) => this.emit("update_err", 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;
|
|
|
|
}
|
|
|
|
}
|