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-14 17:37:03 +01:00
|
|
|
item: Object.entries(this.customfields).map(([, value]) => {
|
|
|
|
return Array.isArray(value[1]) ? value[1][0] : value[1];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fetchRSS(): any {
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
start(callback: any) {
|
|
|
|
// TODO any = Pas bien !!
|
|
|
|
/**
|
|
|
|
* Un des soucis qu'on a c'est sur le typage de l'objet de retour. le problème étant que la nature de l'obj de
|
|
|
|
* retour ne peut pas etre connue puisque il depend des custom fields qui sont definit par l'utilisateur. L'idée
|
|
|
|
* pourrait etre de creer une classe generique (cf le type CustomFields du ficher index.d.ts du package rss-parser).
|
|
|
|
* Après quelques recherches ca doit etre la manière ""correct"" de faire. Entre autre avec l'utilisation des mots
|
|
|
|
* clés keyof U ou encore Array<keyof U>. Je vais continuer a gratter dans cette direction perso.
|
|
|
|
*/
|
|
|
|
this.loopRunning = true;
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
while (this.loopRunning) {
|
|
|
|
this.fetchRSS().then((obj: any, err: any) => callback(obj, err)); // TODO Erreur a la compile
|
|
|
|
await new Promise((res) => setTimeout(res, 2000));
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief stop the async function
|
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
this.loopRunning = false;
|
|
|
|
}
|
|
|
|
}
|