-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (75 loc) · 1.9 KB
/
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var fs = require('fs'),
_ = require('underscore'),
feedparser = require('feedparser'),
express = require('express'),
app = express();
app.use(express.bodyParser());
// Simple logger
app.use(function (request, response, next) {
console.log('%s %s', request.method, request.url);
next();
});
// Handle uncaughtException
process.on('uncaughtException', function (error) {
exit('Error: ' + error.message);
});
var exit = function (message) {
if (message) {
console.log(message);
}
console.log('Exiting...');
process.exit(1);
};
app.options('/', function (request, response) {
// CORS support
response.set('Access-Control-Allow-Origin', '*');
response.set('Access-Control-Allow-Methods', 'OPTIONS,POST');
response.set('Access-Control-Allow-Headers', 'Content-Type');
// The block definition
response.send({
name: "Parse RSS",
description: "Parse an RSS feed into a series of records.",
inputs: [{
name: "url",
type: "string",
description: "The URL for the RSS feed."
}],
outputs: [{
name: "entries",
type: "array",
description: "All entries from the RSS feed."
}]
});
});
app.post('/', function (request, response) {
if (!_.has(request.body, 'inputs') && _.isObject(request.body.inputs)) {
exit('WebPipe "input" is missing or formatted incorrectly.');
}
var inputs = request.body.inputs[0];
var outputs = {
outputs: []
};
// Verify input key exist
if (!_.has(inputs, 'url')) {
exit('Required "url" is missing.');
}
feedparser.parseFile(inputs.url, function callback(error, meta, articles) {
if (error) {
console.error(error);
response.send(500);
} else {
articles.forEach(function (article) {
outputs.outputs.push({
date: article.date,
title: article.title,
link: article.link
});
});
response.json(outputs);
}
});
});
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Listening on ' + port);
});