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-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-14 15:00:33 +01:00
|
|
|
export class ListenerRss {
|
|
|
|
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-14 17:07:23 +01:00
|
|
|
constructor(config: ListenerInfo) {
|
|
|
|
this.setData(config);
|
2021-02-13 13:40:18 +01:00
|
|
|
this.setParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 00:12:57 +01:00
|
|
|
fetchRSS(): Promise<any> {
|
2021-02-13 13:40:18 +01:00
|
|
|
// TODO Pas Bien
|
|
|
|
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-21 00:12:57 +01:00
|
|
|
start(
|
|
|
|
callback: (
|
|
|
|
obj: { [key: string]: any } | undefined,
|
|
|
|
err: Error | undefined
|
|
|
|
) => void
|
|
|
|
) {
|
2021-02-13 13:40:18 +01:00
|
|
|
this.loopRunning = true;
|
|
|
|
|
2021-02-21 00:12:57 +01:00
|
|
|
const fun = () => {
|
|
|
|
this.fetchRSS()
|
|
|
|
.then((obj: { [key: string]: any }) => callback(obj, undefined))
|
|
|
|
.catch((err) => callback(undefined, err));
|
|
|
|
};
|
|
|
|
let fun_loop = () => {
|
|
|
|
fun();
|
|
|
|
console.log("while");
|
|
|
|
if (this.loopRunning) setTimeout(fun_loop, this.timeloop * 1000);
|
|
|
|
};
|
|
|
|
fun_loop();
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief stop the async function
|
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
this.loopRunning = false;
|
|
|
|
}
|
|
|
|
}
|