-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.52 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
package main
import (
"github.com/deckarep/gosx-notifier"
coinmarketcapApi "github.com/miguelmota/go-coinmarketcap"
"strings"
"time"
"log"
)
//List of coins check
var coins []string = []string{
"stellar",
"ripple",
"cardano",
"siacoin",
"ubiq",
"electra",
"myriad"}
func main() {
resultCh := make(chan string)
errorCh := make(chan error)
for _, s := range coins {
go checkCoin(resultCh, errorCh, s)
}
for {
select {
case result := <-resultCh:
if strings.HasPrefix(result, "n") {
s := strings.Split(strings.Trim(result, "n"), ",")
sendNotif(s[0], "Change is " + s[1])
}
case error:= <- errorCh:
log.Println(error);
}
}
}
func sendNotif(title string, message string) {
note := gosxnotifier.NewNotification(message)
note.Title = "Crypto: " + title
note.Sound = gosxnotifier.Blow
note.Push()
}
func checkCoin(resultCh chan string, errorCh chan error, coin string) {
var lastChangeNotif float64 = 0
for {
for {
coinInfo, err := coinmarketcapApi.GetCoinData(coin)
if err != nil {
errorCh <- err
} else {
if ((lastChangeNotif == 0 || lastChangeNotif < coinInfo.PercentChange1h) && coinInfo.PercentChange1h > 1) {
resultCh <- "n " + coin + "," + "UP"
lastChangeNotif = coinInfo.PercentChange1h
} else if ((lastChangeNotif == 0 || lastChangeNotif > coinInfo.PercentChange1h) && coinInfo.PercentChange1h < -1) {
resultCh <- "n " + coin + "," + "DOWN"
lastChangeNotif = coinInfo.PercentChange1h
}
}
time.Sleep(20 * time.Second)
}
}
}