-
Notifications
You must be signed in to change notification settings - Fork 23
/
weather.go
143 lines (130 loc) · 4.23 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
134
135
136
137
138
139
140
141
142
143
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/nathan-osman/go-sunrise"
)
type UserAgentTransport struct {
http.RoundTripper
}
func (c *UserAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("User-Agent", `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36`)
return c.RoundTripper.RoundTrip(r)
}
func displayWeather(w Writer) {
// Display weather if lat and lon are set
if lat != 0 && lon != 0 {
w.write(getWeather(lat, lon))
}
}
func displaySunriseSunset(w Writer) {
if sunrise_sunset && lat != 0 && lon != 0 {
rise, set := sunrise.SunriseSunset(
lat, lon,
time.Now().Year(), time.Now().Month(), time.Now().Day(),
)
w.write(fmt.Sprintf("🌅 %s 🌇 %s", rise.Local().Format("15:04"), set.Local().Format("15:04")))
}
}
func getWeather(lat, lon float64) string {
client := &http.Client{
Transport: &UserAgentTransport{http.DefaultTransport},
}
resp, err := client.Get(fmt.Sprintf("https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=%.2f&lon=%.2f", lat, lon))
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
res := AutoGenerated{}
json.Unmarshal([]byte(body), &res)
var temperature float64 = res.Properties.Timeseries[0].Data.Instant.Details.AirTemperature
var next_12_hours string = res.Properties.Timeseries[0].Data.Next12Hours.Summary.SymbolCode
var weatherEmoji string = determineWeatherEmoji(next_12_hours)
return fmt.Sprintf("# %d°C %s ️", int(temperature+0.5), weatherEmoji)
}
func determineWeatherEmoji(desc string) string {
switch {
case strings.Contains(desc, "cloudy") || strings.Contains(desc, "partlycloudy_day"):
return "☁️"
case strings.Contains(desc, "rain") || strings.Contains(desc, "heavyrain"):
return "🌧"
case strings.Contains(desc, "snow"):
return "❄️"
case strings.Contains(desc, "clearsky_day"):
return "☀️"
case strings.Contains(desc, "storm"):
return "⛈"
case strings.Contains(desc, "clearsky_night"):
return "🌙"
case strings.Contains(desc, "fair_night") || strings.Contains(desc, "fair_day"):
return "🌤"
default:
fmt.Println("Unknown weather: " + desc)
return ""
}
}
type AutoGenerated struct {
Type string `json:"type"`
Geometry struct {
Type string `json:"type"`
Coordinates []int `json:"coordinates"`
} `json:"geometry"`
Properties struct {
Meta struct {
UpdatedAt time.Time `json:"updated_at"`
Units struct {
AirPressureAtSeaLevel string `json:"air_pressure_at_sea_level"`
AirTemperature string `json:"air_temperature"`
CloudAreaFraction string `json:"cloud_area_fraction"`
PrecipitationAmount string `json:"precipitation_amount"`
RelativeHumidity string `json:"relative_humidity"`
WindFromDirection string `json:"wind_from_direction"`
WindSpeed string `json:"wind_speed"`
} `json:"units"`
} `json:"meta"`
Timeseries []struct {
Time time.Time `json:"time"`
Data struct {
Instant struct {
Details struct {
AirPressureAtSeaLevel float64 `json:"air_pressure_at_sea_level"`
AirTemperature float64 `json:"air_temperature"`
CloudAreaFraction float64 `json:"cloud_area_fraction"`
RelativeHumidity float64 `json:"relative_humidity"`
WindFromDirection float64 `json:"wind_from_direction"`
WindSpeed float64 `json:"wind_speed"`
} `json:"details"`
} `json:"instant"`
Next12Hours struct {
Summary struct {
SymbolCode string `json:"symbol_code"`
} `json:"summary"`
} `json:"next_12_hours"`
Next1Hours struct {
Summary struct {
SymbolCode string `json:"symbol_code"`
} `json:"summary"`
Details struct {
PrecipitationAmount float64 `json:"precipitation_amount"`
} `json:"details"`
} `json:"next_1_hours"`
Next6Hours struct {
Summary struct {
SymbolCode string `json:"symbol_code"`
} `json:"summary"`
Details struct {
PrecipitationAmount float64 `json:"precipitation_amount"`
} `json:"details"`
} `json:"next_6_hours"`
} `json:"data"`
} `json:"timeseries"`
} `json:"properties"`
}