2021-03-12 13:53:08 +01:00
|
|
|
# listener rss
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
A lightweight library to make simple actions with a RSS feed.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
# USAGE
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
## Punctual usage
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
You can parse RSS from a URL with some custom data.
|
|
|
|
An example :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
2021-03-12 13:53:08 +01:00
|
|
|
const ListenerModule = require("ListenerRSS");
|
|
|
|
const ListenerRss = ListenerModule.ListenerRss;
|
2021-02-07 16:18:16 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
const listener = new ListenerRss("my-test-service", "fake.rss.service");
|
2021-02-07 16:18:16 +01:00
|
|
|
|
|
|
|
// make a request to the adr 'fake.rss.service'
|
|
|
|
myListener.fetchRSS().then((obj, err) => {
|
2021-02-07 17:50:54 +01:00
|
|
|
// some act
|
2021-02-07 16:18:16 +01:00
|
|
|
});
|
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
## Recurrent usage
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
You can parse RSS from a URL each n times.
|
|
|
|
An example :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
2021-03-12 13:53:08 +01:00
|
|
|
const ListenerModule = require("ListenerRSS");
|
|
|
|
const ListenerRss = ListenerModule.ListenerRss;
|
2021-02-07 16:18:16 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
const listener = new ListenerRss("my-test-service", "fake.rss.service", 5 * 60);
|
2021-02-07 16:18:16 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
const callback_fun_update = (obj) => {
|
2021-02-07 17:50:54 +01:00
|
|
|
// some act
|
|
|
|
};
|
2021-03-12 13:53:08 +01:00
|
|
|
const callback_fun_new_entries = (obj) => {
|
|
|
|
// some act
|
|
|
|
};
|
|
|
|
|
|
|
|
listener.on("update", callback_fun);
|
|
|
|
listener.on("newEntries", callback_fun);
|
|
|
|
|
|
|
|
listener.start();
|
2021-02-07 16:18:16 +01:00
|
|
|
|
|
|
|
/*...*/
|
|
|
|
|
|
|
|
listener.stop();
|
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
# Documentation
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
## ListenerRSSInfo
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
An interface to structure listener's data.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
### Constructor
|
2021-03-23 18:20:42 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
- address : the service address
|
|
|
|
- [optional] timeloop : time to wait between 2 request in seconds (default 5 minutes)
|
2021-02-07 17:50:54 +01:00
|
|
|
- [optional] customfields : to notice field who's custom to the service (default blank)
|
2021-02-07 16:18:16 +01:00
|
|
|
[cf annexe CustomFields](#customfields)
|
2021-03-23 18:20:42 +01:00
|
|
|
- [optional] lastEntriesLinks : to specify an predefined history.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
## ListenerRSS
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
### Constructor
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
`constructor(listenerRSSInfo)`
|
2021-02-07 17:50:54 +01:00
|
|
|
|
|
|
|
- listenerRSSInfo : object from the ListenerRSSInfo's class.
|
2021-02-07 16:18:16 +01:00
|
|
|
|
|
|
|
### fetchRSS()
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
This function allows to make a request to the rss service.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
#### Return
|
2021-02-07 17:50:54 +01:00
|
|
|
|
|
|
|
Return a promise object who's resolved like `resolve: (value: result_fetch) => void))` where `result_fetch` is a json
|
2021-02-07 16:18:16 +01:00
|
|
|
object who's contain the data. [cf Annexe Output](#output)
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
#### Issues
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
Reject the promise if the server can't be resolved.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
### start()
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-23 18:20:42 +01:00
|
|
|
This function will call the `update` event to each success update, the
|
2021-03-12 13:53:08 +01:00
|
|
|
`update_err` event to each fail update, and the `newEntries` event for
|
|
|
|
each update who contains a new item.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
#### Events
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
Each event take one arg into the callback function.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
2021-03-12 13:53:08 +01:00
|
|
|
const callback_fun = (obj) => {
|
2021-03-23 18:20:42 +01:00
|
|
|
// some act
|
2021-02-07 17:50:54 +01:00
|
|
|
};
|
2021-03-12 13:53:08 +01:00
|
|
|
const callback_fun_err = (err) => {
|
2021-03-23 18:20:42 +01:00
|
|
|
// some act
|
2021-03-12 13:53:08 +01:00
|
|
|
};
|
|
|
|
const callback_fun_new_entries = (newEntries) => {
|
2021-03-23 18:20:42 +01:00
|
|
|
// some act
|
2021-03-12 13:53:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
listener.on("update", callback_fun);
|
|
|
|
listener.on("update_err", callback_fun_err);
|
2021-03-23 18:20:42 +01:00
|
|
|
listener.on("new_entries", callback_fun_new_entries);
|
2021-02-07 16:18:16 +01:00
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
#### update
|
2021-03-23 18:20:42 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
It used a callback who receive the received object entirely inside an object.
|
2021-03-23 18:20:42 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
#### update_err
|
2021-03-23 18:20:42 +01:00
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
It used a callback who receive an error object.
|
2021-03-23 18:20:42 +01:00
|
|
|
|
|
|
|
#### new_entries
|
|
|
|
|
2021-03-12 13:53:08 +01:00
|
|
|
It used a callback who receive only new entries inside an array.
|
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
### stop()
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
This function will stop the execution of the callbackFun each time loop.
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
# Annexe
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
## CustomFields
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
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 :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
2021-02-07 17:50:54 +01:00
|
|
|
[["media:group", "media:group"]];
|
2021-02-07 16:18:16 +01:00
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
You can also rename the field with the left part :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
2021-02-07 17:50:54 +01:00
|
|
|
[["my_custom_media_group_name", "media:group"]];
|
2021-02-07 16:18:16 +01:00
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
In adition you can rename child element into custom field like this :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
|
|
|
[
|
2021-02-07 17:50:54 +01:00
|
|
|
["media:group", "media:group"],
|
|
|
|
["description", ["media:group", "media:description"]],
|
|
|
|
["icon", ["media:group", "media:thumbnail"]],
|
|
|
|
];
|
2021-02-07 16:18:16 +01:00
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
In this case it's useless to specify the parent field, so you can just omit the first line :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```js
|
|
|
|
[
|
2021-02-07 17:50:54 +01:00
|
|
|
["description", ["media:group", "media:description"]],
|
|
|
|
["icon", ["media:group", "media:thumbnail"]],
|
|
|
|
];
|
2021-02-07 16:18:16 +01:00
|
|
|
```
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
## Output
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
Here an example of what type of json object is output during a fetch :
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-07 16:18:16 +01:00
|
|
|
```json
|
2021-03-12 13:53:08 +01:00
|
|
|
{
|
|
|
|
"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> & <b>this is bold text</b>",
|
|
|
|
"contentSnippet": "this is a link & this is bold text",
|
|
|
|
"guid": "fake.rrs.service/item1",
|
2021-03-23 18:20:42 +01:00
|
|
|
"categories": ["test", "npm", "fakeInfos"],
|
2021-03-12 13:53:08 +01:00
|
|
|
"isoDate": "2015-11-12T21:16:39.000Z"
|
2021-03-23 18:20:42 +01:00
|
|
|
}
|
2021-03-12 13:53:08 +01:00
|
|
|
/*Some Others items*/
|
|
|
|
]
|
|
|
|
}
|
2021-02-07 17:50:54 +01:00
|
|
|
```
|