From 6a5688f24586ec386f23805e52f2aef67d8a1df6 Mon Sep 17 00:00:00 2001 From: Manu Masson Date: Sun, 9 Jul 2017 13:53:35 +0200 Subject: [PATCH] Push results to frontend via socket.io --- index.html | 37 ++++++++++++++++++++++ main.js | 89 +++++++++++++++++++++++++++++++--------------------- package.json | 11 ++++--- settings.js | 5 +-- 4 files changed, 99 insertions(+), 43 deletions(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 00000000..7c0ace39 --- /dev/null +++ b/index.html @@ -0,0 +1,37 @@ + + + + Socket.IO chat + + + + + + + + + + + + diff --git a/main.js b/main.js index bde9f359..fe5fbb17 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,25 @@ const request = require('request'), Promise = require("bluebird"); //request for pulling JSON from api. Bluebird for Promises. +const app = require('express')(), helmet = require('helmet'), http = require('http').Server(app), + io = require('socket.io')(http); // For websocket server functionality +app.use(helmet.hidePoweredBy({setTo: 'PHP/5.4.0'})); + +const port = 3000; + +app.get('/', function (req, res) { + res.sendFile(__dirname + '/index.html'); +}); + +io.on('connection', function (socket) { + socket.emit('news', results); +}); + +http.listen(port, function () { + console.log('listening on *:3000'); +}); + + require('./settings.js')(); //Includes settings file. // coin_prices is an object with data on price differences between markets. = {BTC : {market1 : 2000, market2: 4000, p : 2}, } (P for percentage difference) @@ -20,11 +39,12 @@ function getMarketData(options, coin_prices, callback) { //GET JSON DATA let data = JSON.parse(body); if (options.marketName) { + let newCoinPrices; newCoinPrices = options.last(data, coin_prices, options.toBTCURL); - - + numberOfRequests++; + if(numberOfRequests >=1) computePrices(coin_prices); resolve(newCoinPrices); } @@ -44,58 +64,57 @@ function getMarketData(options, coin_prices, callback) { //GET JSON DATA } function computePrices(data) { - for(let coin in data) { - - if(Object.keys(data[coin]).length > 1) { - let arr= []; - for( let market in data[coin]) { - // console.log(coin, market, data[coin][market]); - arr.push([market, data[coin][market]]); - } - arr.sort(function(a, b) { - return a[1] - b[1]; - }); + if(numberOfRequests >= 2) { + results = []; + for (let coin in data) { + + if (Object.keys(data[coin]).length > 1) { + let arr = []; + for (let market in data[coin]) { + arr.push([market, data[coin][market]]); + } + arr.sort(function (a, b) { + return a[1] - b[1]; + }); - let min = arr[0][1], max = arr[arr.length-1][1]; - // console.log(min, max); + let min = arr[0][1], max = arr[arr.length - 1][1]; - if(min == 0) min = arr[1][1]; - if(max == 0) max = arr[arr.length-2][1]; + if (min == 0) min = arr[1][1]; + if (max == 0) max = arr[arr.length - 2][1]; - if(min != 0 && max != 0) { - let marketPair = arr[arr.length-1][0] + "/" + arr[0][0]; + if (min != 0 && max != 0) { + let marketPair = arr[arr.length - 1][0] + "/" + arr[0][0]; - results.push([coin, max/min, marketPair]); + results.push([coin, max / min, marketPair]); + } } } + results.sort(function (a, b) { + return a[1] - b[1]; + }); + + io.emit('news', results); + console.log(results); } - results.sort(function(a, b) { - return a[1] - b[1]; - }); - console.log(results); } - - - - (async function main() { - // console.log("heloo"); let arrayOfRequests = []; - for(let i = 0; i <= markets.length; i++) { + for (let i = 0; i <= markets.length; i++) { arrayOfRequests.push(getMarketData(markets[i], coin_prices)); } await Promise.all(arrayOfRequests.map(p => p.catch(e => e))) .then(results => computePrices(coin_prices)) - // .then(results => console.log(coin_prices)) .catch(e => console.log(e)); -})() - .then(v => { - // console.log(v); - }); + setTimeout(main, 10000); +})(); + +// .then(v => { +// // console.log(v); +// }); diff --git a/package.json b/package.json index 598df17a..47726da6 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,26 @@ { "name": "arbittrex", - "version": "1.0.0", + "version": "1.0.3", "description": "An arbitrage bot", "main": "main.js", "dependencies": { "bluebird": "^3.5.0", - "request" : "^2.81.0" + "express": "^4.15.3", + "helmet": "^3.6.1", + "request": "^2.81.0", + "socket.io": "^2.0.3" }, "devDependencies": {}, "scripts": { "test": "node main", - "start" : "node main" + "start": "node main" }, "repository": { "type": "git", "url": "git+https://github.com/manu354/arbitrage.git" }, "keywords": [ - "Bittrex", + "bittrex", "arbitrage" ], "author": "Manu Masson", diff --git a/settings.js b/settings.js index 0748706b..04895114 100644 --- a/settings.js +++ b/settings.js @@ -23,8 +23,7 @@ // // } - -markets = [ +let markets = [ { marketName: 'bittrex', URL: 'https://bittrex.com/api/v1.1/public/getmarketsummaries', @@ -84,7 +83,6 @@ markets = [ last: function (data, coin_prices, toBTCURL) { //Where to find the last price of coin in JSON data return new Promise(function (res, rej) { - console.log("HHELLEEO!!"); let priceOfBTC = data.btc.last; console.log(priceOfBTC); try { @@ -94,7 +92,6 @@ markets = [ if (!coin_prices[coinName]) coin_prices[coinName] = {}; coin_prices[coinName]["jubi"] = data[key].last / priceOfBTC; - console.log(data[key].last, coinName); } res(coin_prices); }