forked from borjiso/node-red-contrib-scrape-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape-it.js
26 lines (24 loc) · 1.03 KB
/
scrape-it.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
module.exports = function(RED) {
function scrape(config) {
RED.nodes.createNode(this,config);
this.mapping = config.mapping;
this.sourceProperty = config.sourceProperty;
this.targetProperty = config.targetProperty;
var node = this;
const scrapeIt = require('scrape-it')
node.on('input', function(msg) {
source = RED.util.getMessageProperty(msg, this.sourceProperty);
mapping = JSON.parse( node.mapping );
try {
let result = scrapeIt.scrapeHTML(source, mapping);
RED.util.setMessageProperty(msg, this.targetProperty, result, true);
} catch(err){
node.error(`error occurred while setting the target property${this.targetProperty}: ${err}`);
node.status({fill:'red', shape:'ring',text:`Error applying the target property:${this.targetProperty}`});
return;
}
node.send(msg);
});
}
RED.nodes.registerType("scrape",scrape);
}