summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-04 16:10:16 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-04 16:10:35 +0200
commit1a2ab41ae7140039c2fb2e5054038219c3e51da0 (patch)
treeef6313a9916c98633d401103ee7c08869346528d
parenta524bbdd3c7c84916ceb357fa6f83b4c8f98b82d (diff)
downloadstartpage-1a2ab41ae7140039c2fb2e5054038219c3e51da0.tar.gz
startpage-1a2ab41ae7140039c2fb2e5054038219c3e51da0.tar.bz2
startpage-1a2ab41ae7140039c2fb2e5054038219c3e51da0.zip
Move weather code into own package
-rw-r--r--main.go22
-rw-r--r--weather/weather.go (renamed from yr_no.go)17
2 files changed, 20 insertions, 19 deletions
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/yr_no.go b/weather/weather.go
index e1b8727..d60e22c 100644
--- a/yr_no.go
+++ b/weather/weather.go
@@ -1,8 +1,8 @@
-package main
+// Package weather provides functions to retrieve eweather forecast data from yr.no
+package weather
import (
"encoding/xml"
- "errors"
"net/http"
"time"
)
@@ -35,18 +35,7 @@ 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) {
+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 {