如何获取多个RSS源并将它们合并到一个Feed中,然后将合并的源转换为JSON数据? (JavaScript的)

时间:2023-01-24 09:09:04

I am trying to merge multiple RSS feeds into one and convert it to JSON. To merge the RSS feeds i used this package: rss-combiner

我正在尝试将多个RSS提要合并为一个并将其转换为JSON。要合并RSS源,我使用了这个包:rss-combiner

Here is the code i used to merge the RSS feeds it works:

这是我用来合并它的RSS源的代码:

var RSSCombiner = require('rss-combiner');

var feedConfig = {
  title: 'Tech news from Guardian and BBC',
  size: 20,
  feeds: [
    'http://feeds.bbci.co.uk/news/technology/rss.xml',
    'https://www.theguardian.com/uk/technology/rss'
  ],
  pubDate: new Date()
};

RSSCombiner(feedConfig)
.then(function (combinedFeed) {
  xml = combinedFeed.xml();
  console.log(xml);
});

Then to convert a feed to JSON i used this package: rss-to-json with this code and it works:

然后将源转换为JSON我使用此包:rss-to-json使用此代码并且它可以工作:

var Feed = require('rss-to-json');

Feed.load('https://codek.tv/feed/', function(err, rss){
   console.log(rss);
});

The problem is when i try to convert the merged feed to JSON, i don't get any results with this code:

问题是,当我尝试将合并的源转换为JSON时,我没有得到任何结果与此代码:

var RSSCombiner = require('rss-combiner');
var Feed = require('rss-to-json');

var feedConfig = {
  title: 'Tech news from Guardian and BBC',
  size: 20,
  feeds: [
    'http://feeds.bbci.co.uk/news/technology/rss.xml',
    'https://www.theguardian.com/uk/technology/rss'
  ],
  pubDate: new Date()
};

// combine feeds
RSSCombiner(feedConfig)
.then(function (combinedFeed) {
  var xml = combinedFeed.xml();

  // convert combined feed to json
  Feed.load(xml, function(err, rss){ 
    console.log(rss);
  });
});

1 个解决方案

#1


0  

I think rss-to-json is asking for a URL; you're passing the XML result. Why not just use xml2js after you combining the RSS?

我认为rss-to-json要求提供一个URL;你正在传递XML结果。为什么不在组合RSS后使用xml2js?

const RSSCombiner = require('rss-combiner');
const xml2js = require('xml2js');

const feedConfig = {
  title: 'Tech news from Guardian and BBC',
  size: 20,
  feeds: [
    'http://feeds.bbci.co.uk/news/technology/rss.xml',
    'https://www.theguardian.com/uk/technology/rss',
  ],
  pubDate: new Date(),
};

RSSCombiner(feedConfig)
  .then((combinedFeed) => {
    const xml = combinedFeed.xml();
    const parser = xml2js.Parser();

    parser.parseString(xml, (err, result) => {
      console.log(result);
    });
  });

#1


0  

I think rss-to-json is asking for a URL; you're passing the XML result. Why not just use xml2js after you combining the RSS?

我认为rss-to-json要求提供一个URL;你正在传递XML结果。为什么不在组合RSS后使用xml2js?

const RSSCombiner = require('rss-combiner');
const xml2js = require('xml2js');

const feedConfig = {
  title: 'Tech news from Guardian and BBC',
  size: 20,
  feeds: [
    'http://feeds.bbci.co.uk/news/technology/rss.xml',
    'https://www.theguardian.com/uk/technology/rss',
  ],
  pubDate: new Date(),
};

RSSCombiner(feedConfig)
  .then((combinedFeed) => {
    const xml = combinedFeed.xml();
    const parser = xml2js.Parser();

    parser.parseString(xml, (err, result) => {
      console.log(result);
    });
  });