//* comment *// Copyright (C) 2015 Sam Parkinson This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . //* globals *// var financeMashapeKey = '3Rfxc7fwp2mshJxgtDxKSueYna8Ap1qZfAcjsn2hjpuWPuBCrI'; //* block:currencyrate *// var currencyRate = new ProtoBlock('currencyrate'); currencyRate.palette = palettes.dict['external']; blocks.protoBlockDict['currencyrate'] = currencyRate; currencyRate.extraWidth = 20; currencyRate.staticLabels.push(_('currency'), _('from'), _('to')); currencyRate.adjustWidthToLabel(); currencyRate.twoArgMathBlock(); currencyRate.dockTypes[1] = 'anyin'; currencyRate.dockTypes[2] = 'anyin'; currencyRate.defaults.push('AUD', 'USD'); //* arg:currencyrate *// var block = logo.blocks.blockList[blk]; var conns = block.connections; var from = logo.parseArg(logo, turtle, conns[1]); var to = logo.parseArg(logo, turtle, conns[2]); var apiURL = 'https://currency-exchange.p.mashape.com/exchange?from=' + from + '&q=1.0&to=' + to; if (block.cacheFor === apiURL && block.cache !== undefined) { var response = block.cache; } else { try { var request = new XMLHttpRequest(); request.open('GET', apiURL, false); request.setRequestHeader('X-Mashape-Authorization', financeMashapeKey); request.send(null); var response = request.responseText; block.cacheFor = apiURL; block.cache = response; } catch (e) { logo.errorMsg('Could not connect to Currency API', blk); } } block.value = parseFloat(response); //* block:stockprice *// var stockprice = new ProtoBlock('stockprice'); stockprice.palette = palettes.dict['external']; blocks.protoBlockDict['stockprice'] = stockprice; stockprice.staticLabels.push(_('stock price')); stockprice.adjustWidthToLabel(); stockprice.oneArgMathBlock(); stockprice.dockTypes[1] = 'anyin'; stockprice.defaults.push('GOOG'); //* arg:stockprice *// var block = logo.blocks.blockList[blk]; var conns = block.connections; var stock = logo.parseArg(logo, turtle, conns[1]); var apiURL = 'https://cors-anywhere.herokuapp.com/dev.markitondemand.com/Api/v2/Quote/json?symbol=' + stock; if (block.cacheFor === apiURL && block.cache !== undefined) { var response = block.cache; } else { try { var request = new XMLHttpRequest(); request.open('GET', apiURL, false); request.send(null); var response = request.responseText; block.cacheFor = apiURL; block.cache = response; } catch (e) { logo.errorMsg('Could not connect to Markit On Demand API', blk); } } j = JSON.parse(response); if (j.Status !== 'SUCCESS') { if (j.Message.indexOf('No symbol matches') !== -1) { logo.errorMsg('No stock symbol %s found. Try another, eg. GOOG, AAPL'.replace('%s', stock), blk); } else { logo.errorMsg(j.Message, blk); } } else { block.value = j.LastPrice; } //* palette-icon:external *// //* palette-fill:external *// #59f //* palette-stroke:external *// #3771c8 //* palette-highlight:external *// #9DC4FF //* palette-stroke-highlight:external *// #000000