From 1a2ab41ae7140039c2fb2e5054038219c3e51da0 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 4 Oct 2020 16:10:16 +0200 Subject: Move weather code into own package --- main.go | 22 ++++++++++++++---- weather/weather.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ yr_no.go | 68 ------------------------------------------------------ 3 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 weather/weather.go delete mode 100644 yr_no.go diff --git a/main.go b/main.go index 4e2d25a..911fd13 100644 --- a/main.go +++ b/main.go @@ -11,10 +11,11 @@ import ( "path" "strings" "time" + "github.com/silvasur/startpage/weather" ) var porn EarthPorn -var weather Weather +var curWeather weather.Weather func trylater(ch chan<- bool) { log.Println("Will try again later...") @@ -36,16 +37,27 @@ func earthPornUpdater(ch chan bool) { } } +var place = "" + +func setPlaceCmd(params []string) error { + if len(params) != 1 { + return errors.New("set-weather-place needs one parameter") + } + + place = params[0] + return nil +} + func weatherUpdater(ch chan bool) { for _ = range ch { - newW, err := CurrentWeather() + newW, err := weather.CurrentWeather(place) if err != nil { log.Printf("Failed getting latest weather data: %s", err) go trylater(ch) continue } - weather = newW + curWeather = newW log.Println("New weather data") } } @@ -138,14 +150,14 @@ func main() { type TplData struct { Porn *EarthPorn - Weather *Weather + Weather *weather.Weather Links []Link } func startpage(rw http.ResponseWriter, req *http.Request) { defer req.Body.Close() - if err := tpl.Execute(rw, &TplData{&porn, &weather, links}); err != nil { + if err := tpl.Execute(rw, &TplData{&porn, &curWeather, links}); err != nil { log.Printf("Failed executing template: %s\n", err) } } diff --git a/weather/weather.go b/weather/weather.go new file mode 100644 index 0000000..d60e22c --- /dev/null +++ b/weather/weather.go @@ -0,0 +1,57 @@ +// Package weather provides functions to retrieve eweather forecast data from yr.no +package weather + +import ( + "encoding/xml" + "net/http" + "time" +) + +func toTime(s string) time.Time { + t, _ := time.Parse("2006-01-02T15:04:05", s) + return t +} + +type Temperature struct { + Value int `xml:"value,attr"` + Unit string `xml:"unit,attr"` +} + +type Weather struct { + Temp Temperature `xml:"temperature"` + Symbol struct { + Var string `xml:"var,attr"` + } `xml:"symbol"` + From string `xml:"from,attr"` + URL string + Icon string +} + +func (w *Weather) prepIcon() { + w.Icon = "http://symbol.yr.no/grafikk/sym/b100/" + w.Symbol.Var + ".png" +} + +type weatherdata struct { + Forecast []*Weather `xml:"forecast>tabular>time"` +} + +func CurrentWeather(place string) (Weather, error) { + url := "http://www.yr.no/place/" + place + "/forecast_hour_by_hour.xml" + resp, err := http.Get(url) + if err != nil { + return Weather{}, err + } + defer resp.Body.Close() + + var wd weatherdata + dec := xml.NewDecoder(resp.Body) + if err := dec.Decode(&wd); err != nil { + return Weather{}, err + } + + w := wd.Forecast[0] + w.URL = "http://www.yr.no/place/" + place + w.prepIcon() + + return *w, nil +} diff --git a/yr_no.go b/yr_no.go deleted file mode 100644 index e1b8727..0000000 --- a/yr_no.go +++ /dev/null @@ -1,68 +0,0 @@ -package main - -import ( - "encoding/xml" - "errors" - "net/http" - "time" -) - -func toTime(s string) time.Time { - t, _ := time.Parse("2006-01-02T15:04:05", s) - return t -} - -type Temperature struct { - Value int `xml:"value,attr"` - Unit string `xml:"unit,attr"` -} - -type Weather struct { - Temp Temperature `xml:"temperature"` - Symbol struct { - Var string `xml:"var,attr"` - } `xml:"symbol"` - From string `xml:"from,attr"` - URL string - Icon string -} - -func (w *Weather) prepIcon() { - w.Icon = "http://symbol.yr.no/grafikk/sym/b100/" + w.Symbol.Var + ".png" -} - -type weatherdata struct { - Forecast []*Weather `xml:"forecast>tabular>time"` -} - -var place = "" - -func setPlaceCmd(params []string) error { - if len(params) != 1 { - return errors.New("set-weather-place needs one parameter") - } - - place = params[0] - return nil -} - -func CurrentWeather() (Weather, error) { - url := "http://www.yr.no/place/" + place + "/forecast_hour_by_hour.xml" - resp, err := http.Get(url) - if err != nil { - return Weather{}, err - } - defer resp.Body.Close() - - var wd weatherdata - dec := xml.NewDecoder(resp.Body) - if err := dec.Decode(&wd); err != nil { - return Weather{}, err - } - - w := wd.Forecast[0] - w.URL = "http://www.yr.no/place/" + place - w.prepIcon() - - return *w, nil -} -- cgit v1.2.3-54-g00ecf