danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 192 forks source link

How do I access channel/title (not item/title)? #158

Closed ghost closed 8 years ago

ghost commented 8 years ago

Using the stack overflow podcast[1] as an example. I'm able to get all the item/title's but not the main channel/title using this:

    var FeedParser = require('feedparser')
      , request = require('request');

    var req = request('http://blog.stackoverflow.com/feed/podcast/')
      , 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);
    });

    feedparser.on('error', function(error) {
    });
    feedparser.on('readable', function() {
      var stream = this
        , meta = this.meta
        , item;

    while (item = stream.read()) {
        if(err) { return console.dir(err); }
            var collection = db.collection('Stack');
            var doc = {'Title': item['title'], 'URL': item['link']};
            collection.insert(doc);
            logger.log(item);
    }
    });

I tried this not knowing if the variable was being used as an xpath argument somehow... probably stupid of me.

feedparser.on('readable', function() {
  var stream = this
    , meta = this.meta
    , item
    , channel;

while (channel = stream.read()) {
    if(err) { return console.dir(err); }
    console.log(channel['title']);
}
});
  1. http://blog.stackoverflow.com/feed/podcast/
danmactough commented 8 years ago

"Channel" data is in the meta property (so-called because "channel" is an RSS-specific term).

feedparser.on('readable', function() {
  var stream = this,
    meta = this.meta,
    item;

 var channelTitle = this.meta.title;
});