-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.68 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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/deifyed/stock-notifier/pkg/config"
"github.com/deifyed/stock-notifier/pkg/core"
"github.com/deifyed/stock-notifier/pkg/notification"
"github.com/deifyed/stock-notifier/pkg/notification/gotify"
"github.com/deifyed/stock-notifier/pkg/stock"
"github.com/deifyed/stock-notifier/pkg/stock/yahoo"
)
func main() {
cfg := config.LoadConfig(os.Getenv)
if err := cfg.Validate(); err != nil {
log.Fatalln(err)
}
stockData, err := yahoo.NewYahooClient().FetchCurrentPrice(cfg.Symbols...)
if err != nil {
log.Fatalln(fmt.Errorf("fetching quotes: %w", err))
}
targetsGetter := generateTargetsGetter(cfg.PriceTargets)
currentPriceGetter := generateCurrentPriceGetter(stockData)
stockFactory := core.NewFactory(targetsGetter, currentPriceGetter)
notificationClient := gotify.NewGotifyClient(cfg.PushServerURL)
for _, symbol := range cfg.Symbols {
currentStock := stockFactory.New(symbol)
if currentStock.TestPriceTargets() {
err := notificationClient.Notify(notification.Message{
Title: "Price target",
Message: fmt.Sprintf("%s hit %f", symbol, currentStock.CurrentPrice),
Priority: 5,
})
if err != nil {
log.Fatalln(fmt.Errorf("sending notification: %w", err))
}
}
}
}
func generateTargetsGetter(rawPriceTargets map[string][]string) func(string) []string {
return func(symbol string) []string {
return rawPriceTargets[strings.ToUpper(symbol)]
}
}
func generateCurrentPriceGetter(datas []stock.Data) func(string) float64 {
return func(symbol string) float64 {
for _, item := range datas {
if strings.EqualFold(item.Symbol, symbol) {
return item.CurrentPrice
}
}
return -1
}
}