Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use FeedParser as a transform stream
let Transform = require('stream').Transform
let FeedParser = require('feedparser')
let pump = require('pump')
class Filter extends Transform {
constructor(articles_max, feedparser) {
super()
this._writableState.objectMode = true // we can eat objects
this.articles_max = articles_max
this.articles_count = 0
if (feedparser) {
this.once('unpipe', () => {
this.end() // ensure 'finish' event gets emited
})
}
this.feedparser = feedparser
}
_transform(input, encoding, done) {
if (this.articles_count++ < this.articles_max) {
this.push(input.title + '\n')
} else {
console.error('stop on', this.articles_count)
if (this.feedparser) this.feedparser.unpipe(this)
}
done()
}
}
let articles_max = Number(process.argv.slice(2)) || 1
let fp = new FeedParser()
pump(process.stdin, fp, new Filter(articles_max, fp), process.stdout,
err => err && err.code !== 'EPIPE' && console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment