-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.go
118 lines (97 loc) · 2.58 KB
/
weather.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
package main
import (
"fmt"
owm "github.com/briandowns/openweathermap"
"github.com/gtuk/discordwebhook"
"github.com/joho/godotenv"
"log"
"os"
"strconv"
"time"
"unicode"
)
func getWeather(weather chan []discordwebhook.Field) {
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
apiKey := os.Getenv("OWM_KEY")
forecast, err := owm.NewOneCall("C", "EN", apiKey, []string{owm.ExcludeHourly, owm.ExcludeCurrent})
if err != nil {
log.Fatal("Error get api data:", err)
}
long, err := strconv.ParseFloat(os.Getenv("LONGITUDE"), 64)
if err != nil {
log.Fatal("Error converting longitude:", err)
}
lat, err := strconv.ParseFloat(os.Getenv("LATITUDE"), 64)
if err != nil {
log.Fatal("Error converting latitude:", err)
}
location := os.Getenv("LOCATION")
coord := &owm.Coordinates{
Longitude: long,
Latitude: lat,
}
err = forecast.OneCallByCoordinates(coord)
if err != nil {
log.Fatal(err)
}
weather <- []discordwebhook.Field{
parseWeather(forecast.Daily[0], forecast.Alerts, location),
parseWeather(forecast.Daily[1], forecast.Alerts, location),
}
}
func parseWeather(forecast owm.OneCallDailyData, alerts []owm.OneCallAlertData, location string) discordwebhook.Field {
weatherType := forecast.Weather[0].Main
weatherEmoji := ""
switch weatherType {
case "Clear":
weatherEmoji = "☀️"
break
case "Clouds":
weatherEmoji = "☁️"
break
case "Mist":
weatherEmoji = "🌁"
break
case "Snow":
weatherEmoji = "❄️"
break
case "Rain":
weatherEmoji = "🌧️"
break
case "Drizzle":
weatherEmoji = "🌧️"
break
case "Thunderstorm":
weatherEmoji = "⛈️"
break
}
weatherDescription := []rune(forecast.Weather[0].Description)
weatherDescription[0] = unicode.ToUpper(weatherDescription[0])
title := weatherEmoji + " " + string(weatherDescription)
content := "\n🌡️ " + fmt.Sprintf("%.2f", forecast.Temp.Day) + "°C"
content += "\n🔺 " + fmt.Sprintf("%.2f", forecast.Temp.Max) + "°C"
content += "\n🔻 " + fmt.Sprintf("%.2f", forecast.Temp.Min) + "°C"
if len(alerts) > 0 {
location, err := time.LoadLocation(location)
if err != nil {
log.Fatalf("Error loading time")
}
for _, alert := range alerts {
content += "\n🚨 " + alert.Event
content += " from " + time.Unix(int64(alert.Start), 0).In(location).Format("15:04")
content += " to " + time.Unix(int64(alert.End), 0).In(location).Format("15:04")
content += " !"
}
}
inline := true
field := discordwebhook.Field{
Name: &title,
Value: &content,
Inline: &inline,
}
return field
}