summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-06-12 19:23:08 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-06-12 19:23:37 -0400
commit7abdb69a69c793a153275e8645253a6ba5f05721 (patch)
tree9d1161ee32a5d499bb32db6ebf25613c355978d4
parent1c1e4caba9075e7aab26033d2d636d6de89b6dc6 (diff)
move "weather" stuff to its own package
-rw-r--r--main.go97
-rw-r--r--weather/weather.go106
2 files changed, 108 insertions, 95 deletions
diff --git a/main.go b/main.go
index 0a86101..7cfe341 100644
--- a/main.go
+++ b/main.go
@@ -13,11 +13,11 @@ import (
"regexp"
"strconv"
"strings"
- "time"
"ricketyspace.net/peach/nws"
"ricketyspace.net/peach/photon"
"ricketyspace.net/peach/version"
+ "ricketyspace.net/peach/weather"
)
// Peach port. Defaults to 8151
@@ -36,35 +36,6 @@ var peachTemplates = template.Must(template.ParseFS(peachFS, "templates/*.tmpl")
// Lat,Long regex.
var latLngRegex = regexp.MustCompile(`/(-?[0-9]+\.?[0-9]+?),(-?[0-9]+\.?[0-9]+)`)
-type Weather struct {
- Title string
- Version string
- Location string
- Now WeatherNow
- Q2HTimeline WeatherTimeline // Q2H forecast of the next 12 hours.
- BiDailyTimeline WeatherTimeline // BiDaily forecast for the next 3 days.
-}
-
-type WeatherNow struct {
- Temperature int
- TemperatureUnit string
- Forecast string
- WindSpeed string
- WindDirection string
-}
-
-type WeatherPeriod struct {
- Name string
- Forecast string
- Hour int
- Temperature int
- TemperatureUnit string
-}
-
-type WeatherTimeline struct {
- Periods []WeatherPeriod
-}
-
type Search struct {
Title string
Version string
@@ -128,7 +99,7 @@ func showWeather(w http.ResponseWriter, lat, lng float32) {
}
// Make weather
- weather, err := NewWeather(forecastBundle.Point,
+ weather, err := weather.NewWeather(forecastBundle.Point,
forecastBundle.Forecast,
forecastBundle.ForecastHourly)
if err != nil {
@@ -176,70 +147,6 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request) {
server.ServeHTTP(w, r)
}
-func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) {
- w := new(Weather)
- w.Location = fmt.Sprintf("%s, %s",
- strings.ToLower(point.Properties.RelativeLocation.Properties.City),
- strings.ToLower(point.Properties.RelativeLocation.Properties.State),
- )
- w.Title = w.Location
- w.Version = version.Version
- w.Now = WeatherNow{
- Temperature: fh.Properties.Periods[0].Temperature,
- TemperatureUnit: fh.Properties.Periods[0].TemperatureUnit,
- Forecast: fh.Properties.Periods[0].ShortForecast,
- WindSpeed: fh.Properties.Periods[0].WindSpeed,
- WindDirection: fh.Properties.Periods[0].WindDirection,
- }
-
- // Build Q2H timeline for the 12 hours.
- q2hPeriods := []WeatherPeriod{}
- max := 6
- for i, period := range fh.Properties.Periods {
- if i%2 != 0 {
- continue // Take every other period
- }
- t, err := time.Parse(time.RFC3339, period.StartTime)
- if err != nil {
- return nil, err
- }
- p := WeatherPeriod{
- Forecast: period.DetailedForecast,
- Hour: t.Hour(),
- Temperature: period.Temperature,
- TemperatureUnit: period.TemperatureUnit,
- }
- q2hPeriods = append(q2hPeriods, p)
- if len(q2hPeriods) == max {
- break
- }
- }
- w.Q2HTimeline = WeatherTimeline{
- Periods: q2hPeriods,
- }
-
- // Build BiDaily timeline for the next 3 days.
- bdPeriods := []WeatherPeriod{}
- max = 8
- for _, period := range f.Properties.Periods {
- p := WeatherPeriod{
- Name: period.Name,
- Forecast: period.DetailedForecast,
- Temperature: period.Temperature,
- TemperatureUnit: period.TemperatureUnit,
- }
- bdPeriods = append(bdPeriods, p)
- if len(bdPeriods) == max {
- break
- }
- }
- w.BiDailyTimeline = WeatherTimeline{
- Periods: bdPeriods,
- }
-
- return w, nil
-}
-
func NewSearch(r *http.Request) (*Search, error) {
s := new(Search)
s.Title = "search"
diff --git a/weather/weather.go b/weather/weather.go
new file mode 100644
index 0000000..17e8884
--- /dev/null
+++ b/weather/weather.go
@@ -0,0 +1,106 @@
+// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package weather
+
+import (
+ "fmt"
+ "strings"
+ "time"
+
+ "ricketyspace.net/peach/nws"
+ "ricketyspace.net/peach/version"
+)
+
+type Weather struct {
+ Title string
+ Version string
+ Location string
+ Now WeatherNow
+ Q2HTimeline WeatherTimeline // Q2H forecast of the next 12 hours.
+ BiDailyTimeline WeatherTimeline // BiDaily forecast for the next 3 days.
+}
+
+type WeatherNow struct {
+ Temperature int
+ TemperatureUnit string
+ Forecast string
+ WindSpeed string
+ WindDirection string
+}
+
+type WeatherPeriod struct {
+ Name string
+ Forecast string
+ Hour int
+ Temperature int
+ TemperatureUnit string
+}
+
+type WeatherTimeline struct {
+ Periods []WeatherPeriod
+}
+
+func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) {
+ w := new(Weather)
+ w.Location = fmt.Sprintf("%s, %s",
+ strings.ToLower(point.Properties.RelativeLocation.Properties.City),
+ strings.ToLower(point.Properties.RelativeLocation.Properties.State),
+ )
+ w.Title = w.Location
+ w.Version = version.Version
+ w.Now = WeatherNow{
+ Temperature: fh.Properties.Periods[0].Temperature,
+ TemperatureUnit: fh.Properties.Periods[0].TemperatureUnit,
+ Forecast: fh.Properties.Periods[0].ShortForecast,
+ WindSpeed: fh.Properties.Periods[0].WindSpeed,
+ WindDirection: fh.Properties.Periods[0].WindDirection,
+ }
+
+ // Build Q2H timeline for the 12 hours.
+ q2hPeriods := []WeatherPeriod{}
+ max := 6
+ for i, period := range fh.Properties.Periods {
+ if i%2 != 0 {
+ continue // Take every other period
+ }
+ t, err := time.Parse(time.RFC3339, period.StartTime)
+ if err != nil {
+ return nil, err
+ }
+ p := WeatherPeriod{
+ Forecast: period.DetailedForecast,
+ Hour: t.Hour(),
+ Temperature: period.Temperature,
+ TemperatureUnit: period.TemperatureUnit,
+ }
+ q2hPeriods = append(q2hPeriods, p)
+ if len(q2hPeriods) == max {
+ break
+ }
+ }
+ w.Q2HTimeline = WeatherTimeline{
+ Periods: q2hPeriods,
+ }
+
+ // Build BiDaily timeline for the next 3 days.
+ bdPeriods := []WeatherPeriod{}
+ max = 8
+ for _, period := range f.Properties.Periods {
+ p := WeatherPeriod{
+ Name: period.Name,
+ Forecast: period.DetailedForecast,
+ Temperature: period.Temperature,
+ TemperatureUnit: period.TemperatureUnit,
+ }
+ bdPeriods = append(bdPeriods, p)
+ if len(bdPeriods) == max {
+ break
+ }
+ }
+ w.BiDailyTimeline = WeatherTimeline{
+ Periods: bdPeriods,
+ }
+
+ return w, nil
+}