-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment-canada.go
145 lines (127 loc) · 3.66 KB
/
environment-canada.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package weather
import (
"encoding/xml"
"fmt"
"net/http"
"strconv"
"time"
"golang.org/x/net/html/charset"
)
type DateTime []struct {
TimeZone string `xml:"zone,attr"`
UTCOffset int `xml:"UTCOffset,attr"`
Year int `xml:"year"`
Month time.Month `xml:"month"`
Day int `xml:"day"`
Hour int `xml:"hour"`
Minute int `xml:"minute"`
TextSummarty string `xml:"textSummary"`
}
func (dt DateTime) Time() time.Time {
d := dt[1]
l, err := time.LoadLocation(d.TimeZone)
if err != nil {
panic(err)
}
return time.Date(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0, 0, l)
}
type Unit struct {
Type string `xml:"unitType,attr"`
Units string `xml:"units,attr"`
Class string `xml:"class,attr"`
Value string `xml:",innerxml"`
}
func (u *Unit) Float64() float64 {
f, _ := strconv.ParseFloat(u.Value, 64)
return f
}
func (u *Unit) String() string {
unit := " " + u.Units
switch u.Units {
case "C":
unit = "°C"
}
switch u.Type {
case "metric":
unit = "°C"
}
return fmt.Sprintf("%0.0f%s", u.Float64(), unit)
}
type Location struct {
Continent string `xml:"continent"`
Country string `xml:"country"`
// CountryCode string `xml:"country > code,attr"`
Province string `xml:"province"`
// ProvinceCode string `xml:"province > code,attr"`
City string `xml:"name"`
// CityCode string `xml:"city > code,attr"`
// Longitude string `xml:"city > lon,attr"`
// Latitude string `xml:"city > lat,attr"`
Region string `xml:"region"`
}
type Wind struct {
Speed float64 `xml:"speed"`
Gust float64 `xml:"gust"`
Direction string `xml:"direction"`
Bearing float64 `xml:"Bearing"`
}
type Conditions struct {
ObservationTime DateTime `xml:"dateTime"`
Condition string `xml:"condition"`
IconCode int `xml:"iconCode"`
Temperature Unit `xml:"temperature"`
Dewpoint Unit `xml:"dewpoint"`
WindChill Unit `xml:"windChill"`
Humidex Unit `xml:"humidex"`
Pressure Unit `xml:"pressure"`
Visibility Unit `xml:"visibility"`
RelativeHumidity float64 `xml:"relativeHumidity"`
Wind Wind `xml:"wind"`
}
type AbbreviatedForecast struct {
IconCode int `xml:"iconCode"`
POP float64 `xml:"pop"`
Summary string `xml:"textSummary"`
}
type Forecast struct {
Period string `xml:"Period"`
Summarty string `xml:"textSummary"`
AbbreviatedForecast AbbreviatedForecast `xml:"abbreviatedForecast"`
CloudPrecipitation string `xml:"cloudPrecip>textSummary"`
Temperature Unit `xml:"temperatures>temperature"`
Humidex Unit `xml:"humidex>calculated"`
}
type ForecastGroup struct {
IssueTime DateTime `xml:"dateTime"`
Forcast []Forecast `xml:"forecast"`
}
type Weather struct {
DateCreated DateTime `xml:"dateTime"`
Location Location `xml:"location"`
CurrentConditions Conditions `xml:"currentConditions"`
ForecastGroup ForecastGroup `xml:"forecastGroup"`
}
/*
0: sun
*/
func Load() (*Weather, error) {
// http://dd.weather.gc.ca/citypage_weather/xml/ON/s0000571_e.xml
responce, err := http.Get("http://dd.weather.gc.ca/citypage_weather/xml/ON/s0000571_e.xml")
if err != nil {
return nil, err
}
w := &Weather{}
defer responce.Body.Close()
// io.Copy(os.Stdout, responce.Body)
// os.Exit(1)
decoder := xml.NewDecoder(responce.Body)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(w)
if err != nil {
return nil, err
}
// fmt.Printf("%+v", w)
// spew.Dump(w.ForecastGroup.Forcast)
// os.Exit(1)
return w, nil
}