-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule.js
44 lines (41 loc) · 1.37 KB
/
schedule.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
/*
Schedule Job
Get Coin Price Every * minutes...
*/
const request = require('request');
const mongo = require('./mongo');
const config = require('./config.json');
const defaultConfig = config.dev;
module.exports.schtest = function() {
const current_time = new Date();
console.log(`Scheduler Job test at ${current_time}...`);
}
module.exports.updateCoinPrice = function(source, convert) {
console.log(`Catching ${source} to ${convert} price from CoinMarketCap...`)
const options = {
method: 'GET',
url: `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${source}&convert=${convert}`,
headers: {
Accepts: 'application/json',
'X-CMC_PRO_API_KEY': defaultConfig.cmc_key,
},
};
request(options, function(err, resp, body) {
// Error handler Verified
if (err) {
console.log(err);
return;
}
// console.log(resp);
// console.log(body);
console.log('Latest Price Catched...');
const resData = JSON.parse(body);
// Error handler Verified
try {
mongo.addLatest(source, convert, resData['data'][source]['quote'][convert]['price'],
'CoinMarketCap', resData['data'][source]['quote'][convert]['last_updated']);
} catch (err) {
console.log(err);
}
})
}