-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
281 lines (240 loc) · 14.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var d=new Date().toString();
var http = require('http');
var url = require('url');
var fs = require('fs');
var rest = require('restler');
var async = require('async');
var e=require('./email')
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' , err.stack);
sendemaillog('index.js uncaughtException', err.stack);
//process.exit(3);
});
rates={
dollar: 3.5 ,// ["USDILS=X",0,"1/1/2000","0:00pm"],
euro:["EURILS=X",0,"1/1/2000","0:00pm"],
bitsofgold: {"btc_spot":{"ILS":0,"EUR":0,"USD":0},"buy_btc":{"ILS":0,"USD":0,"EUR":0},"sell_btc":{"ILS":0,"USD":0,"EUR":0}},
bit2c: {"amount":0,"total":0,"av_price":0,"min_price":0,"max_price":0},
bitgo: {"currentSellingPrice":0,"currentBuyingPrice":0,"amountAvalibleForSale":"0","amountAvalibleForBuy":0},
bitcoinaverageUSD: { "24h_avg": 0, "ask": 0, "bid": 0, "last": 0, "timestamp": "Sun, 1 Jan 2000 00:00:00 -0000", "volume_btc": 0, "volume_percent": 0 },
bitcoinaverageEUR: { "24h_avg": 0, "ask": 0, "bid": 0, "last": 0, "timestamp": "Sun, 1 Jan 2000 00:00:00 -0000", "volume_btc": 0, "volume_percent": 0 },
bitstamp: {"high": "0", "last": "0", "timestamp": "0", "bid": "0", "vwap": "0", "volume": "0", "low": "0", "ask": "0"},
btce: {"ticker":{"high":0,"low":0,"avg":0,"vol":0,"vol_cur":0,"last":0,"buy":0,"sell":0,"updated":0,"server_time":0}},
bitpay: [{"code":"USD","name":"US Dollar","rate":0},{"code":"ILS","name":"Israeli Shekel","rate":0}]
}
function cbguard(cb,printerr){ //prevents a callback to be called twice
var cb1=cb;
return function() {
if(cb1) { var cb2=cb1; cb1=false; return cb2.apply(this,arguments); }
else if(printerr)console.log(new Error('cb called twice').stack);
}
}
var prev_ils=0,prev_usd=0;
update=function(cb)
{
async.parallel( //faster
//async.series(// slower
[
function(cb){cb=cbguard(cb);try{ rest.get("https://www.investing.com/common/modules/js_instrument_chart/api/data.php?pair_id=63&pair_id_for_news=63&chart_type=area&pair_interval=60&candle_count=120&events=yes&volume_series=yes&period=https://www.investing.com/common/modules/js_instrument_chart/api/data.php?pair_id=63&pair_id_for_news=63&chart_type=area&pair_interval=60&candle_count=120&events=yes&volume_series=yes&period=",{ rejectUnauthorized:false, headers:{ 'Accept-Encoding':'gzip, deflate', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36', 'Referer': 'https://www.investing.com/currencies/usd-ils', 'X-Requested-With': 'XMLHttpRequest' } }).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} data=JSON.parse(data); if(typeof data=='object' && data!==null) rates.dollar=data.candles[data.candles.length-1][1]; cb(); }); } catch(e){console.log(e.stack);cb();} }
// function(cb){cb=cbguard(cb);try{ rest.get('https://www.bloomberg.com/markets/watchlist/recent-tickers/data/USDILS:CUR').on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null)rates.dollar=data.tickers[0].last_price; cb(); }); } catch(e){console.log(e.stack);cb();} }
// function(cb){cb=cbguard(cb);try{ rest.get('http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=USDILS=X').on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} try{data=JSON.parse("["+data.trim().replace(/""/,"\\\"")+']');} catch(e){console.log(e.stack)} if(typeof data=='object' && data!==null)rates.dollar=data; cb(); }); } catch(e){console.log(e.stack);cb();} }
// ,function(cb){cb=cbguard(cb);try{ rest.get('http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=EURILS=X').on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} try{data=JSON.parse("["+data.trim().replace(/""/,"\\\"")+']');} catch(e){console.log(e.stack)} if(typeof data=='object' && data!==null)rates.euro=data; cb(); }); } catch(e){console.log(e.stack);cb();} }
,function(cb){cb=cbguard(cb);try{ rest.get("https://webapi.bitsofgold.co.il/v1/rates",{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} data=JSON.parse(data);if(typeof data=='object' && data!==null) rates.bitsofgold=data; cb(); }); } catch(e){console.log(e.stack);cb();} }
,function(cb){cb=cbguard(cb);try{ rest.get("https://bit2c.co.il/Exchanges/GetPrice?IsBid=true&total=0&pair=BtcNis&amount=1",{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null) rates.bit2c=data; cb(); }); } catch(e){console.log(e.stack);cb();} }
// ,function(cb){cb=cbguard(cb);try{ rest.get('https://www.bitgo.co.il/components/loadcontrol.aspx?cn=statspanel&json=true',{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null) rates.bitgo=data; cb(); }); } catch(e){console.log(e.stack);cb();} }
,function(cb){cb=cbguard(cb);try{ rest.get('https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD',{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null) rates.bitcoinaverageUSD=data; }); cb(); } catch(e){console.log(e.stack);cb();} }
// ,function(cb){cb=cbguard(cb);try{ rest.get('https://api.bitcoinaverage.com/ticker/global/EUR/',{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null) rates.bitcoinaverageEUR=data; }); cb(); } catch(e){console.log(e.stack);cb();} }
// ,function(cb){cb=cbguard(cb);try{ rest.get('https://www.bitstamp.net/api/ticker/',{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null) Object.keys(data).forEach(function(a){data[a]=parseFloat(data[a])}); rates.bitstamp=data; cb(); }); } catch(e){console.log(e.stack);cb();} }
// ,function(cb){cb=cbguard(cb);try{ rest.get('https://btc-e.com/api/2/btc_usd/ticker',{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} try{data=JSON.parse(data);} catch(e){console.log(e.stack)} if(typeof data=='object' && data!==null) rates.btce=data; }); cb(); } catch(e){console.log(e.stack);cb();} }
/* ,function(cb){cb=cbguard(cb);try{ rest.get('https://bitpay.com/api/rates',{rejectUnauthorized:false}).on('complete', function(data) { if(data instanceof Error){console.log(data.stack);return cb();} if(typeof data=='object' && data!==null)
{
var d=[];
if(data.length && data[prev_usd].code=='USD' && data[prev_ils].code=='ILS')
{
d=[data[prev_usd],data[prev_ils]];
}
else
{
for(var c,i=0;i<data.length;i++)
{
c=data[i];
if(c.code=='USD'){d.push(c);prev_usd=i}
if(c.code=='ILS'){d.push(c);prev_ils=i}
}
}
rates.bitpay=d;
}
cb(); }); } catch(e){console.log(e.stack);cb();} }
*/
],cb
)//parallel
}
var Canvas = require((require('os').arch()=='arm'?'./arm_node_modules/':(process.platform=='win32'?'./win_node_modules/':''))+'canvas')
, Font = Canvas.Font
, mcanvas = new Canvas(20,20)
, mctx = mcanvas.getContext('2d');
var GifEncoder = require('gif-encoder');
var fontarr=false;
function loadfonts(ctx)
{
if (!Font) { console.log('unable to load fonts from files. you will need to use windows font names'); return;}
if(fontarr===false){ fontarr=[];fs.readdirSync(__dirname+'/fonts').forEach(function(a){
var file= __dirname+'/fonts/'+a;
var namea=a.toLowerCase().split('.');
var name=namea[0]
if(namea[namea.length-1]=='ttf')
{
console.log(" add font '"+name+"'")
var font = new Font(name, file);
//font.addFace(fontFile('PfennigBold.ttf'), 'bold');
//font.addFace(fontFile('PfennigItalic.ttf'), 'normal', 'italic');
//font.addFace(fontFile('PfennigBoldItalic.ttf'), 'bold', 'italic');
fontarr.push(font);
}
else
{
console.log(" not add font '"+file+"'")
}
});}
if(ctx)
for(var i=0;i<fontarr.length;i++)
{
ctx.addFont(fontarr[i]);
}
}
loadfonts(mctx);
var fliphebrew=require('./fliphebrew.js')
var parseurl=require('url').parse
var rateshtml=fs.readFileSync(__dirname+'/rates.html')
http.createServer(function (req, res) {
if(req.url=='/rates'){ res.writeHead(200, {'Content-Type': 'text/javascript'}); res.end(JSON.stringify(rates, null, 2));}
if(req.url=='/') { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(rateshtml);}
if(req.url.substring(0,'/image'.length)=='/image') {
var purl=parseurl(req.url,true)
var text=fliphebrew(purl.query.text)||'bitcoin!';
mctx.antialias = 'none';
var font=purl.query.font||'14px Impact';
mctx.font = font;
var te = mctx.measureText(text);
te.height=
te.emHeightAscent//: 8,
+te.emHeightDescent//: 2,
-1;// for 16 size
te.top=
te.emHeightAscent //: 8
-te.actualBoundingBoxAscen//: 7
var canvas = new Canvas(te.width,te.height)
, ctx = canvas.getContext('2d');
loadfonts(ctx);
ctx.fillStyle="#FFFFFF";
ctx.fillRect(0,0,te.width,te.height);
ctx.antialias = 'none';
ctx.font = font;
ctx.fillStyle="#000000";
ctx.fillText(text, te.actualBoundingBoxLeft,te.emHeightAscent);
res.writeHead(200, {'Content-Type': 'image/gif'});
//var stream = canvas.createPNGStream();
// Create a 10 x 10 gif
var gif = new GifEncoder(canvas.width, canvas.height);
// using an rgba array of pixels [r, g, b, a, ... continues on for every pixel]
// This can be collected from a <canvas> via context.getImageData(0, 0, width, height).data
//var pixels = [0, 0, 0, 255/*, ...*/];
// Collect output
//var file = require('fs').createWriteStream('img.gif');
gif.pipe(res);
// Write out the image into memory
gif.writeHeader();
gif.addFrame(ctx.getImageData(0,0,canvas.width,canvas.height).data);
// gif.addFrame(pixels); // Write subsequent rgba arrays for more frames
gif.finish();
}
else { res.writeHead(404, {'Content-Type': 'text/plain'}); res.end(':-), since '+d+'\n');}
}).listen(3333);
console.log('Server running at http://127.0.0.1:3333/');
Number.prototype.formatMoney = function(c, d, t){
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
ratestext=function()
{
var p=3;
var text1='Bitcoin'
if(rates.bitcoinaverageUSD.ask) text1='mBTC/ILS '+((rates.bitcoinaverageUSD.ask*rates.dollar)/1000).formatMoney(p, '.', ',')//+' '
var p=3;
var text2=''
if(rates.bitsofgold.buy_btc.ILS) text2+= 'Bits of Gold '+(rates.bitsofgold.buy_btc.ILS/1000).formatMoney(p, '.', ',')+' '
if(rates.bit2c.av_price) text2+= 'Bit2c '+(rates.bit2c.av_price/1000).formatMoney(p, '.', ',')+' '
// if(rates.bitgo.currentSellingPrice) text2+= 'BitGo '+(rates.bitgo.currentSellingPrice/1000).formatMoney(p, '.', ',')+' '
// if(rates.bitcoinaverageUSD.ask) text2+= 'BitcoinAverage '+(rates.bitcoinaverageUSD.ask*rates.dollar/1000).formatMoney(p, '.', ',')+' '
// if(rates.bitstamp.ask) text2+= 'Bitstamp '+(rates.bitstamp.ask*rates.dollar[1]/1000).formatMoney(p, '.', ',')+' '
// if(rates.btce.ticker.sell) text2+= 'Btc-e '+(rates.btce.ticker.sell*rates.dollar[1]/1000).formatMoney(p, '.', ',')+' '
// if(rates.bitpay.filter(function(a){return a.code=='ILS'})[0].rate) text2+= 'Bitpay '+(rates.bitpay.filter(function(a){return a.code=='ILS'})[0].rate/1000).formatMoney(p, '.', ',')+' '
/*
var p=2;
var text2='Bits of Gold '+(rates.bitsofgold.sell/1000).formatMoney(p, '.', ',')+' '+
'Bit2c '+(rates.bit2c.ll/1000).formatMoney(p, '.', ',')+' '+
'Bit Go '+(rates.bitgo.currentSellingPrice/1000).formatMoney(p, '.', ',')+' '
*/
/*
var p=5;
var text2='Bits of Gold '+(rates.bitsofgold.sell/1000).formatMoney(p, '.', ',')+' '+
'Bit2c '+(rates.bit2c.ll/1000).formatMoney(p, '.', ',')+' '+
'BitGo '+(rates.bitgo.currentSellingPrice/1000).formatMoney(p, '.', ',')+' '+
'Bitcoin Average '+(rates.bitcoinaverageUSD.ask/1000).formatMoney(p, '.', ',')+' USD='+(rates.bitcoinaverageUSD.ask*rates.dollar/1000).formatMoney(p, '.', ',')+' ILS '+
'Bitcoin Average '+(rates.bitcoinaverageEUR.ask/1000).formatMoney(p, '.', ',')+' EUR='+(rates.bitcoinaverageEUR.ask*rates.euro[1]/1000).formatMoney(p, '.', ',')+' ILS '+
'Bitstamp '+(rates.bitstamp.ask/1000).formatMoney(p, '.', ',')+' USD='+(rates.bitstamp.ask*rates.dollar[1]/1000).formatMoney(p, '.', ',')+' ILS '+
'Btc-e '+(rates.btce.ticker.sell/1000).formatMoney(p, '.', ',')+' USD='+(rates.btce.ticker.sell*rates.dollar[1]/1000).formatMoney(p, '.', ',')+' ILS ' +
'Bitpay '+(rates.bitpay.filter(function(a){return a.code=='USD'})[0].rate/1000).formatMoney(p, '.', ',')+' USD='+(rates.bitpay.filter(function(a){return a.code=='ILS'})[0].rate/1000).formatMoney(p, '.', ',')+' ILS '
*/
var r={text1 :text1,text2:text2}
console.log(r)
return r;
}
//cp=null // dont use led display
cp=require('./cpower1200.js'); // use led display
var prevtext={text1:'',text2:''};
ledtext=function(options,cb)
{
if(!cp){
if(cb) return cb(); //if led display avalibale and callback avalible
return;
}
try{
function t1(cb)
{
if(options.text1&&prevtext.text1!=options.text1)
{
cp.serialwrite(cp.sendTextDataToASpecifiedWindow({window:0, text:options.text1, effect: cp.effect.indexOf('Draw') ,align:'center' }),cb);
prevtext.text1=options.text1
}
else cb();
}
function t2(cb)
{
if(options.text2&&prevtext.text2!=options.text2)
{
cp.serialwrite(cp.sendTextDataToASpecifiedWindow({window:1, text:options.text2 }));
prevtext.text2=options.text2
}
else cb();
}
t1(function(){
t2(function(){
//if(cp.serial)cp.serial.close();
if(cb)cb();
});
});
}catch(err){ if(err)console.log('err ', err.stack);}
}
var repl = require("repl");repl.start({ useGlobal:true, useColors:true, });// uncomment to test
function run()
{
update(function(){ledtext(ratestext());console.log('done');setTimeout(run,30000);});
}
run()