-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-function.js
30 lines (29 loc) · 903 Bytes
/
rss-function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const _ = require('lodash');
const request = require('request');
const needle = require('needle');
const feedparser = require('feedparser');
const es = require('event-stream');
/**
* @param context {WebtaskContext}
*/
module.exports = function(context, cb) {
if(context.secrets.token !== context.query.token) {
return cb('No token.');
}
const rss = _.get(context, 'query.rss', _.get(context, 'body.rss'));
if(!rss) {
return cb('No rss param provided.');
}
const max = _.get(context, 'query.max', _.get(context, 'body.max', _.get(context, 'secrets.max')));
return needle.get(rss, {follow_max: 5, parse: false})
.pipe(new feedparser())
.pipe(es.writeArray(function (err, arr) {
if(err) {
return cb(err);
}
return cb(null, {rss: arr.slice(0, +max)});
}))
.on('error', function(err) {
cb(err);
});
};