Code Monkey home page Code Monkey logo

Comments (6)

fent avatar fent commented on May 25, 2024

There's an example in a test.

Basically, call new FeedMe(true), and wait for the "done" event where you can call parser.done().

from feedme.js.

leihelondon avatar leihelondon commented on May 25, 2024

Thanks for you swift response, @fent! I'm sorry I don't think I get you. What do you mean by "wait for the "done" event where you can call parser.done()"?

Basically, what I'd like to do is to get the first 4 items in this feed http://feeds.bbci.co.uk/mundo/rss.xml. But at the moment, I got all the items. What do you think is the best way of doing it?

Here's my code:

var http = require('http');
var FeedMe = require('feedme');

var url = 'http://feeds.bbci.co.uk/mundo/rss.xml';
var parser = new FeedMe(true);

parser.on('item', function(item) {
  console.log(item);
});

parser.on('end', function() {
  console.log('end');
});

http.get(url, function(res) {
  res.pipe(parser);
});

from feedme.js.

fent avatar fent commented on May 25, 2024

What do you mean by "wait for the "done" event where you can call parser.done()"?

What I mean is call .done() inside of the event callback.

parser.on('end', function() {
  var items = parser.done();
});

But if you only want the first 4 items, you'll probably want to listen for the first 4 "item" events.

from feedme.js.

leihelondon avatar leihelondon commented on May 25, 2024

Thanks a lot @fent! That was helpful. So I tweaked the code as you suggested (see following):

var http = require('http');
var FeedMe = require('feedme');

var url = 'http://feeds.bbci.co.uk/mundo/rss.xml';
var parser = new FeedMe(true);

parser.on('item', function(item) {
  //console.log(item);
});

parser.on('end', function() {
  var items = parser.done();
  console.log(items);
});

http.get(url, function(res) {
  res.pipe(parser);
});

But I cannot get the full object, missing guid and media thumbnail. They were shown as [object]. Is it possible to get them? Here's the incompleteconsole.log(items):

{ type: 'rss 2.0',
  title: 'BBC Mundo - Noticias',
  description: 'BBC Mundo - Noticias',
  link: 'http://www.bbcmundo.com',
  image:
   { url: 'http://www.bbc.co.uk/mundo/images/gel/rss_logo.gif',
     title: 'BBC Mundo - Noticias',
     link: 'http://www.bbcmundo.com' },
  generator: 'RSS for Node',
  lastbuilddate: 'Wed, 04 May 2016 17:52:35 GMT',
  copyright: 'Derechos Reservados de la British Broadcasting Corporation (Corporación Británica de Radiodifusión) ',
  language: 'es',
  managingeditor: '[email protected]',
  ttl: '15',
  items:
   [ { title: '¿Qué tan probable es que el millonario Donald Trump llegue a ser presidente de Estados Unidos?',
       description: 'El magnate será previsiblemente el candidato del Partido Republicano en las presidenciales de noviembre. Hace un año parecía imposible que ocupara la Casa Blanca. Pero ahora ya no.',
       link: 'http://www.bbc.com/mundo/noticias/2016/05/160503_internacional_elecciones_estados_unidos_donald_trump_dgm',
       guid: [Object],
       pubdate: 'Wed, 04 May 2016 06:44:18 GMT',
       'media:thumbnail': [Object] },
     { title: 'Las 5 familias más ricas de América Latina',
       description: 'Algunos de sus apellidos son conocidos y muchas tienen un punto en común: sus negocios están relacionados con una mega empresa cervecera que les ha ayudado a aumentar sus fortunas a niveles estratosféricos.',
       link: 'http://www.bbc.com/mundo/noticias/2016/05/160503_economia_dinastias_familiares_mas_ricas_america_latina',
       guid: [Object],
       pubdate: 'Tue, 03 May 2016 22:30:26 GMT',
       'media:thumbnail': [Object] },
     ...
    ] }

from feedme.js.

leihelondon avatar leihelondon commented on May 25, 2024

I've also tried to get the first 4 items with this code, but it doesn't work. What do you mean by "listen for the first 4 item events"? Thanks again!

var http = require('http');
var FeedMe = require('feedme');

var url = 'http://feeds.bbci.co.uk/mundo/rss.xml';
var parser = new FeedMe(true);

parser.on('item', function(item) {
  var counter = 0;
  counter++;
  if (counter===4) {
    parser.emit('end');
    console.log(item);
  }
});

parser.on('end', function() {
  var items = parser.done();
  //console.log(items);
});

http.get(url, function(res) {
  res.pipe(parser);
});

from feedme.js.

fent avatar fent commented on May 25, 2024

guid and media:thumbnail are there, but console.log() has a limited depth of what it will print in a terminal. Use console.dir() if you want more depth.

from feedme.js.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.