Skip to content

Commit

Permalink
Update examples to use node-fetch in place of request
Browse files Browse the repository at this point in the history
Because request has been deprecated
  • Loading branch information
danmactough committed May 1, 2020
1 parent e3f590e commit 3affacc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 494 deletions.
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,27 @@ npm install feedparser

This example is just to briefly demonstrate basic concepts.

**Please** also review the [compressed example](examples/compressed.js) for a
**Please** also review the [complete example](examples/complete.js) for a
thorough working example that is a suitable starting point for your app.

```js

var FeedParser = require('feedparser');
var request = require('request'); // for fetching the feed
var fetch = require('node-fetch'); // for fetching the feed

var req = request('http://somefeedurl.xml')
var req = fetch('http://somefeedurl.xml')
var feedparser = new FeedParser([options]);

req.on('error', function (error) {
// handle any request errors
});

req.on('response', function (res) {
var stream = this; // `this` is `req`, which is a stream

if (res.statusCode !== 200) {
this.emit('error', new Error('Bad status code'));
req.then(function (res) {
if (res.status !== 200) {
throw new Error('Bad status code');
}
else {
stream.pipe(feedparser);
// The response `body` -- res.body -- is a stream
res.body.pipe(feedparser);
}
}, function (err) {
// handle any request errors
});

feedparser.on('error', function (error) {
Expand Down Expand Up @@ -202,7 +199,7 @@ the original inspiration and a starting point.

(The MIT License)

Copyright (c) 2011-2018 Dan MacTough and contributors
Copyright (c) 2011-2020 Dan MacTough and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
Expand Down
52 changes: 24 additions & 28 deletions examples/iconv.js → examples/complete.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
/**
* Tips
* Tip
* ====
* - Set `user-agent` and `accept` headers when sending requests. Some services will not respond as expected without them.
* - Set `pool` to false if you send lots of requests using "request" library.
*/

var request = require('request')
var fetch = require('node-fetch')
, FeedParser = require(__dirname+'/..')
, iconv = require('iconv-lite');

function fetch(feed) {
// Define our streams
var req = request(feed, {timeout: 10000, pool: false});
req.setMaxListeners(50);
// Some feeds do not respond without user-agent and accept headers.
req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
req.setHeader('accept', 'text/html,application/xhtml+xml');
function get(feed) {
// Get a response stream
fetch(feed, { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36', 'accept': 'text/html,application/xhtml+xml' }).then(function (res) {

var feedparser = new FeedParser();
// Setup feedparser stream
var feedparser = new FeedParser();
feedparser.on('error', done);
feedparser.on('end', done);
feedparser.on('readable', function() {
var post;
while (post = this.read()) {
console.log(JSON.stringify(post, ' ', 4));
}
});

// Define our handlers
req.on('error', done);
req.on('response', function(res) {
if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
var charset = getParams(res.headers['content-type'] || '').charset;
res = maybeTranslate(res, charset);
// Handle our response and pipe it to feedparser
if (res.status != 200) throw new Error('Bad status code');
var charset = getParams(res.headers.get('content-type') || '').charset;
var responseStream = res.body;
responseStream = maybeTranslate(responseStream, charset);
// And boom goes the dynamite
res.pipe(feedparser);
});
responseStream.pipe(feedparser);

feedparser.on('error', done);
feedparser.on('end', done);
feedparser.on('readable', function() {
var post;
while (post = this.read()) {
console.log(JSON.stringify(post, ' ', 4));
}
});
}).catch(done);
}

function maybeTranslate (res, charset) {
Expand Down Expand Up @@ -82,8 +77,9 @@ function done(err) {
var server = require('http').createServer(function (req, res) {
var stream = require('fs').createReadStream(require('path').resolve(__dirname, '../test/feeds' + req.url));
res.setHeader('Content-Type', 'text/xml; charset=Windows-1251');
res.setHeader('Content-Encoding', 'gzip');
stream.pipe(res);
});
server.listen(0, function () {
fetch('http://localhost:' + this.address().port + '/iconv.xml');
get('http://localhost:' + this.address().port + '/compressed.xml');
});
96 changes: 0 additions & 96 deletions examples/compressed.js

This file was deleted.

Loading

0 comments on commit 3affacc

Please sign in to comment.