This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
130 lines (122 loc) · 3.84 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
var request = require('request')
const _ = require('underscore')
const async = require('async')
var CoinDeskAPI = function CoinDeskAPI () {}
var baseUrl = 'http://api.coindesk.com/v1/bpi/'
/**
* Returning a list of all currently "supported" (attention, querys will fail for some currencies
* from this list as an array of three letter strings.
* @param callback
*/
CoinDeskAPI.prototype.supportedCurrencies = function (callback) {
var url = baseUrl + 'supported-currencies.json'
request.get({uri: url}, function (err, response, body) {
if (err) {
callback(err, null)
}
if (body) {
var currencies = JSON.parse(body)
var currencyCodeArray = _.map(currencies, function (item) {
return item['currency']
})
callback(null, currencyCodeArray)
}
})
}
/**
* Calling the callback on an array of objects of the format: {time, rate} where rate is a float and time is in milliseconds.
* @param from
* @param to
* @param currency
* @param callback
*/
CoinDeskAPI.prototype.getPricesForSingleCurrency = function (from, to, currency, callback) {
if (from > to) {
callback(new Error('To date is before from date.'), [])
return
}
if (Date.parse(from) < Date.parse('2010-07-17')) {
// prevention of bad requests to coindesk. They only have data from '2010-07-17'.
from = '2010-07-17'
}
var url = 'http://api.coindesk.com/v1/bpi/historical/close.json?start=' + from + '&end=' + to + '¤cy=' + currency
request.get({uri: url}, function (err, response, body) {
if (err) {
callback(err, null)
}
if (body) {
var ratesValues = JSON.parse(body)
const exchangeRatesCurrency = ratesValues.bpi
var resultArray = _.map(exchangeRatesCurrency, function (key, item) {
return {time: (Date.parse(item) + 86400000), rate: key}
})
callback(null, resultArray)
}
})
}
CoinDeskAPI.prototype.getPricesForMultipleCurrencies = function (from, to, currencies, callback) {
var self = this
var ratesWithTimes = {}
var allRates = {}
async.each(currencies,
// 2nd param is the function that each item is passed to
function (currency, callback) {
// Call an asynchronous function, often a save() to DB
self.getPricesForSingleCurrency(from, to, currency, function (err, result) {
if (err) {
callback(err)
} else {
ratesWithTimes[currency] = result
callback()
}
})
},
// 3rd param is the function to call when everything's done
function (err) {
if (err) {
callback(err, null)
}
_.each(ratesWithTimes, function (rates, currency) {
rates.forEach(function (timeratepair) {
var newEntry = {}
newEntry[currency] = timeratepair.rate
if (allRates[timeratepair.time]) {
allRates[timeratepair.time].push(newEntry)
} else {
allRates[timeratepair.time] = [newEntry]
}
})
})
var allRatesNice = []
_.each(allRates, function (rates, time) {
var ratesNice = {}
_.each(rates, function (currencyratepair) {
_.each(currencyratepair, function (rate, currency) {
ratesNice[currency] = rate
})
})
allRatesNice.push({time: time, rates: ratesNice})
})
callback(null, allRatesNice)
}
)
}
/**
* Currenlty broken. Coindesk does not support all currency that are returned by the according query.
* @param from
* @param to
* @param currencies
* @param callback
*/
/*
CoinDeskAPI.prototype.getPricesForAllCurrencies = function(from, to, currencies, callback) {
var self = this
self.supportedCurrencies(function(err, currencies) {
if (err) {
callback(err, null)
}
self.getPricesForMultipleCurrencies(from, to, currencies, callback)
})
}
*/
module.exports = CoinDeskAPI