-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (116 loc) · 3.29 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/Nonoelgringo/metego/openweather"
"github.com/gregdel/pushover"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
// Args
city := flag.String("city", "Paris", "Wanted city for the forecast")
days := flag.Int("days", 5, "Wanted number of days to forecast")
debug := flag.Bool("debug", false, "Set logging to debug level")
sendPushover := flag.Bool("pushover", false, "Send the forecast to pushover")
flag.Parse()
// Logging
atom := zap.NewAtomicLevel()
if *debug {
atom.SetLevel(zap.DebugLevel)
}
encoderCfg := zap.NewDevelopmentEncoderConfig()
encoderCfg.TimeKey = ""
logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atom,
))
defer logger.Sync()
sugar := logger.Sugar()
// Get tokens
tokens, err := getTokens("tokens.txt")
if err != nil {
sugar.Errorf("Error when getting tokens from file : %v\n", err)
os.Exit(1)
}
// Initialize openweather
openweatherApp, err := openweather.New((*tokens)["openweather"])
if err != nil {
sugar.Errorf("Error when creating an OpenWeather app : %v\n", err)
os.Exit(1)
}
// Creates a new CityForecast instance
cityForecast, err := openweather.NewCityForecast(*city, *days)
if err != nil {
sugar.Errorf("Error when creating a CityForecast : %v\n", err)
os.Exit(1)
}
forecastSlice, err := openweatherApp.Forecast(cityForecast)
if err != nil {
sugar.Errorf("Error when getting Forecast : %v\n", err)
os.Exit(1)
}
// Forecast title
forecastTitle := fmt.Sprintf("Weather forecast for %s (%d day(s))", *city, *days)
sugar.Infof(forecastTitle)
// Debug prints
sugar.Debugf("Forecast len='%v'", len(*forecastSlice))
// Print forecast
fmt.Println((*forecastSlice)[0])
for _, v := range (*forecastSlice)[1:] {
fmt.Println(v)
}
// Pushover
if *sendPushover {
sugar.Infof("Sending message to pushover")
// Initialize pushover and recipient
pushoverApp := pushover.New((*tokens)["pushover"])
recipient := pushover.NewRecipient((*tokens)["recipient"])
// Pushover message made of the first forecast entry + title
message := pushover.NewMessageWithTitle((*forecastSlice)[0], forecastTitle)
// Send message to recipient
_, err = pushoverApp.SendMessage(message, recipient)
if err != nil {
sugar.Errorf("Error when sending message to Pushover : %v\n", err)
os.Exit(1)
}
// Send the rest of the forecast (when days>1)
for _, v := range (*forecastSlice)[1:] {
message = pushover.NewMessage(v)
_, err = pushoverApp.SendMessage(message, recipient)
if err != nil {
sugar.Errorf("Error when sending message to Pushover : %v\n", err)
os.Exit(1)
}
}
}
}
// Returns a *map[string]string containing tokens from filename
func getTokens(filename string) (*map[string]string, error) {
//
tokensMap := make(map[string]string)
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
tokenName, token := sliceToStrings(strings.Split(scanner.Text(), " "))
tokensMap[tokenName] = token
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil, err
}
return &tokensMap, nil
}
func sliceToStrings(p []string) (string, string) {
return p[0], p[1]
}