• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

module that displays a certain text from a website, the text on the website is updated daily

Scheduled Pinned Locked Moved Requests
36 Posts 6 Posters 7.5k Views 6 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.
  • L Offline
    lolo
    last edited by Jan 24, 2021, 8:11 PM

    node_helper. But need to find different approach.

    const NodeHelper = require("node_helper");
    const cheerio = require("cheerio");
    const request = require('request');
    var self;
    module.exports = NodeHelper.create({
    
        start: function () {
            self = this;
            console.log("Starting node_helper for: " + this.name);
        },
    
        getData: function () {
            
    		const url = 'https://wol.jw.org/hr/wol/h/r19/lp-c';
            request({
                url: url,
                method: 'GET'
            }, (error, response, body) => {
                if (error) {
                    return console.error("ERROR: ",error)
                }
                if (!error && response.statusCode == 200) {
                var $ = cheerio.load(body);
                //console.log($.html());
                const data = $('div[class="tabContent"]').first();//   
    
                let header = $(data).find('h2').text();
                let title = $(data).find('p').first().text();
                let textData = $(data).find('div.pGroup>p').text();
                //console.log(header,title,text);
                var recivedData = {
                    header,
                    title,
                    textData
                }
    			//console.log('recivedData: ',recivedData);
                
    			self.sendSocketNotification('TEXT_RESULT', recivedData);
    			}
            });
        },
    
        socketNotificationReceived: function (notification, payload) {
         var self = this;
            if (notification === 'GET_TEXT_DATA') {
                //console.log(notification,payload);
    			self.getData();
            }
        },
    });
    
    

    MMM-Dnevni_citat.js. getDom need some styling.

    Module.register("MMM-Dnevni_citat", {
        defaults: {
            updateInterval: 10 * 60 * 1000, //  minute ,seconds ,milliseconds
            //retryDelay: 5000          // not needed for now
        },
        start: function () {
            Log.info("Starting module: " + this.name);
            requiresVersion: "2.1.0";
            this.loaded = false;
            this.scheduleUpdate();
    
        },
        scheduleUpdate: function () {
            setInterval(() => {
                this.getData();
            }, this.config.updateInterval);
            this.getData();
        },
        getData: function () {
            console.log(' GET_TEXT_DATA ', this.config)
            this.sendSocketNotification('GET_TEXT_DATA', this.config);
        },
        socketNotificationReceived: function (notification, payload) {
            if (notification === "TEXT_RESULT") {
                this.textDataRecived = payload;
                console.log(this.textDataRecived);
                this.loaded = true;
            }
            this.updateDom();
    
        },
    
        getDom: function () {
            var wrapper = document.createElement("div");
    
            if (!this.loaded) {
                wrapper.innerHTML = "LOADING";
                return wrapper;
            }
            if (this.loaded) {
                var header = document.createElement("header");
                header.innerHTML = this.textDataRecived.header;
                wrapper.appendChild(header);
    
                var table = document.createElement('tr');
                table.classList.add("xsmall", "normal");
                var title = document.createElement('tr');
                title.innerHTML = this.textDataRecived.title;
                table.appendChild(title);
                var textData = document.createElement('tr');
                textData.innerHTML = this.textDataRecived.textData;
    
                table.appendChild(textData);
                table.appendChild(title);
                wrapper.appendChild(table);
    
            }
            return wrapper;
        },
    });
    
    

    config.js

    		{    module: "MMM-Dnevni_citat",
                 position: "middle_center"
    		 },
    
    1 Reply Last reply Reply Quote 0
    • L Offline
      lolo @Amoniak
      last edited by Jan 25, 2021, 11:25 AM

      @Amoniak
      This should fix the problem with loading text for current Date.
      node_helper:

      const NodeHelper = require("node_helper");
      const cheerio = require("cheerio");
      const request = require('request');
      var self;
      const ur = 'https://wol.jw.org/wol/dt/r19/lp-c';
      
      module.exports = NodeHelper.create({
      
          start: function () {
              self = this;
              self.browser
              console.log("Starting node_helper for: " + this.name);
          },
      
          getData: function () {
              var self = this;
              var url = self.setURL();
              request({
                  url: url,
                  method: 'GET'
              }, (error, response, body) => {
                  if (!error) { 
                      var result = JSON.parse(body);
                      var contentData = result.items[0].content;
                      var $ = cheerio.load(contentData);
                      //console.log("cheerio:  ", $.html());
                      var header = $('html>body>header>h2').text();
                      var title = $('p').first().text();
                      var textData = $('p').first().next().text().replace(/(\r\n|\n|\r)/gm, "");
                      var recivedData = {
                          header,
                          title,
                          textData
                      };
                      //console.log(recivedData);    
      				self.sendSocketNotification('TEXT_RESULT', recivedData);
                  }
                  if (error) { 
                      console.log(error);
                  }
              });
      
          },
      
          setURL: function () {
              console.log("ur old: ", ur);
              var utc = new Date().toJSON().slice(0, 10).replace(/-/g, '/');
              var newURL = ur.concat("/").concat(utc);
              return newURL
          },
      
      
          socketNotificationReceived: function (notification, payload) {
              var self = this;
              if (notification === 'GET_TEXT_DATA') {
                  self.getData();
              }
          },
      });
      
      A 1 Reply Last reply Jan 25, 2021, 11:28 AM Reply Quote 0
      • A Offline
        Amoniak @lolo
        last edited by Jan 25, 2021, 11:28 AM

        @lolo
        thank you, I’ll try right now

        1 Reply Last reply Reply Quote 0
        • A Offline
          Amoniak
          last edited by Amoniak Jan 25, 2021, 11:54 AM Jan 25, 2021, 11:52 AM

          dnevni_citat.jpg

          @lolo there it is.
          many, many, many thanks. and thanks for adding explanations

          L 1 Reply Last reply Jan 25, 2021, 12:42 PM Reply Quote 0
          • L Offline
            lolo @Amoniak
            last edited by Jan 25, 2021, 12:42 PM

            @Amoniak you’re welcome.
            Just delete the second appendChild in MMM-Dnevni_citat.js

                        var table = document.createElement('tr');
                        table.classList.add("xsmall", "normal");
                        var title = document.createElement('tr');
                        title.innerHTML = this.textDataRecived.title;
                        table.appendChild(title);
                        var textData = document.createElement('tr');
                        textData.innerHTML = this.textDataRecived.textData;
            
                        table.appendChild(textData);
                        table.appendChild(title);        // delete this one
                        wrapper.appendChild(table);
            
            

            I added twice,my mistake.

            1 Reply Last reply Reply Quote 0
            • E Offline
              e199504 @Amoniak
              last edited by May 13, 2021, 11:10 AM

              @amoniak how did you fix the black screen? I am getting the same error

              S 1 Reply Last reply May 13, 2021, 5:15 PM Reply Quote 0
              • S Offline
                sdetweil @e199504
                last edited by May 13, 2021, 5:15 PM

                @e199504 is this after install?

                did u do the npm install in the module folder?

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                E 1 Reply Last reply May 14, 2021, 10:17 AM Reply Quote 0
                • E Offline
                  e199504 @sdetweil
                  last edited by May 14, 2021, 10:17 AM

                  @sdetweil in the reeadme file it did not say to go inside the module and use npm install but once I did it started working.
                  thank you

                  S 1 Reply Last reply May 14, 2021, 11:37 AM Reply Quote 0
                  • S Offline
                    sdetweil @e199504
                    last edited by May 14, 2021, 11:37 AM

                    @e199504 yes. it seems many authors forget to write that step.

                    general rule. look for a package.json file.
                    if present, run the npm install

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 1
                    • 1
                    • 2
                    • 3
                    • 4
                    • 4 / 4
                    • 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