-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.go
133 lines (112 loc) · 2.89 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/gtuk/discordwebhook"
"github.com/hectormalot/omgo"
"github.com/joho/godotenv"
"os"
"strconv"
)
func getWeather(weather chan []discordwebhook.Field, e chan error) {
// load .env file
err := godotenv.Load(".env")
if err != nil {
e <- fmt.Errorf("Error loading .env file: %v", err)
weather <- nil
return
}
long, err := strconv.ParseFloat(os.Getenv("LONGITUDE"), 64)
if err != nil {
e <- fmt.Errorf("Error converting longitude: %v", err)
weather <- nil
return
}
lat, err := strconv.ParseFloat(os.Getenv("LATITUDE"), 64)
if err != nil {
e <- fmt.Errorf("Error converting latitude: %v", err)
weather <- nil
return
}
weatherDescriptions := map[int]string{}
// Read the JSON file
file, err := os.ReadFile("weather_description.json")
if err != nil {
e <- fmt.Errorf("Error reading JSON file: %v", err)
weather <- nil
return
}
// Unmarshal the JSON data into the map
err = json.Unmarshal(file, &weatherDescriptions)
if err != nil {
e <- fmt.Errorf("Error unmarshalling JSON data: %v", err)
weather <- nil
return
}
c, err := omgo.NewClient()
if err != nil {
e <- fmt.Errorf("Error creating OpenMeteo client: %v", err)
weather <- nil
return
}
loc, err := omgo.NewLocation(lat, long)
if err != nil {
e <- fmt.Errorf("Error creating location: %v", err)
weather <- nil
return
}
opts := omgo.Options{
DailyMetrics: []string{"temperature_2m_max", "temperature_2m_min", "weathercode"},
}
res, err := c.Forecast(context.Background(), loc, &opts)
if err != nil {
e <- fmt.Errorf("Error getting forecast: %v", err)
weather <- nil
return
}
weather <- []discordwebhook.Field{
parseWeather(res, 0, weatherDescriptions),
parseWeather(res, 1, weatherDescriptions),
}
e <- nil
}
func parseWeather(forecast *omgo.Forecast, day int, weatherDesc map[int]string) discordwebhook.Field {
weatherType := int(forecast.DailyMetrics["weathercode"][day])
weatherEmoji := ""
switch weatherType {
case 0, 1:
weatherEmoji = "☀️"
break
case 2, 3:
weatherEmoji = "☁️"
break
case 45, 48:
weatherEmoji = "🌁"
break
case 51, 53, 55, 56, 57, 61, 63, 65, 66, 67:
weatherEmoji = "🌧️"
break
case 71, 73, 75, 77, 85, 86:
weatherEmoji = "❄️"
break
case 98, 96, 99:
weatherEmoji = "⛈️"
break
}
weatherDescription := weatherDesc[weatherType]
title := weatherEmoji + " " + weatherDescription
maxTemp := forecast.DailyMetrics["temperature_2m_max"][day]
minTemp := forecast.DailyMetrics["temperature_2m_min"][day]
averageTemp := (maxTemp + minTemp) / 2
content := "\n🌡️ " + fmt.Sprintf("%.2f", averageTemp) + "°C"
content += "\n🔺 " + fmt.Sprintf("%.2f", maxTemp) + "°C"
content += "\n🔻 " + fmt.Sprintf("%.2f", minTemp) + "°C"
inline := true
field := discordwebhook.Field{
Name: &title,
Value: &content,
Inline: &inline,
}
return field
}