danmactough / node-feedparser

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

How to read the outputted JS object #242

Closed yPhil-gh closed 6 years ago

yPhil-gh commented 6 years ago

Hi!

This is not an issue per se, as feedparser works like a charm for me :1st_place_medal: but I want to dig further into the returned object ; Say for example the CNN feed:

(
  {"title":"Merkel's critics will miss her when she's gone",
   "description":"In her own words, it was a day for \"deep reflection.\" Angela Merkel was nothing if not diplomatic when she met the press early on Monday morning after her coalition negotiations had broken down with the Free Democrats and the Greens.",
   "summary":"In her own words, it was a day for \"deep reflection.\" Angela Merkel was nothing if not diplomatic when she met the press early on Monday morning after her coalition negotiations had broken down with the Free Democrats and the Greens.",
   "date":"2017-11-21T12:54:58.000Z",
   "pubdate":"2017-11-21T12:54:58.000Z",
   "pubDate":"2017-11-21T12:54:58.000Z",
   "link":"http://www.cnn.com/2017/11/21/opinions/you-will-miss-merkel-when-she-goes-mattq-opinion/index.html",
   "guid":"http://www.cnn.com/2017/11/21/opinions/you-will-miss-merkel-when-she-goes-mattq-opinion/index.html",
   "author":null,
   "comments":null,
   "origlink":null,
   "image":{},
   "source":{},
   "categories":[],
   "enclosures":[],
   "rss:@":{},
   "rss:title":{
     "@":{},
     "#":"Merkel's critics will miss her when she's gone"
   },
   "rss:description":{
     "@":{},
     "#":"In her own words, it was a day for \"deep reflection.\" Angela Merkel was nothing if not diplomatic when she met the press early on Monday morning after her coalition negotiations had broken down with the Free Democrats and the Greens."
   },"rss:link":{
     "@":{},
     "#":"http://www.cnn.com/2017/11/21/opinions/you-will-miss-merkel-when-she-goes-mattq-opinion/index.html"
   },
   "permalink":"http://www.cnn.com/2017/11/21/opinions/you-will-miss-merkel-when-she-goes-mattq-opinion/index.html",
   "rss:guid":{
     "@":{
       "ispermalink":"true"
     },
     "#":"http://www.cnn.com/2017/11/21/opinions/you-will-miss-merkel-when-she-goes-mattq-opinion/index.html"
   },
   "rss:pubdate":{
     "@":{},
     "#":"Tue, 21 Nov 2017 12:54:58 GMT"
   },
   "media:group":{
     "@":{},
     "media:content":[
       {
         "@":{
           "medium":"image",
           "url":"http://i2.cdn.turner.com/cnnnext/dam/assets/170925131456-04-angela-merkel-0925-super-169.jpg",
           "height":"619",
           "width":"1100"
         }
       },
...

This is what I get in item from a $.each(data.feedItems, function(index, item) {} and it's very neat and predictable, now how would I then go and get the url of the first found media:group:media:content tag? I can't seem to get past/traverse the @ and # entries, and I can't find any reference to a way to select this form of object ; please excuse me if it's a dumb question :|

yPhil-gh commented 6 years ago

Hey, I found out!

      if (item['media:group']) {
        console.log( "Media: (%s)", JSON.stringify(item['media:group']['media:content']));
        var myArray = item['media:group']['media:content'];
        for (var i = 0; i < myArray.length; i++) {
          console.log('Wopopop:' + JSON.stringify(myArray[i]['@'].url));
        }
      }

Sorry about the noise :|