Created
April 1, 2018 17:50
-
-
Save gromnitsky/d282dd0a1c0478b81c8c3db8b3d596c9 to your computer and use it in GitHub Desktop.
Use FeedParser as a transform stream
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
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