-
Notifications
You must be signed in to change notification settings - Fork 126
/
CoingeckoScraper.go
173 lines (143 loc) · 4.39 KB
/
CoingeckoScraper.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package foreignscrapers
// Please implement the scraping of coingecko quotations here.
import (
"encoding/json"
"errors"
"strconv"
"strings"
"time"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/utils"
log "github.com/sirupsen/logrus"
)
const (
source = "Coingecko"
)
var (
coingeckoUpdateSeconds int64
)
type CoingeckoCoin struct {
ID string `json:"id"`
Symbol string `json:"symbol"`
Name string `json:"name"`
CurrentPrice float64 `json:"current_price"`
Yesterday24hVolume float64 `json:"total_volume"`
LastUpdated string `json:"last_updated"`
}
type nothing struct{}
type CoingeckoScraper struct {
ticker *time.Ticker
foreignScrapper ForeignScraper
apiKey string
apiSecret string
}
func init() {
var err error
coingeckoUpdateSeconds, err = strconv.ParseInt(utils.Getenv("COINGECKO_UPDATE_SECONDS", "14400"), 10, 64)
if err != nil {
log.Error("Parse COINGECKO_UPDATE_SECONDS: ", err)
}
}
func NewCoingeckoScraper(datastore models.Datastore, apiKey string, apiSecret string) *CoingeckoScraper {
foreignScrapper := ForeignScraper{
shutdown: make(chan nothing),
error: nil,
datastore: datastore,
chanQuotation: make(chan *models.ForeignQuotation),
}
s := &CoingeckoScraper{
ticker: time.NewTicker(time.Duration(coingeckoUpdateSeconds) * time.Second),
foreignScrapper: foreignScrapper,
apiKey: apiKey,
apiSecret: apiSecret,
}
go s.mainLoop()
return s
}
// mainLoop runs in a goroutine until channel s is closed.
func (scraper *CoingeckoScraper) mainLoop() {
// Initial run.
err := scraper.UpdateQuotation()
if err != nil {
log.Error(err)
}
// Update every @coingeckoUpdateSeconds.
for {
select {
case <-scraper.ticker.C:
err := scraper.UpdateQuotation()
if err != nil {
log.Error(err)
}
case <-scraper.foreignScrapper.shutdown: // user requested shutdown
log.Printf("Coingeckoscraper shutting down")
return
}
}
}
// Update retrieves new coin information from the coingecko API and stores it to influx
func (scraper *CoingeckoScraper) UpdateQuotation() error {
log.Printf("Executing CoingeckoScraper update")
coins, err := scraper.getCoingeckoData()
if err != nil {
log.Errorln("Error getting data from coingecko", err)
return err
}
for _, coin := range coins {
// TO DO: normalize symbol instead of all upper
coin.Symbol = strings.ToUpper(coin.Symbol)
// Parse last updated timestamp
layout := "2006-01-02T15:04:05.000Z"
timestamp, err := time.Parse(layout, coin.LastUpdated)
if err != nil {
log.Errorln("error parsing time")
}
// Get yesterday's price from influx if available
priceYesterday, err := scraper.foreignScrapper.datastore.GetForeignPriceYesterday(coin.Symbol, source)
if err != nil {
priceYesterday = 0
}
foreignQuotation := models.ForeignQuotation{
Symbol: coin.Symbol,
Name: coin.Name,
Price: coin.CurrentPrice,
PriceYesterday: priceYesterday,
VolumeYesterdayUSD: coin.Yesterday24hVolume,
Source: source,
Time: timestamp,
}
scraper.foreignScrapper.chanQuotation <- &foreignQuotation
}
return nil
}
func (scraper *CoingeckoScraper) GetQuoteChannel() chan *models.ForeignQuotation {
return scraper.foreignScrapper.chanQuotation
}
func (scraper *CoingeckoScraper) getCoingeckoData() (coins []CoingeckoCoin, err error) {
var response []byte
if scraper.apiKey != "" {
baseString := "https://pro-api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=false&x_cg_pro_api_key=" + scraper.apiKey
response, _, err = utils.GetRequest(baseString)
} else {
response, _, err = utils.GetRequest("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=false")
}
if err != nil {
return
}
err = json.Unmarshal(response, &coins)
if err != nil {
return
}
return
}
// Close closes any existing API connections
func (scraper *CoingeckoScraper) Close() error {
if scraper.foreignScrapper.closed {
return errors.New("scraper already closed")
}
close(scraper.foreignScrapper.shutdown)
<-scraper.foreignScrapper.shutdownDone
scraper.foreignScrapper.errorLock.RLock()
defer scraper.foreignScrapper.errorLock.RUnlock()
return scraper.foreignScrapper.error
}