Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sunrise/sunset time to openweathermap #182

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -252,6 +256,14 @@ func (c *openWeatherConfig) Fetch(location string, numdays int) iface.Data {
log.Fatalf("Failed to fetch weather data: %v\n", err)
}
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