Last active
August 19, 2018 06:31
-
-
Save midudev/6f05b27e16c98f887995 to your computer and use it in GitHub Desktop.
Example of using feedparser (https://github.com/danmactough/node-feedparser) with ES6 on node.js >= 4.0.0
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
'use strict' | |
const FeedParser = require('feedparser') | |
const request = require('request') | |
let req = request('https://github.com/danmactough.atom') | |
let parser = new FeedParser() | |
req.on('error', (err) => { | |
// handle request error | |
console.log(err) | |
}) | |
req.on('response', (res) => { | |
// check if status code is not correct | |
if (res.statusCode !== 200) { | |
return req.emit('error', new Error('Bad status code')) | |
} | |
// if the res is correct, when can pipe the response | |
req.pipe(parser) // pipe response to feedparser | |
}) | |
parser.on('error', (err) => { | |
// handle parser error | |
console.log(err) | |
}) | |
parser.on('end', () => { | |
// handle that we've finished reading articles | |
console.log('End parsing') | |
}) | |
parser.on('readable', () => { | |
let item = parser.read() | |
let meta = parser.meta // get the metadata of the feed | |
while (item) { | |
// do whatever you want with the item | |
console.log(item) | |
// get the next item, if none, then item will be null next time | |
item = parser.read() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used Standard Style to code the gist: https://github.com/feross/standard