1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Package weather provides functions to retrieve eweather forecast data from yr.no
package weather
import (
"encoding/xml"
"github.com/silvasur/startpage/http_getter"
"github.com/silvasur/startpage/interval"
"log"
"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_getter.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var wd weatherdata
dec := xml.NewDecoder(resp.Body)
if err := dec.Decode(&wd); err != nil {
return nil, err
}
w := wd.Forecast[0]
w.URL = "http://www.yr.no/place/" + place
w.prepIcon()
return w, nil
}
type WeatherProvider struct {
place string
intervalRunner *interval.IntervalRunner
weather *Weather
err error
}
const (
UPDATE_INTERVAL = 30 * time.Minute
RETRY_INTERVAL = 1 * time.Minute
)
func NewWeatherProvider(place string) *WeatherProvider {
return &WeatherProvider{
place: place,
intervalRunner: interval.NewIntervalRunner(UPDATE_INTERVAL, RETRY_INTERVAL),
}
}
func (wp *WeatherProvider) CurrentWeather() (*Weather, error) {
wp.intervalRunner.Run(func() bool {
log.Printf("Getting new weather data")
wp.weather, wp.err = CurrentWeather(wp.place)
if wp.err == nil {
log.Printf("Successfully loaded weather data")
} else {
log.Printf("Failed loading weather data: %s", wp.err)
}
return wp.err == nil
})
return wp.weather, wp.err
}
|