-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
104 lines (89 loc) · 3.28 KB
/
app.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
try{
var axios = require("axios");
var mysql = require("mysql");
require('dotenv').config(); // Library to allow the importing of enviromental variables in .env files
}catch(error){
console.error("ERROR are all the dependencies installed?");
console.error("Try Running npm start");
console.log(error);
process.exit(1);
}
var refreshInterval = 1000 // 1000 ms = 1 second default: 1000*60*5 = 5 minutes
if(process.env.host == undefined || process.env.user == undefined || process.env.password == undefined){
console.error("Could not find all the required enviromental variables");
console.error("Did you create the .env file?");
process.exit(1);
}else{
var con = mysql.createConnection({
host: process.env.host,
user: process.env.user,
password: process.env.password,
database: process.env.database
});
var coinExchanges = []
con.connect(function(err){
if(err){
console.log('ERROR Connecting to DB');
console.log(err);
con.end();
process.exit(1);
};
console.log("Connected To Database");
var sql = "select * from V_coinexchanges";
var loopable = true;
// setInterval(function(){
con.query(sql, function(err,result){
if(err) throw err;
for (var row in result) {
var coinExchange = {
'symbol' : result[row].symbol,
'exchangeName' : result[row]['Exchange Name'],
'coin_id' : result[row].coin_id,
'exchange_id' : result[row].exchange_id
}
coinExchanges.push(coinExchange);
}
getPrices(coinExchanges);
})
// },refreshInterval);
})
}
function getPrices(coinExchanges){
var ecount = 0;
console.log("Found " + coinExchanges.length + " coinExchange pairs");
var completedCount = 0;
for (let i = 0;i<coinExchanges.length; i++) {
var url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=" + coinExchanges[i].symbol + "&tsyms=USD&e="+ coinExchanges[i].exchangeName;
// console.log(url);
axios.get(url)
.then(response => {
// console.log("Recieved Response");
// console.log(JSON.stringify(response.data));
for (var coin in response.data) {
if(coinExchanges[i].coin_id == undefined || response.data[coin].USD == undefined || coinExchanges[i].exchange_id == undefined){
console.log("Skipping insert got undefined value on coin_id "+ coinExchanges[i].coin_id + " USD " + response.data[coin].USD + " exchange id " + coinExchanges[i].exchange_id );
}else{
let sqlInsert = 'insert into price_feed (coin,price,exchange) values (' + coinExchanges[i].coin_id + ',' + response.data[coin].USD + ',' + coinExchanges[i].exchange_id + ');';
con.query(sqlInsert, function(err,result){
if(err) throw err;
console.log(sqlInsert);
if(completedCount == coinExchanges.length - 1){
console.log("Completed Insertions");
console.log("ecount " + ecount);
con.end();
process.exit(0);
}else{
completedCount++;
}
})
}
}
})
.catch(error => {
console.log("ERROR " + error);
ecount++;
completedCount++;
});
// console.log("Loop " + i);
}
}