-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.go
138 lines (122 loc) · 3.69 KB
/
fetch.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
package main
import (
"fmt"
cmc "github.com/miguelmota/go-coinmarketcap"
"github.com/nlopes/slack"
"time"
)
// Fetch data for a particular ticker from CMC
// Avoid invoking this directly as this pings the endpoint directly.
func fetchCMCTicker(ticker string) (coinInfo Coin, err error) {
// Fetch coin data from CMC
cmcCoin, err := cmc.GetCoinData(ticker)
if err != nil {
return Coin{}, fmt.Errorf("coin data retrieval failed: %v", err)
} else {
return Coin{cmcCoin, time.Now()}, nil
}
}
// Ticker getter with caching wrapper (not less than 10 per minute)
func getTicker(ticker string) (coinInfo Coin, err error) {
t := time.Now()
// Do we even have the coin on record?
coinList, ok := []Coin{}, false
if coinList, ok = Storage.Coins[ticker]; ok {
// We have the coin, and it is not stale.
if !isTickerStale(Storage.LastUpdates[ticker], 6) {
coinInfo = coinList[len(coinList)-1] // Last element of the entries.
} else {
// Stale coin, update storage.
if coinInfo, err = fetchCMCTicker(ticker); err != nil {
fmt.Errorf("ticker failed to refresh: %v", err)
} else {
Storage.Coins[ticker] = append(Storage.Coins[ticker], coinInfo)
Storage.LastUpdates[ticker] = t
}
}
} else {
// No coin, need to fetch.
if coinInfo, err = fetchCMCTicker(ticker); err != nil {
Storage.Coins[ticker] = append(Storage.Coins[ticker], coinInfo)
}
}
return
}
// Check if the ticker is stale based on the passed timeout. Missing means stale.
func isTickerStale(lastUpdated time.Time, timeLimit int64) bool {
return time.Since(lastUpdated) > time.Duration(timeLimit)*time.Second
}
// Main loop to periodically download the status for all tickers.
func (s *SlackListener) fetchData(period int, coinTickers []string) {
// Fetch coin data from CMC for all tickers
ticker := time.NewTicker(time.Duration(period) * time.Second)
for _ = range ticker.C {
s.updateData(coinTickers)
}
}
func (s *SlackListener) updateData(tickers []string) {
t := time.Now()
coinList := make([]cmc.Coin, 3)
fmt.Printf("[%v] ", t.Format(time.Stamp))
for i, tck := range tickers {
coinInfo, err := cmc.GetCoinData(tck)
if err != nil {
fmt.Errorf("coin data retrieval failed: %v", err)
}
s.recordData(coinInfo)
coinList[i] = coinInfo
}
s.postUpdate(coinList)
fmt.Printf("\n")
}
func (s *SlackListener) recordData(coinInfo cmc.Coin) {
fmt.Printf("%v@%.2f ", coinInfo.Symbol, coinInfo.PriceUSD)
}
func (s *SlackListener) postUpdate(coinList []cmc.Coin) error {
t := time.Now()
attachment := slack.Attachment{
Text: fmt.Sprintf("[%v] hourly update", slackTime(t.Unix(), "{time}")),
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: coinList[0].ID,
Value: fmt.Sprintf("$%.2f", coinList[0].PriceUSD),
Short: true,
},
slack.AttachmentField{
Title: "24h Change",
Value: fmt.Sprintf("%.2f%%", coinList[0].PercentChange24H),
Short: true,
},
slack.AttachmentField{
Title: coinList[1].ID,
Value: fmt.Sprintf("$%.2f", coinList[1].PriceUSD),
Short: true,
},
slack.AttachmentField{
Title: "24h Change",
Value: fmt.Sprintf("%.2f%%", coinList[1].PercentChange24H),
Short: true,
},
slack.AttachmentField{
Title: coinList[2].ID,
Value: fmt.Sprintf("$%.2f", coinList[2].PriceUSD),
Short: true,
},
slack.AttachmentField{
Title: "24h Change",
Value: fmt.Sprintf("%.2f%%", coinList[2].PercentChange24H),
Short: true,
},
},
Color: areWeHappy(coinList[0].PercentChange1H),
}
params := slack.PostMessageParameters{
Attachments: []slack.Attachment{
attachment,
},
}
if _, _, err := s.client.PostMessage(s.channelID, "", params); err != nil {
return fmt.Errorf("failed to post message: %s", err)
}
return nil
}