Skip to content

Commit 3eb1d00

Browse files
Add sunrise/sunset time to openweathermap (#182)
* Corrected temperatures in openweathermap.org backend * Set the sunrise/sunset information from the api * Print the astronomy information if present * Fix rebase gone wrong --------- Co-authored-by: alessandro <campoloalex@gmail.com>
1 parent 4cac53c commit 3eb1d00

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

backends/openweathermap.org.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ type openWeatherResponse struct {
2424
City struct {
2525
Name string `json:"name"`
2626
Country string `json:"country"`
27+
TimeZone int64 `json: "timezone"`
28+
// sunrise/sunset are once per call
29+
SunRise int64 `json: "sunrise"`
30+
SunSet int64 `json: "sunset"`
2731
} `json:"city"`
2832
List []dataBlock `json:"list"`
2933
}
3034

3135
type dataBlock struct {
3236
Dt int64 `json:"dt"`
3337
Main struct {
34-
TempMin float32 `json:"temp_min"`
35-
TempMax float32 `json:"temp_max"`
36-
Humidity int `json:"humidity"`
38+
TempC float32 `json:"temp"`
39+
FeelsLikeC float32 `json:"feels_like"`
40+
Humidity int `json:"humidity"`
3741
} `json:"main"`
3842

3943
Weather []struct {
@@ -201,8 +205,8 @@ func (c *openWeatherConfig) parseCond(dataInfo dataBlock) (iface.Cond, error) {
201205
ret.Code = iface.CodeUnknown
202206
ret.Desc = dataInfo.Weather[0].Description
203207
ret.Humidity = &(dataInfo.Main.Humidity)
204-
ret.TempC = &(dataInfo.Main.TempMin)
205-
ret.FeelsLikeC = &(dataInfo.Main.TempMax)
208+
ret.TempC = &(dataInfo.Main.TempC)
209+
ret.FeelsLikeC = &(dataInfo.Main.FeelsLikeC)
206210
if &dataInfo.Wind.Deg != nil {
207211
p := int(dataInfo.Wind.Deg)
208212
ret.WinddirDegree = &p
@@ -256,6 +260,14 @@ func (c *openWeatherConfig) Fetch(location string, numdays int) iface.Data {
256260
return ret
257261
}
258262
ret.Forecast = c.parseDaily(resp.List, numdays)
263+
264+
// add in the sunrise/sunset information to the first day
265+
// these maybe should deal with resp.City.TimeZone
266+
if len(ret.Forecast) > 0 {
267+
ret.Forecast[0].Astronomy.Sunrise = time.Unix(resp.City.SunRise, 0)
268+
ret.Forecast[0].Astronomy.Sunset = time.Unix(resp.City.SunSet, 0)
269+
}
270+
259271
return ret
260272
}
261273

frontends/emoji.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ func (c *emojiConfig) formatCond(cur []string, cond iface.Cond, current bool) (r
9393
return
9494
}
9595

96+
func (c *emojiConfig) printAstro(astro iface.Astro) {
97+
// print sun astronomy data if present
98+
if astro.Sunrise != astro.Sunset {
99+
// half the distance between sunrise and sunset
100+
noon_distance := time.Duration(int64(float32(astro.Sunset.UnixNano() - astro.Sunrise.UnixNano()) * 0.5))
101+
// time for solar noon
102+
noon := astro.Sunrise.Add(noon_distance)
103+
104+
// the actual print statement
105+
fmt.Printf("🌞 rise↗ %s noon↑ %s set↘ %s\n", astro.Sunrise.Format(time.Kitchen), noon.Format(time.Kitchen), astro.Sunset.Format(time.Kitchen))
106+
}
107+
// print moon astronomy data if present
108+
if astro.Moonrise != astro.Moonset {
109+
fmt.Printf("🌚 rise↗ %s set↘ %s\n", astro.Moonrise.Format(time.Kitchen), astro.Moonset)
110+
}
111+
}
112+
96113
func (c *emojiConfig) printDay(day iface.Day) (ret []string) {
97114
desiredTimesOfDay := []time.Duration{
98115
8 * time.Hour,
@@ -105,6 +122,8 @@ func (c *emojiConfig) printDay(day iface.Day) (ret []string) {
105122
ret[i] = "│"
106123
}
107124

125+
c.printAstro(day.Astronomy)
126+
108127
// save our selected elements from day.Slots in this array
109128
cols := make([]iface.Cond, len(desiredTimesOfDay))
110129
// find hourly data which fits the desired times of day best
@@ -157,6 +176,7 @@ func (c *emojiConfig) Render(r iface.Data, unitSystem iface.UnitSystem) {
157176
if r.Forecast == nil {
158177
log.Fatal("No detailed weather forecast available.")
159178
}
179+
fmt.Printf("\n")
160180
for _, d := range r.Forecast {
161181
for _, val := range c.printDay(d) {
162182
fmt.Fprintln(stdout, val)

0 commit comments

Comments
 (0)