Skip to content

Commit

Permalink
Add sunrise/sunset time to openweathermap (#182)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
peterfpeterson and a13ssandr0 authored Aug 25, 2024
1 parent 4cac53c commit 3eb1d00
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 17 additions & 5 deletions backends/openweathermap.org.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ type openWeatherResponse struct {
City struct {
Name string `json:"name"`
Country string `json:"country"`
TimeZone int64 `json: "timezone"`
// sunrise/sunset are once per call
SunRise int64 `json: "sunrise"`
SunSet int64 `json: "sunset"`
} `json:"city"`
List []dataBlock `json:"list"`
}

type dataBlock struct {
Dt int64 `json:"dt"`
Main struct {
TempMin float32 `json:"temp_min"`
TempMax float32 `json:"temp_max"`
Humidity int `json:"humidity"`
TempC float32 `json:"temp"`
FeelsLikeC float32 `json:"feels_like"`
Humidity int `json:"humidity"`
} `json:"main"`

Weather []struct {
Expand Down Expand Up @@ -201,8 +205,8 @@ func (c *openWeatherConfig) parseCond(dataInfo dataBlock) (iface.Cond, error) {
ret.Code = iface.CodeUnknown
ret.Desc = dataInfo.Weather[0].Description
ret.Humidity = &(dataInfo.Main.Humidity)
ret.TempC = &(dataInfo.Main.TempMin)
ret.FeelsLikeC = &(dataInfo.Main.TempMax)
ret.TempC = &(dataInfo.Main.TempC)
ret.FeelsLikeC = &(dataInfo.Main.FeelsLikeC)
if &dataInfo.Wind.Deg != nil {
p := int(dataInfo.Wind.Deg)
ret.WinddirDegree = &p
Expand Down Expand Up @@ -256,6 +260,14 @@ func (c *openWeatherConfig) Fetch(location string, numdays int) iface.Data {
return ret
}
ret.Forecast = c.parseDaily(resp.List, numdays)

// add in the sunrise/sunset information to the first day
// these maybe should deal with resp.City.TimeZone
if len(ret.Forecast) > 0 {
ret.Forecast[0].Astronomy.Sunrise = time.Unix(resp.City.SunRise, 0)
ret.Forecast[0].Astronomy.Sunset = time.Unix(resp.City.SunSet, 0)
}

return ret
}

Expand Down
20 changes: 20 additions & 0 deletions frontends/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ func (c *emojiConfig) formatCond(cur []string, cond iface.Cond, current bool) (r
return
}

func (c *emojiConfig) printAstro(astro iface.Astro) {
// print sun astronomy data if present
if astro.Sunrise != astro.Sunset {
// half the distance between sunrise and sunset
noon_distance := time.Duration(int64(float32(astro.Sunset.UnixNano() - astro.Sunrise.UnixNano()) * 0.5))
// time for solar noon
noon := astro.Sunrise.Add(noon_distance)

// the actual print statement
fmt.Printf("🌞 rise↗ %s noon↑ %s set↘ %s\n", astro.Sunrise.Format(time.Kitchen), noon.Format(time.Kitchen), astro.Sunset.Format(time.Kitchen))
}
// print moon astronomy data if present
if astro.Moonrise != astro.Moonset {
fmt.Printf("🌚 rise↗ %s set↘ %s\n", astro.Moonrise.Format(time.Kitchen), astro.Moonset)
}
}

func (c *emojiConfig) printDay(day iface.Day) (ret []string) {
desiredTimesOfDay := []time.Duration{
8 * time.Hour,
Expand All @@ -105,6 +122,8 @@ func (c *emojiConfig) printDay(day iface.Day) (ret []string) {
ret[i] = "│"
}

c.printAstro(day.Astronomy)

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

0 comments on commit 3eb1d00

Please sign in to comment.