MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Problems with rss

    Scheduled Pinned Locked Moved Development
    4 Posts 2 Posters 2.2k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      retroflex Project Sponsor Module Developer
      last edited by retroflex

      This is my first attempt at a module using a node_helper.js. My goal is to get an rss and output all rss items on the mirror. I have got the basics working, which is a function in node_helper.js which sends items over to the module js file:

      loadFeed: function(url) {
          var items = [];
          items.push( { title: 'test title', description: 'test description' } );
          
          this.sendSocketNotification('ITEMS', {
            items: items
          });
      },
      

      Output:
      0_1511351554749_087d9f1a-6667-4f48-9602-44b9843419db-image.png

      But when I try to read and parse an rss, I get nothing… Code:

      const FeedMe = require('feedme');
      const http = require('http');
      
      ...
      
      loadFeed: function(url) {
          var items = [];
          
          items.push( { title: 'before', description: 'before' } );
             
          http.get('http://www.npr.org/rss/rss.php?id=1001', (res) => {
            var parser = new FeedMe();
            parser.on('title', (title) => {
              items.push( { title: 'feedtitle', description: 'feedtitle' } );
            });
            parser.on('item', (item) => {
              items.push( { title: 'feeditem', description: 'feeditem' } );
            });
            res.pipe(parser);
          });
      
          items.push( { title: 'after', description: 'after' } );
          
          this.sendSocketNotification('ITEMS', {
            items: items
          });
      },
      
      

      Output:
      0_1511351947298_0176f83f-2af5-45ed-8d09-bb4781d79036-image.png

      “ls node_modules” show that both feedme and http is installed. No relevant errors in .pm2/logs/mm-error-0.log or mm-out-0.log.

      So I tried with another rss parser:

      const parser = require('rss-parser');
      
      ...
      
      loadFeed: function(url) {
          var items = [];
          
          items.push( { title: 'before', description: 'before' } );
             
          parser.parseURL('http://www.npr.org/rss/rss.php?id=1001', function(err, parsed) {
            parsed.feed.entries.forEach(function(entry) {
              items.push( { title: 'entrytitle', description: entry.title } );
            });
          });
      
          items.push( { title: 'after', description: 'after' } );
          
          this.sendSocketNotification('ITEMS', {
            items: items
          });
      },
      

      Same output as above. No log errors. And rss-parser is installed in node_modules. Also this last code works fine in npm RunKit:
      https://runkit.com/embed/q4ucjhbbzpw3

      Any ideas what is failing? Or how to debug?

      ? 1 Reply Last reply Reply Quote 0
      • ? Offline
        A Former User @retroflex
        last edited by A Former User

        @retroflex I didn’t look inside deeply, but I think it might be caused by asynchronous problem.
        I suspect sendNotification is performed before parsing rss(item.push)
        Test it with console.log() before sendNotification and item pushing.

        R 1 Reply Last reply Reply Quote 2
        • R Offline
          retroflex Project Sponsor Module Developer @Guest
          last edited by

          @Sean Thanks! Logging shows your suspicions were correct:

          MYMODULE Thu Nov 23 2017 09:43:15 GMT+0100 (CET): start of method
          MYMODULE Thu Nov 23 2017 09:43:15 GMT+0100 (CET): end of method
          MYMODULE Thu Nov 23 2017 09:43:16 GMT+0100 (CET): parsed an rss entry
          MYMODULE Thu Nov 23 2017 09:43:16 GMT+0100 (CET): parsed an rss entry
          MYMODULE Thu Nov 23 2017 09:43:16 GMT+0100 (CET): parsed an rss entry
          ...
          

          Now I will try to find out if the rss parsers have a finished callback and put the sendSocketNotifications in there…

          R 1 Reply Last reply Reply Quote 0
          • R Offline
            retroflex Project Sponsor Module Developer @retroflex
            last edited by

            To follow up… I got it to work by comparing parsed.feed.entries.length to the number of parsed entries, and when all entries were passed I called a callback that sent the notifications.

            I’m new to javascript so there’s still a lot to learn.

            1 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post
            Enjoying MagicMirror? Please consider a donation!
            MagicMirror created by Michael Teeuw.
            Forum managed by Sam, technical setup by Karsten.
            This forum is using NodeBB as its core | Contributors
            Contact | Privacy Policy