MagicMirror² v2.4.1 is available! For more information about this release, check out this topic.
Please read the release notes carefully since this update requires adjustments to your Raspberry Pi configuration!

Stocks



  • Here’s a basic scrolling stocks ticker module. You can get it from here:

    https://github.com/alexyak/stocks

    This is how it looks:

    0_1471133603462_Screen Shot 2016-08-13 at 8.09.32 PM.png



  • This post is deleted!


  • Is it possible to check currency rates using this module?



  • Great module! I’m currently modifying it for some visual changes as well as trying to get it to pull yahoo finance data instead of google. The reason for that is that Yahoo also lists commodities (crude oil, corn, etc).

    I’ve gotten what I think is most of it done, but I’m getting an error on the node_helper.js module. I’m relatively new to node, but can someone explain why if I change the URL in the code to a yahoo URL that also spits back a JSON file, that it gives me an “unexpected token u in JSON at position 0”?

    For reference, the yahoo URL is: https://query.yahooapis.com/v1/public/yql?q=select * from csv where url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3DMSFT,AAPL%26f%3Dsl1d1t1c1ohgv%26e%3D.csv' and columns%3D'symbol%2Cprice%2Cdate%2Ctime%2Cchange%2Ccol1%2Chigh%2Clow%2Ccol2'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

    Thanks!



  • @ZTA0796 The original code does a hardcoded substring on the result:

    var result = JSON.parse(body.substring(3, body.length));

    You probably don’t need to do that.



  • This post is deleted!


  • @alexyak Thanks! I have one more question about syntax that I’m having trouble with.

    I can pull the Yahoo JSON into the node_helper file and print it to the terminal without a problem. But when I try to reference it in the “getDom” function, I get nothing. I haven’t changed the code at the start of that function at all. But when it runs “this.result.length”, the length returned is 0.

    I’m not super-familiar with the “this” syntax, but was hoping you or someone else might have an idea of why the JSON you pass would work, but not the JSON that I pass?

    Thanks!



  • great! would also be good for rss feeds 🙂



  • @ZTA0796 Could you show your code?



  • @alexyak Sure…see below.

    In the node_helper.js:

    getStocks: function (url) {
    var self = this;

      request({ url: url, method: 'GET' }, function (error, response, body) {
          if (!error && response.statusCode == 200) {
            
              var result = JSON.parse(body);
              console.log(result.query.results.row);  //prints the JSON as expected to the terminal
              self.sendSocketNotification('STOCKS_RESULT', result);
          }
      });
    

    },

    //Subclass socketNotificationReceived received.
    socketNotificationReceived: function(notification, payload) {
    if (notification === ‘GET_STOCKS’) {
    this.getStocks(payload);
    }
    }

    });

    in stocks.js:

    getDom: function() {
    
        var wrapper = document.createElement("table");
        wrapper.className = 'normal regular small';
        
        var count = 0;
    
        var _this = this;
    
    	console.log("in the DOM function");
    	console.log("result is " + this);  //prints [object Object]
    	console.log("result is " + _this); //prints [object Object]
    	console.log("result length is " + this.result.length); //prints 0
    
        if (this.result.length > 0){
            this.result.forEach(function(stock) {
    			//since length=0, it never gets in this loop
                               //
    			//
    },
    
    scheduleUpdate: function(delay) {
    	console.log("scheduled update check");
    	var nextLoad = this.config.updateInterval;
    	if (typeof delay !== "undefined" && delay >= 0) {
    		nextLoad = delay;
    	}
    
    	var self = this;
    	setInterval(function() {
    		self.getStocks();
    	}, nextLoad);
    },
    
    roundValue: function(value) {
       return Math.round(value*100)/100; 
    },
    
    getStocks: function () {
        var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%27http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3D' + this.config.stocks + '%26f%3Dsl1d1t1c1ohgv%26e%3D.csv%27%20and%20columns%3D%27symbol%2Cprice%2Cdate%2Ctime%2Cchange%2Ccol1%2Chigh%2Clow%2Ccol2%27&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
        this.sendSocketNotification('GET_STOCKS', url);
    },
    
    
    socketNotificationReceived: function(notification, payload) {
        if (notification === "STOCKS_RESULT") {
    		console.log("notification received");
    		console.log(payload);  //prints Object { query: Object }
            this.result = payload;
            this.updateDom(self.config.fadeSpeed);
        }    
    },