Created
October 8, 2015 18:47
-
-
Save lexicalbits/346f3eada13b4f0c3881 to your computer and use it in GitHub Desktop.
Tutorial: RSS Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'), | |
FeedParser = require('feedparser'), | |
_ = require('lodash'); | |
module.exports = { | |
run: function(step, dexter) { | |
var url = step.input('url').first(), | |
filter = step.input('filter').first(), | |
self = this; | |
if(!url) { | |
this.log('No url provided, returing to App'); | |
//It's not really a critical failure to not have an url to work with, so we'll roll on. | |
return this.complete(); | |
} | |
//We've got an url, so let's use it | |
this.fetchUrl(url, function(err, stream) { | |
if(err) { | |
//It IS a big deal if the url we're trying to contact isn't workable, so we'll fail here. | |
return self.fail(err); | |
} | |
//Let the parser grab the data | |
self.fetchItems(stream, function(err, items) { | |
var response = []; | |
if(err) { | |
return self.fail(err); | |
} | |
//Extract dexter-friendly data from the items | |
_.each(items, function(item) { | |
response.push({ | |
url: item.link, | |
title: item.title, | |
summary: item.summary, | |
author: item.author | |
}); | |
}); | |
if(filter) { | |
response = self.filterItems(response, filter); | |
} | |
return self.complete(response); | |
}); | |
}); | |
}, | |
fetchUrl: function(url, callback) { | |
var req = request(url); | |
req.on('error', callback); | |
req.on('response', function(resp) { | |
if(resp.statusCode != 200) { | |
return callback(new Error('Bad status code ' + resp.statusCode + ' for ' + url)); | |
} | |
return callback(null, this); | |
}); | |
}, | |
fetchItems: function(stream, callback) { | |
var parser = new FeedParser() | |
, items = []; | |
parser.on('error', callback); | |
parser.on('end', function(err) { | |
if(err) { | |
return callback(err); | |
} | |
return callback(null, items); | |
}); | |
parser.on('readable', function() { | |
var item; | |
while((item = this.read())) { | |
items.push(item); | |
} | |
}); | |
stream.pipe(parser); | |
}, | |
filterItems: function(items, term) { | |
var response = [] | |
, lcTerm = term.toLowerCase(); | |
_.each(items, function(item) { | |
_.forIn(item, function(val) { | |
if(val.toLowerCase().indexOf(lcTerm) >= 0) { | |
response.push(item); | |
return false; | |
} | |
}); | |
}); | |
return response; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"inputs": [ | |
{ | |
"id": "url", | |
"title":"Feed URL" | |
}, { | |
"id": "filter", | |
"title": "Search term" | |
} | |
], | |
"outputs": [ | |
{ | |
"id": "url", | |
"title":"Article URL" | |
}, { | |
"id": "title", | |
"title":"Article title" | |
}, { | |
"id": "summary", | |
"title":"Article summary" | |
}, { | |
"id": "author", | |
"title":"Author" | |
} | |
], | |
"title": "Dexter Docs: RSS Filter" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "dexterdocs-rss-filter", | |
"version": "0.0.1", | |
"description": "", | |
"main": "index.js", | |
"author": "RunDexter Documentation", | |
"license": "MIT", | |
"private": true, | |
"dependencies": { | |
"feedparser": "^1.1.3", | |
"lodash": "^3.9.3", | |
"request": "^2.61.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment