MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    1. Home
    2. bwente
    3. Best
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.
    B
    Offline
    • Profile
    • Following 0
    • Followers 1
    • Topics 2
    • Posts 9
    • Groups 0

    Posts

    Recent Best Controversial
    • RE: ReferenceError: primordials is not defined

      I tried to do a simple update that did not work.

      This fixed it. https://github.com/nodesource/distributions/blob/master/README.md

      I also lost electron somehow, once that was installed everything is back. Thank you.

      posted in Troubleshooting
      B
      bwente
    • Updating Compliments using a spreadsheet

      I just discovered MagicMirror last week and got things setup this weekend. It has been a blast trying out new modules. A big thank you to everyone in the community that has given their time.

      I am not a programmer and not much for command line. But I can follow instructions and copy and paste. I was looking for ways to update my mirror without having to touch terminal. I wanted an easy way to update my Compliments without making an JSON file by hand. I setup a new google sheets document.

      0_1502682132337_6dd2e42c-6148-44eb-907a-42fd5bace095-image.png

      Then published it.

      0_1502682216393_2dd17c70-16a4-4c55-b7f7-7f165b581226-image.png

      It turns out the JSON that google exports is not the JSON that Compliments needs. I tired my hand at jquery and html and got it to create the same format in the browser. But what is rendered in the browser window isn’t JSON either, it looks like it but it isn’t. I tried specifying the remotefile path as the html file that I made didn’t work.

      I then tried writing to the file system and javascript can’t do that in the browser either. Then I discovered that node.js can write a file. I converted my jquery JSON looper into a node file.

      0_1502683309962_mirror_snap.JPG

      I started this morning with my first javascript, and this afternoon my first node.js, now tonight I am trying to figure out how to best make it work. I have to manually run

      node remotefile.js
      

      config.js

      {
           module: 'compliments',
           position: 'lower_third',
           config: {
      	 remoteFile: 'remotefile.json'               
           }
      }
      

      And finally my noob code…

      'use strict';
      var request = require('request');
      
      var url = 'https://spreadsheets.google.com/feeds/list/YOUR_SECRET_SHEET_LINK_ID/od6/public/values?alt=json';
      var compliments = '';
      var anytime = '';
      var morning = '';
      var afternoon = '';
      var evening = '';
      
      request.get({
          url: url,
          json: true,
          headers: { 'User-Agent': 'request' }
      }, (err, res, data) => {
          if (err) {
              console.log('Error:', err);
          } else if (res.statusCode !== 200) {
              console.log('Status:', res.statusCode);
          } else {
              // data is already parsed as JSON:
              var entry = data.feed.entry;
      
              // anytime column
              anytime += '\t"anytime" : [\r\n';
              for (var i = 0; i < entry.length; i++) {
                  if (entry[i]['gsx$anytime']['$t']) {
                      anytime += '\t\t"' + entry[i]['gsx$anytime']['$t'] + '",\r\n';
                  }
              }
              anytime = anytime.replace(/,\s*$/, "") + '\r\n';
              anytime += '\t],\r\n';
      
              // morning column
              morning += '\t"morning" : [\r\n';
              for (var i = 0; i < entry.length; i++) {
                  if (entry[i]['gsx$morning']['$t']) {
                      morning += '\t\t"' + entry[i]['gsx$morning']['$t'] + '",\r\n';
                  }
              }
              morning = morning.replace(/,\s*$/, "") + '\r\n';
              morning += '\t],\r\n';
      
              // afternoon column
              afternoon += '\t"afternoon" : [\r\n';
              for (var i = 0; i < entry.length; i++) {
                  if (entry[i]['gsx$afternoon']['$t']) {
                      afternoon += '\t\t"' + entry[i]['gsx$afternoon']['$t'] + '",\r\n';
                  }
              }
              afternoon = afternoon.replace(/,\s*$/, "") + '\r\n';
              afternoon += '\t],\r\n';
      
              // evening column
              evening += '\t"evening" : [\r\n';
              for (var i = 0; i < entry.length; i++) {
                  if (entry[i]['gsx$evening']['$t']) {
                      evening += '\t\t"' + entry[i]['gsx$evening']['$t'] + '",\r\n';
                  }
              }
              evening = evening.replace(/,\s*$/, "") + '\r\n';
              evening += '\t]';
      
      
              compliments = '{\r\n' + anytime + morning + afternoon + evening + '\r\n}';
      
              var fs = require('fs');
              fs.writeFile("remotefile.json", compliments, function(err) {
                  if (err) {
                      return console.log(err);
                  }
      
                  console.log("The file was saved!");
              });
      
          }
      });
      

      I know the code could be a lot better. I also know I could use PHP to make it easier (just return JSON from the url). What would be the best MagicMirror way?

      posted in Development
      B
      bwente
    • 1 / 1