-
Notifications
You must be signed in to change notification settings - Fork 2
/
coindesk.go
155 lines (124 loc) · 3.17 KB
/
coindesk.go
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
package coindesk
import (
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type Price struct {
Date time.Time
Price float64
}
// GetPrice fetches the latest Bitcoin Price from coindesk API in given currency.
// s is the ISO code of the currency. Defaults to USD.
// Supported Currencies - https://api.coindesk.com/v1/bpi/supported-currencies.json
func GetPrice(s ...string) float64 {
curr := "USD"
if len(s) > 0 {
curr = strings.ToUpper(s[0])
}
url := fmt.Sprintf("https://api.coindesk.com/v1/bpi/currentPrice/%s.json", curr)
response, err := http.Get(url)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
body := string(responseData)
data := fmt.Sprintf("bpi.%s.rate_float", curr)
Price := gjson.Get(body, data).String()
output, err := strconv.ParseFloat(Price, 64)
if err != nil {
log.Fatal(err)
}
return output
}
// HistoryPrice takes in startDate and endDate as input
// It returns an array of Price struct
func HistoryPrice(startDate string, endDate string) []Price {
url := "https://api.coindesk.com/v1/bpi/historical/close.json?start="+startDate+"&end="+endDate
var history []Price //Array of struct containing Prices from startDate to endDate
response, err := http.Get(url)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
body := string(responseData)
var m map[string]gjson.Result
m = gjson.Get(body, "bpi").Map()
layout := "2006-01-02"
for t, p := range(m) {
Date, err := time.Parse(layout, t)
if err != nil {
fmt.Println(err)
}
history = append(history, Price{Date, p.Num})
}
return history
}
// CurrentPrice fetches current Price of Bitcoin from coindesk API.
// It returns the Price in USD, GBP and EUR.
func CurrentPrice() (float64, float64, float64) {
url := "https://api.coindesk.com/v1/bpi/currentPrice.json"
response, err := http.Get(url)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
body := string(responseData)
priceUsd := gjson.Get(body, "bpi.USD.rate_float").String()
USD, err := strconv.ParseFloat(priceUsd, 64)
if err != nil {
log.Fatal(err)
}
priceGbp := gjson.Get(body, "bpi.GBP.rate_float").String()
GBP, err := strconv.ParseFloat(priceGbp, 64)
if err != nil {
log.Fatal(err)
}
priceEur := gjson.Get(body, "bpi.EUR.rate_float").String()
EUR, err := strconv.ParseFloat(priceEur, 64)
if err != nil {
log.Fatal(err)
}
return USD, GBP, EUR
}
// Yesterday returns yesterday's Price
func Yesterday() float64{
url := "https://api.coindesk.com/v1/bpi/historical/close.json?for=yesterday"
response, err := http.Get(url)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
body := string(responseData)
var m map[string]gjson.Result
m = gjson.Get(body, "bpi").Map()
if err != nil {
log.Fatal(err)
}
for _, p := range(m) {
return p.Num //Returns the yesterdays Price
}
return 0
}