Compare commits

...

6 Commits

Author SHA1 Message Date
8e800f843c Add readme 2021-02-07 16:06:26 +01:00
4b653194f6 Add class who contain listener info 2021-02-07 13:03:40 +01:00
de195f8592 Version without design pattern builder + some new test 2021-02-07 13:01:48 +01:00
fa6f1abadb Ajout de test
Fix execution test
Quelques renames
2021-01-30 14:02:59 +01:00
f1b1e23792 Update packages version
Add first test
Add design pattern builder + Youtube builder
2021-01-19 13:12:44 +01:00
Florent
bf55adf8da Starting unit testing (hard work) 2020-12-20 18:57:33 +01:00
12 changed files with 1811 additions and 415 deletions

125
README.md Normal file
View File

@ -0,0 +1,125 @@
# easy-rss-parser
A lightweight library to give some additions for the [rss-parser package](https://github.com/rbren/rss-parser).
# USAGE
## Punctual usage
You can parse RSS from a URL with some custom data.
An example :
```js
const easyParser = require('easy-rss-parser');
const ListenerRss = easyParser.ListenerRss;
let listener = new ListenerRss('my-test-service', 'fake.rss.service');
// make a request to the adr 'fake.rss.service'
myListener.fetchRSS().then((obj, err) => {
// some act
});
```
## Recurrent usage
You can parse RSS from a URL each n times.
An example :
```js
const easyParser = require('easy-rss-parser');
const ListenerRss = easyParser.ListenerRss;
let listener = new ListenerRss('my-test-service', 'fake.rss.service', 5*60);
let callback_fun = (obj, err) => {
// some act
};
// call callback_fun each 5 minutes
listener.start(callback_fun);
/*...*/
listener.stop();
```
# Documentation
## ListenerRSSInfo
A class to structure listener's data.
### Constructor
`constructor(name, address, timeloop, customfields)`
- name : the service name
- address : the service address
- [optional] timeloop : time to wait between 2 request in seconds (default 5 minutes)
- [optional] customfields : to notice field who's custom to the service (default blank)
[cf annexe CustomFields](#customfields)
## ListenerRSS
### Constructor
`constructor(listenerRSSInfo)`
- listenerRSSInfo : object from the ListenerRSSInfo's class.
`constructor(name, address, timeloop, customfields)`
- name : the service name
- address : the service address
- [optional] timeloop : time to wait between 2 request in seconds (default 5 minutes)
- [optional] customfields : to notice field who's custom to the service (default blank)
### fetchRSS()
This function allows to make a request to the rss service.
#### Return
Return a promise object who's resolved like `resolve: (value: result_fetch) => void))` where `result_fetch` is a json
object who's contain the data. [cf Annexe Output](#output)
#### Issues
Return an error if the server can't be resolved.
### start(callbackFun)
This function will execute the callbackFun each time loop.
#### Parameter
The `callbackFun` is the function who's going to be called each time loop. She need to be under the shape :
```js
(obj, err) => {
/*...*/
}
```
### stop()
This function will stop the execution of the callbackFun each time loop.
# Annexe
## CustomFields
This parameter permit to specify some custom fields who's present in the service but not in the RFC.
For example the YouTube RSS api give some data into the `<media:group>` field. So you can give this info with this :
```js
[
['media:group', 'media:group']
]
```
You can also rename the field with the left part :
```js
[
['my_custom_media_group_name', 'media:group']
]
```
In adition you can rename child element into custom field like this :
```js
[
['media:group', 'media:group'],
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]
```
In this case it's useless to specify the parent field, so you can just omit the first line :
```js
[
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]
```
## Output
Here an example of what type of json object is output during a fetch :
```json
feedUrl: 'fake.rrs.service'
title: 'myFakeApiTitle'
description: 'My Fake api desc'
link: 'fake.rrs.service'
items:
- title: 'My last item'
link: 'fake.rrs.service/item1'
pubDate: 'Thu, 12 Nov 2015 21:16:39 +0000'
creator: 'someone'
content: '<a href="http://example.com">this is a link</a> &amp; <b>this is bold text</b>'
contentSnippet: 'this is a link & this is bold text'
guid: 'fake.rrs.service/item1'
categories:
- test
- npm
- fakeInfos
isoDate: '2015-11-12T21:16:39.000Z'
```

View File

@ -170,3 +170,8 @@ if (process.argv[2] && process.argv[2] === '--service') {
})
}
module.exports = {
ListenerRssInfos: require('./src/Models/ListenerRSSInfos'),
ListenerRss: require('./src/ListenerRss')
}

1574
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,8 @@
"scripts": {
"start": "node index.js",
"service": "node index.js --service",
"stop": "node index.js stop"
"stop": "node index.js stop",
"test": "mocha tests/*-spec.js"
},
"author": "Ju [kataku] / Skeptikon",
"dependencies": {
@ -15,8 +16,15 @@
"discord.js": "^12.0.1",
"fs-extra": "^9.0.0",
"jsonfile": "^6.0.1",
"lodash": "^4.17.15",
"pm2": "^4.2.3",
"lodash": "^4.17.20",
"pm2": "^4.5.1",
"rss-parser": "^3.7.5"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^8.2.1",
"proxyquire": "^2.1.3",
"sinon": "^9.2.2",
"sinon-chai": "^3.5.0"
}
}

View File

@ -88,4 +88,7 @@ function rssLoop(time) {
// INIT
console.log(` --- [RSS-Youtube] Load`)
routage.log(`Load`)
rssLoop(db.config.timeLoop*60*1000)
// rssLoop(db.config.timeLoop*60*1000)
module.exports = {
rssLoop
};

72
src/ListenerRss.js Normal file
View File

@ -0,0 +1,72 @@
const Parser = require('rss-parser');
const ListenerInfo = require('./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 = null;
loopRunning = false;
constructor(name, address, timeloop, customfields) {
if(name !== undefined && name instanceof ListenerInfo) { // constructor with 1 arg
this.setData(name);
} else if (address !== undefined && typeof(address) === 'string') { // 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) {
// 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() {
return this.parser.parseURL(this.address)
.catch((err) => { throw new Error('bad address or no access : ' + err);});
}
/**
* @brief call the callback function each looptime
* @param callback function who's going to be called with the latest get
*/
start(callback) {
this.loopRunning = true;
(async () => {
while(this.loopRunning === true) {
this.fetchRSS().then((obj, err) => callback(obj, err))
await new Promise(res => setTimeout(res, 2000));
}
})();
}
/**
* @brief stop the async function
*/
stop() {
this.loopRunning = false;
}
}
module.exports = ListenerRss

View File

@ -0,0 +1,42 @@
class ListenerRSSInfos {
_name = undefined; // name of the listener
_address = undefined; // feed's address
_timeloop = 5 * 60; // update time RSS feed
_customfields = [] // rss fields custom
constructor(name, address, timeloop, customfields) {
if(name !== undefined && address !== undefined) {
this._name = name;
this._address = address;
this._timeloop = timeloop;
this._customfields = customfields;
} else throw new Error('Bad constructor\'s args');
}
set timeloop(value) {
this._timeloop = value;
}
set customfields(value) {
this._customfields = value;
}
get name() {
return this._name;
}
get address() {
return this._address;
}
get timeloop() {
return this._timeloop;
}
get customfields() {
return this._customfields;
}
}
module.exports = ListenerRSSInfos;

View File

@ -0,0 +1,20 @@
class ListenerBuildDirector {
_builder = undefined;
constructor(builder) {
this._builder = builder;
}
getListener() {
return this._builder.listenerRSS;
}
build(infos) {
if(infos === undefined)
throw new Error('infos must be initialized');
this._builder.constructListener(infos);
}
}
module.exports = ListenerBuildDirector

View File

@ -0,0 +1,45 @@
const ListenerRSS = require('../../ListenerRss')
class AbstractListenerRSSBuilder {
_listenerRSS = undefined;
constructor() {
if(this.constructor === AbstractListenerRSSBuilder)
throw new Error('The Abstract class "AbstractListnerRSSBuilder" cannot be instantiated');
}
/**
* @brief first function to call after constructor. It's building the listener
* @param infos Must to be an object ListenerRSSInfos
*/
constructListener(infos) {
this._listenerRSS = new ListenerRSS();
this.setInfos(infos);
this.setSpecificInfos();
this.listenerRSS.setParser()
}
/**
* @brief give the listener just build
* @return ListenerRSS with all the setups datas
* @exception if the listener isn't construct
*/
get listenerRSS() {
if(this._listenerRSS === undefined)
throw new Error('the listener is not yet build');
return this._listenerRSS;
}
setInfos(infos) { // Nominal Infos (like name, addresse, and other)
this._listenerRSS.setData(infos);
}
setSpecificInfos() { // More generic infos who's depend of platforms
throw new Error('This function is not implemented');
}
}
module.exports = AbstractListenerRSSBuilder

View File

@ -0,0 +1,16 @@
const AbstractListenerRSSBuilder = require('./AbstractListenerRSSBuilder');
class YoutubeListenerRSSBuilder extends AbstractListenerRSSBuilder {
constructor() {
super();
}
setSpecificInfos() {
this.listenerRSS.customfields = [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]
}
}
module.exports = YoutubeListenerRSSBuilder

View File

@ -0,0 +1,42 @@
class ListenerRSSInfos {
_name = undefined; // name of the listener
_address = undefined; // feed's address
_timeloop = 1 * 60; // update time RSS feed
_customfields = [] // rss fields custom
set name(value) {
this._name = value;
}
set address(value) {
this._address = value;
}
set timeloop(value) {
this._timeloop = value;
}
set customfields(value) {
this._customfields = value;
}
get name() {
return this._name;
}
get address() {
return this._address;
}
get timeloop() {
return this._timeloop;
}
get customfields() {
return this._customfields;
}
}
module.exports = ListenerRSSInfos;

270
tests/rss-youtube-spec.js Normal file
View File

@ -0,0 +1,270 @@
// external lib
const Parser = require("rss-parser");
// tested class
/*const ListenerRssPackage = require("../index");
const Listeners = ListenerRssPackage.ListenerRss
const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos*/
const Listeners = require('../src/ListenerRss')
const ListenerRRSInfo = require('../src/Models/ListenerRSSInfos')
// Unit test
const chai = require("chai");
const sinon = require("sinon");
const sinon_chai = require("sinon-chai");
chai.use(sinon_chai);
const expect = chai.expect;
describe("test class RSS: jsonfile", function () {
let myListener = undefined;
const infosListener = new ListenerRRSInfo('my-test-service', 'fake.rss.service', 15, [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
// parseURL tests
let stubParser;
const mockedRSSOutput = {
items: [
{
'title': 'my title 00',
'media:group': {
'media:description': 'my description 00',
'media:thumbnail': [{$: {height: 360, width: 420, url: 'my_image00.jpg'}}],
},
'link': 'my_url_00.com',
'pubDate': 'myDate00'
},
{
'title': 'my title 01',
'media:group' : {
'media:description': 'my description 01',
'media:thumbnail': [{$: { height: 360, width: 420, url: 'my_image01.jpg'}}],
},
'link': 'my_url_01.com',
'pubDate': 'myDate01'
},
{
'title': 'my title 02',
'media:group' : {
'media:description': 'my description 02',
'media:thumbnail': [{$: { height: 360, width: 420, url: 'my_image02.jpg'}}],
},
'link': 'my_url_02.com',
'pubDate': 'myDate02'
},
]
};
beforeEach(function () {
// stubs
stubParser = sinon.stub(Parser.prototype, 'parseURL');
stubParser.withArgs(infosListener.address)
.resolves(mockedRSSOutput);
stubParser.withArgs('bad.rss.service')
.resolves(new Error('connect ECONNREFUSED 127.0.0.1:80'));
// constructor
myListener = undefined;
})
afterEach(function () {
// restore stubs
Parser.prototype.parseURL.restore();
});
describe("Building Ytb listener", function () {
it("The build without issues (infosListener parameters)", function () {
myListener = new Listeners(infosListener);
// assertions
// myListener data
expect(myListener.timeloop).to.eql(15);
expect(myListener.name).to.eql('my-test-service');
expect(myListener.address).to.eql('fake.rss.service');
expect(myListener.customfields).to.eql([
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
expect(myListener.parser.options.customFields).to.eql({
feed: [],
item: [
'media:group',
'media:group'
]
});
});
it("The build without issues (raw infos : 4 params)", function () {
myListener = new Listeners('my-test-service', 'fake.rss.service', 15, [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
// assertions
// myListener data
expect(myListener.timeloop).to.eql(15);
expect(myListener.name).to.eql('my-test-service');
expect(myListener.address).to.eql('fake.rss.service');
expect(myListener.customfields).to.eql([
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
expect(myListener.parser.options.customFields).to.eql({
feed: [],
item: [
'media:group',
'media:group'
]
});
});
it("The build without issues (raw infos : just 2 params)", function () {
myListener = new Listeners('my-test-service', 'fake.rss.service');
// assertions
// myListener data
expect(myListener.timeloop).to.eql(5*60);
expect(myListener.name).to.eql('my-test-service');
expect(myListener.address).to.eql('fake.rss.service');
expect(myListener.customfields).to.eql([]);
expect(myListener.parser.options.customFields).to.eql({
feed: [],
item: []
});
});
it("The build without issues (raw infos : just 3 params (no custom fields))", function () {
myListener = new Listeners('my-test-service', 'fake.rss.service', 15);
// assertions
// myListener data
expect(myListener.timeloop).to.eql(15);
expect(myListener.name).to.eql('my-test-service');
expect(myListener.address).to.eql('fake.rss.service');
expect(myListener.customfields).to.eql([]);
expect(myListener.parser.options.customFields).to.eql({
feed: [],
item: []
});
});
it("The build without issues (raw infos : just 3 params (no timeloop))", function () {
myListener = new Listeners('my-test-service', 'fake.rss.service', undefined, [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
// assertions
// myListener data
expect(myListener.timeloop).to.eql(5*60);
expect(myListener.name).to.eql('my-test-service');
expect(myListener.address).to.eql('fake.rss.service');
expect(myListener.customfields).to.eql([
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
expect(myListener.parser.options.customFields).to.eql({
feed: [],
item: [
'media:group',
'media:group'
]
});
});
});
describe("fetch some data", function () {
it("fetch without issues", function () {
// classic build
myListener = new Listeners(infosListener);
// fetch
let res = myListener.fetchRSS();
//assertion
// calls
expect(stubParser).to.have.been.calledOnce;
expect(stubParser).to.have.been.calledWith(infosListener._address);
// Promise
//await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput);
res.then((obj, err) => {
expect(obj).to.be.eql(mockedRSSOutput);
expect(err).to.be.undefined
})
})
it("fetch with bad address", function () {
// classic build
myListener = new Listeners('my-test-service', 'bad.rss.service', undefined, [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
// fetch
let res = myListener.fetchRSS();
//assertion
// calls
expect(stubParser).to.have.been.calledOnce;
expect(stubParser).to.have.been.calledWith('bad.rss.service');
// Promise
res.then((obj, err) => {
expect(obj).to.be.undefined
expect(err).to.be.eql(new Error('connect ECONNREFUSED 127.0.0.1:80'))
});
})
})
describe("start", function () {
it("Let's start the timer", async function () {
//custom timeout
this.timeout(15000);
// classic build
myListener = new Listeners('my-test-service', 'fake.rss.service', 2, [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
//spy
const fun_spy = sinon.spy();
// start timer
myListener.start(fun_spy);
await new Promise(res => setTimeout(res, 5 * 1000));
myListener.stop();
//assertion
// calls
expect(1).to.be.eql(1);
expect(fun_spy).to.have.been.callCount(3);
expect(fun_spy).to.have.been.calledWith(mockedRSSOutput, undefined);
});
it("Let's start the timer (with a bad address)", async function () {
//custom timeout
this.timeout(15000)
// classic build
myListener = new Listeners('my-test-service', 'bad.rss.service', 2, [
['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']]
]);
//spy
const fun_spy = sinon.spy();
// start timer
myListener.start(fun_spy);
await new Promise(res => setTimeout(res, 5 * 1000));
myListener.stop();
//assertion
// calls
expect(1).to.be.eql(1);
expect(fun_spy).to.have.been.callCount(3);
expect(fun_spy).to.have.been.calledWith(undefined, Error); //yagni
});
});
});