2021-03-06 16:17:34 +01:00
|
|
|
import Parser from "rss-parser";
|
2021-03-28 13:22:55 +02:00
|
|
|
import {
|
|
|
|
ListenerRSSInfos as ListenerInfo,
|
|
|
|
ListenerRSSInfos,
|
|
|
|
} 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
|
2021-03-12 13:53:08 +01:00
|
|
|
* Emit 'error' when the fetch has an issue
|
|
|
|
* Emit 'newEntries' when the fetch has new entris
|
2021-02-28 16:37:27 +01:00
|
|
|
*/
|
|
|
|
export class ListenerRss extends EventEmitter {
|
2021-02-14 15:00:33 +01:00
|
|
|
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
|
2021-03-06 16:17:34 +01:00
|
|
|
parser: Parser;
|
2021-02-13 13:40:18 +01:00
|
|
|
loopRunning: boolean = false;
|
2021-03-08 09:26:38 +01:00
|
|
|
lastEntriesLinks: string[] = [];
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
|
|
|
* @brief constructor
|
2021-03-12 13:53:08 +01:00
|
|
|
* @param config ListenerRSSInfos interface who contains the ListenerInfos
|
2021-02-25 14:02:02 +01:00
|
|
|
*/
|
2021-02-14 17:07:23 +01:00
|
|
|
constructor(config: ListenerInfo) {
|
2021-02-28 16:37:27 +01:00
|
|
|
super();
|
|
|
|
|
2021-03-06 16:17:34 +01:00
|
|
|
this.address = config.address;
|
|
|
|
this.timeloop =
|
|
|
|
config.timeloop === undefined ? DEFAULT_TIMELOOP : config.timeloop;
|
|
|
|
this.customfields = config.customfields;
|
2021-03-21 21:35:22 +01:00
|
|
|
this.lastEntriesLinks =
|
|
|
|
config.lastEntriesLinks === undefined ? [] : config.lastEntriesLinks;
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-03-06 16:17:34 +01:00
|
|
|
this.parser = this.generateParser();
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
2021-03-12 13:53:08 +01:00
|
|
|
* @brief Private function. Is used to initilize the parser object with the customfields var
|
2021-02-25 14:02:02 +01:00
|
|
|
*/
|
2021-03-06 16:17:34 +01:00
|
|
|
generateParser() {
|
|
|
|
const parserConfig = this.customfields && {
|
|
|
|
customFields: {
|
|
|
|
feed: [],
|
|
|
|
item: Object.entries(this.customfields).map(([, value]) => {
|
|
|
|
return Array.isArray(value) ? value[0] : value;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return new Parser(parserConfig);
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 14:02:02 +01:00
|
|
|
/**
|
2021-03-12 13:53:08 +01:00
|
|
|
* @brief use the parseURL function from rss-parser with the objects data
|
2021-02-25 14:02:02 +01:00
|
|
|
* @return return a promise with the received data
|
|
|
|
*/
|
|
|
|
fetchRSS(): Promise<Parser.Output<any>> {
|
2021-03-06 17:32:59 +01:00
|
|
|
return this.parser.parseURL(this.address);
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief call the callback function each looptime
|
|
|
|
*/
|
2021-02-28 16:37:27 +01:00
|
|
|
start(): void {
|
2021-02-13 13:40:18 +01:00
|
|
|
this.loopRunning = true;
|
2021-04-18 16:16:15 +02:00
|
|
|
|
2021-03-23 18:10:57 +01:00
|
|
|
const fun: () => void = async () => {
|
2021-04-18 16:16:15 +02:00
|
|
|
await Promise.resolve(
|
2021-04-10 17:43:59 +02:00
|
|
|
await this.fetchRSS()
|
2021-03-23 18:10:57 +01:00
|
|
|
.then((obj: { [key: string]: any }) => {
|
|
|
|
this.emit("update", obj);
|
|
|
|
const updatedEntriesLinks = obj.items.map(
|
|
|
|
(item: { link: string }) => item.link
|
|
|
|
);
|
2021-03-08 09:26:38 +01:00
|
|
|
|
2021-03-23 18:10:57 +01:00
|
|
|
const newEntries = obj.items.filter(
|
|
|
|
(item: { link: string }) =>
|
|
|
|
!this.lastEntriesLinks.includes(item.link)
|
|
|
|
);
|
2021-03-08 09:26:38 +01:00
|
|
|
|
2021-03-23 18:10:57 +01:00
|
|
|
if (newEntries.length !== 0) {
|
|
|
|
this.emit("newEntries", newEntries);
|
|
|
|
}
|
|
|
|
this.lastEntriesLinks = updatedEntriesLinks;
|
|
|
|
})
|
|
|
|
.catch((err) => this.emit("error", 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;
|
|
|
|
}
|
2021-03-28 13:22:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse the datas inti a ListenerRSSInfos object
|
|
|
|
* @return return a ListenerRSSInfos object
|
|
|
|
*/
|
|
|
|
getProperty(): ListenerRSSInfos {
|
|
|
|
return {
|
|
|
|
address: this.address,
|
|
|
|
customfields: this.customfields,
|
|
|
|
lastEntriesLinks: this.lastEntriesLinks,
|
|
|
|
timeloop: this.timeloop,
|
|
|
|
};
|
|
|
|
}
|
2021-02-13 13:40:18 +01:00
|
|
|
}
|