listener-rss/src/listener-rss.ts

96 lines
3.1 KiB
TypeScript
Raw Normal View History

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-14 15:00:33 +01:00
const DEFAULT_TIMELOOP: number = 5 * 60; // default timeloop is 5 min
2021-02-14 15:00:33 +01:00
export class ListenerRss {
name: string = "";
address: string = "";
timeloop: number = DEFAULT_TIMELOOP; // time in seconds
customfields: [string, string | [string, string]][] | undefined = undefined;
// private fields
parser: Parser | undefined = undefined;
loopRunning: boolean = false;
constructor(
name: string | ListenerInfo,
address?: string,
timeloop?: number,
2021-02-14 15:00:33 +01:00
customfields?: [string, string | [string, string]][]
) {
2021-02-14 15:00:33 +01:00
if (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 =
2021-02-14 15:00:33 +01:00
infos._customfields === undefined ? 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;
}
}