Created
October 9, 2015 01:06
-
-
Save neofreko/3a9f24b951615feea494 to your computer and use it in GitHub Desktop.
parse feed and run tldr on the feed item urls
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 FeedParser = require('feedparser'), | |
request = require('request'), | |
summary = require('node-tldr'), | |
Promise = require('promise'); | |
function summarize(link) { | |
return new Promise(function(resolve, reject) { | |
summary.summarize(link, function(result, failure) { | |
if (failure) { | |
console.log("An error occured! " + result.error); | |
reject(result.error) | |
} | |
resolve(result) | |
}) | |
}) | |
} | |
main_promise = new Promise(function(resolve, reject) { | |
var req = request('https://aws.amazon.com/blogs/aws/feed/'), | |
feedparser = new FeedParser([]); | |
req.on('error', function(error) { | |
// handle any request errors | |
}); | |
req.on('response', function(res) { | |
var stream = this; | |
if (res.statusCode != 200) return this.emit('error', new Error('Bad status code')); | |
stream.pipe(feedparser); | |
}); | |
var item_promises = [] | |
feedparser.on('error', function(error) { | |
// always handle errors | |
console.log(error) | |
}); | |
feedparser.on('readable', function() { | |
// This is where the action is! | |
var stream = this, | |
meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance | |
, | |
item; | |
while (item = stream.read()) { | |
//console.log(item.title, item.link); | |
item_promises.push(summarize(item.link)); | |
} | |
}) | |
feedparser.on('end', function (err) { | |
if (err) { | |
console.log(err, err.stack); | |
return process.exit(1); | |
} | |
Promise.all(item_promises) | |
.then(function(res) { | |
resolve(res) | |
}) | |
}); | |
}); | |
main_promise.then(function(res) { | |
//console.log('DONE', res) | |
res.forEach(function (result) { | |
console.log(result.title); | |
console.log(result.words); | |
console.log(result.compressFactor); | |
console.log(result.summary.join("\n")); | |
console.log("\n\n\n") | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment