/
main.go
124 lines (103 loc) · 2.26 KB
/
main.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
package main
import (
"fmt"
"github.com/getlantern/systray"
"net/http"
"encoding/json"
"log"
"time"
)
type Crypto struct {
Id string `json:"id"`
PriceBtc float64 `json:"price_btc"`
PriceUsd float64 `json:"price_usd"`
PriceEur float64 `json:"price_eur"`
}
func main() {
systray.Run(onReady, onExit)
}
func updateTicker(coin string) Crypto {
url := fmt.Sprintf(fmt.Sprintf("http://coincap.io/page/%s", coin))
// Build request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
}
// Create a client
client := &http.Client{}
// Send request
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
// Close
defer resp.Body.Close()
var crypto Crypto
// Decode
if err := json.NewDecoder(resp.Body).Decode(&crypto); err != nil {
log.Println(err)
}
return crypto
}
func setText(crypto Crypto) {
systray.SetTitle(fmt.Sprintf("$%.2f", crypto.PriceUsd))
systray.SetTooltip(fmt.Sprintf("%s is currently at $%.2f", crypto.Id, crypto.PriceUsd))
}
func onReady() {
// Setup menu items
menuBtc := systray.AddMenuItem("BTC", "Bitcoin")
menuEth := systray.AddMenuItem("ETH", "Ethereum")
menuLtc := systray.AddMenuItem("LTC", "Litecoin")
menuNeo := systray.AddMenuItem("NEO", "NEO")
systray.AddSeparator()
menuQuit := systray.AddMenuItem("Quit", "Quit application")
// Set btc as default
var coin string = "BTC"
menuBtc.Check()
go func() {
for {
setText(updateTicker(coin))
time.Sleep(1 * time.Second)
}
}()
go func() {
for {
select {
case <-menuBtc.ClickedCh:
menuBtc.Check()
menuEth.Uncheck()
menuLtc.Uncheck()
menuNeo.Uncheck()
coin = "BTC"
setText(updateTicker(coin))
case <-menuEth.ClickedCh:
menuEth.Check()
menuBtc.Uncheck()
menuLtc.Uncheck()
menuNeo.Uncheck()
coin = "ETH"
setText(updateTicker(coin))
case <-menuLtc.ClickedCh:
menuLtc.Check()
menuBtc.Uncheck()
menuEth.Uncheck()
menuNeo.Uncheck()
coin = "LTC"
setText(updateTicker(coin))
case <-menuNeo.ClickedCh:
menuNeo.Check()
menuBtc.Uncheck()
menuEth.Uncheck()
menuLtc.Uncheck()
coin = "NEO"
setText(updateTicker(coin))
case <-menuQuit.ClickedCh:
systray.Quit()
return
}
}
}()
}
func onExit() {
// Cleaning stuff here
}