98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
|
import Parser from "rss-parser";
|
||
|
import { ListenerRSSInfo as ListenerInfo } from "./Models/ListenerRSSInfos";
|
||
|
|
||
|
const DEFAULT_TIMELOOP = 5 * 60; // default timeloop is 5 min
|
||
|
|
||
|
class ListenerRss {
|
||
|
name = undefined;
|
||
|
address = undefined;
|
||
|
timeloop = DEFAULT_TIMELOOP; // time in seconds
|
||
|
customfields = [];
|
||
|
|
||
|
// private fields
|
||
|
parser: Parser | undefined = undefined;
|
||
|
loopRunning: boolean = false;
|
||
|
|
||
|
constructor(
|
||
|
name: string | ListenerInfo,
|
||
|
address?: string,
|
||
|
timeloop?: number,
|
||
|
customfields?: string[][]
|
||
|
) {
|
||
|
if (name !== undefined && name instanceof ListenerInfo) {
|
||
|
// constructor with 1 arg
|
||
|
this.setData(name);
|
||
|
} else if (address !== undefined) {
|
||
|
// construct with between 2 and 4 args
|
||
|
this.setData(new ListenerInfo(name, address, timeloop, customfields));
|
||
|
} else throw new Error("the constructor must have args");
|
||
|
this.setParser();
|
||
|
}
|
||
|
|
||
|
setParser() {
|
||
|
// set parser
|
||
|
this.parser = new Parser(
|
||
|
this.customfields !== undefined
|
||
|
? {
|
||
|
customFields: {
|
||
|
item: this.customfields.map((elt) => {
|
||
|
return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
|
||
|
}),
|
||
|
},
|
||
|
}
|
||
|
: {}
|
||
|
); // if customfield is set -> let's set the parser with, else let the option empty
|
||
|
}
|
||
|
|
||
|
setData(infos: ListenerInfo) {
|
||
|
// Set data
|
||
|
this.name = infos._name;
|
||
|
this.address = infos._address;
|
||
|
this.timeloop =
|
||
|
infos._timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop;
|
||
|
this.customfields =
|
||
|
infos._customfields === undefined ? [] : infos._customfields;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = ListenerRss;
|