weather/main.go
2025-12-24 13:31:09 -05:00

90 lines
1.8 KiB
Go

package main
import (
"fmt"
"time"
)
func main() {
SearchLocations("Somerville")
return
w := NewWeather()
// if err := w.getLatest(); err != nil {
// panic(err)
// }
fmt.Println(w)
}
type HourForecast struct {
StartTime time.Time
EndTime time.Time
Temperature float64
TemperatureUnit string
ProbabilityOfPercipitation float64
WindSpeed float64
WindDirection string
Icon string
}
type Weather struct {
*Location
Forecast []*HourForecast
ForecastURL string
}
func NewWeather() *Weather {
l := NewLocation()
// try to set location
// if err := l.getCoords(); err != nil {
// panic(err)
// }
//
// if err := l.getStation(); err != nil {
// panic(err)
// }
return &Weather{
Location: l,
}
}
// func (w *Weather) getLatest() error {
// url := fmt.Sprintf("https://api.weather.gov/stations/%s/observations/latest", w.Location.Station)
//
// res, err := http.Get(url)
//
// if err != nil {
// return err
// }
//
// body, err := io.ReadAll(res.Body)
// res.Body.Close()
// if res.StatusCode > 299 {
// errMsg := fmt.Sprintf("Request failed with status code: %d\n", res.StatusCode)
// return errors.New(errMsg)
// }
//
// if err != nil {
// return err
// }
//
// temp := gjson.Get(string(body), "properties.temperature.value")
// humidity := gjson.Get(string(body), "properties.relativeHumidity.value")
// cond := gjson.Get(string(body), "properties.textDescription")
//
// // convert to Farenheit
// w.Temperature = temp.Float()*(9.0/5) + 32
// w.Humidity = humidity.Float()
// w.Conditions = cond.String()
//
// return nil
// }
//
// func (w *Weather) String() string {
// return fmt.Sprintf("%s %.1f deg %.1f%% RH in %s, %s", w.Conditions, w.Temperature, w.Humidity, w.Location.City, w.Location.State)
// }