forked from hpsaturn/wego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforecast.io.go
291 lines (247 loc) · 8.26 KB
/
forecast.io.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package backends
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"time"
"github.com/schachmat/wego/iface"
)
type forecastConfig struct {
apiKey string
lang string
debug bool
tz *time.Location
}
type forecastDataPoint struct {
Time *int64 `json:"time"`
Summary string `json:"summary"`
Icon string `json:"icon"`
SunriseTime *int64 `json:"sunriseTime"`
SunsetTime *int64 `json:"sunsetTime"`
PrecipIntensity *float32 `json:"precipIntensity"`
PrecipProb *float32 `json:"precipProbability"`
Temperature *float32 `json:"temperature"`
ApparentTemperature *float32 `json:"apparentTemperature"`
WindSpeed *float32 `json:"windSpeed"`
WindBearing *float32 `json:"windBearing"`
Visibility *float32 `json:"visibility"`
Humidity *float32 `json:"humidity"`
}
type forecastDataBlock struct {
Summary string `json:"summary"`
Icon string `json:"icon"`
Data []forecastDataPoint `json:"data"`
}
type forecastResponse struct {
Latitude *float32 `json:"latitude"`
Longitude *float32 `json:"longitude"`
Timezone *string `json:"timezone"`
Currently forecastDataPoint `json:"currently"`
Hourly forecastDataBlock `json:"hourly"`
Daily forecastDataBlock `json:"daily"`
}
const (
// see https://developer.forecast.io/docs/v2
// see also https://github.com/mlbright/forecast
//https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE
forecastWuri = "https://api.forecast.io/forecast/%s/%s?units=ca&lang=%s&exclude=minutely,alerts,flags&extend=hourly"
)
func (c *forecastConfig) parseAstro(cur *iface.Day, days []forecastDataPoint) {
for _, day := range days {
if day.Time != nil && cur.Date.Day() == time.Unix(*day.Time, 0).In(c.tz).Day() {
if day.SunriseTime != nil {
cur.Astronomy.Sunrise = time.Unix(*day.SunriseTime, 0).In(c.tz)
}
if day.SunsetTime != nil {
cur.Astronomy.Sunset = time.Unix(*day.SunsetTime, 0).In(c.tz)
}
return
}
}
}
func (c *forecastConfig) parseDaily(hours, days forecastDataBlock, numdays int) []iface.Day {
var forecast []iface.Day
var day *iface.Day
for _, hourData := range hours.Data {
slot, err := c.parseCond(hourData)
if err != nil {
log.Println("Error parsing hourly weather condition:", err)
continue
}
if day != nil && day.Date.Day() != slot.Time.Day() {
if len(forecast) >= numdays-1 {
break
}
forecast = append(forecast, *day)
day = nil
}
if day == nil {
day = new(iface.Day)
day.Date = slot.Time
c.parseAstro(day, days.Data)
}
day.Slots = append(day.Slots, slot)
}
return append(forecast, *day)
}
func (c *forecastConfig) parseCond(dp forecastDataPoint) (ret iface.Cond, err error) {
codemap := map[string]iface.WeatherCode{
"clear-day": iface.CodeSunny,
"clear-night": iface.CodeSunny,
"rain": iface.CodeLightRain,
"snow": iface.CodeLightSnow,
"sleet": iface.CodeLightSleet,
"wind": iface.CodePartlyCloudy,
"fog": iface.CodeFog,
"cloudy": iface.CodeCloudy,
"partly-cloudy-day": iface.CodePartlyCloudy,
"partly-cloudy-night": iface.CodePartlyCloudy,
"thunderstorm": iface.CodeThunderyShowers,
}
if dp.Time == nil {
return iface.Cond{}, fmt.Errorf("The forecast.io response did not provide a time for the weather condition")
}
ret.Time = time.Unix(*dp.Time, 0).In(c.tz)
ret.Code = iface.CodeUnknown
if val, ok := codemap[dp.Icon]; ok {
ret.Code = val
}
ret.Desc = dp.Summary
ret.TempC = dp.Temperature
ret.FeelsLikeC = dp.ApparentTemperature
if dp.PrecipProb != nil && *dp.PrecipProb >= 0 && *dp.PrecipProb <= 1 {
p := int(*dp.PrecipProb * 100)
ret.ChanceOfRainPercent = &p
}
if dp.PrecipIntensity != nil && *dp.PrecipIntensity >= 0 {
p := *dp.PrecipIntensity / 1000
ret.PrecipM = &p
}
if dp.Visibility != nil && *dp.Visibility >= 0 {
p := *dp.Visibility * 1000
ret.VisibleDistM = &p
}
if dp.WindSpeed != nil && *dp.WindSpeed >= 0 {
ret.WindspeedKmph = dp.WindSpeed
}
//ret.WindGustKmph not provided by forecast.io :(
if dp.WindBearing != nil && *dp.WindBearing >= 0 {
p := int(*dp.WindBearing) % 360
ret.WinddirDegree = &p
}
if dp.Humidity != nil && *dp.Humidity >= 0 && *dp.Humidity <= 1 {
p := int(*dp.Humidity * 100)
ret.Humidity = &p
}
return ret, nil
}
func (c *forecastConfig) fetch(url string) (*forecastResponse, error) {
res, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Unable to get (%s): %v", url, err)
} else if res.StatusCode != 200 {
return nil, fmt.Errorf("Unable to get (%s): http status %d", url, res.StatusCode)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Unable to read response body (%s): %v", url, err)
}
if c.debug {
log.Printf("Response (%s): %s\n", url, string(body))
}
var resp forecastResponse
if err = json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("Unable to unmarshal response (%s): %v\nThe json body is: %s", url, err, string(body))
}
if resp.Timezone == nil {
log.Printf("No timezone set in response (%s)", url)
} else {
c.tz, err = time.LoadLocation(*resp.Timezone)
if err != nil {
log.Printf("Unknown Timezone used in response (%s)", url)
}
}
return &resp, nil
}
func (c *forecastConfig) fetchToday(location string) ([]iface.Cond, error) {
location = fmt.Sprintf("%s,%d", location, time.Now().Unix())
resp, err := c.fetch(fmt.Sprintf(forecastWuri, c.apiKey, location, c.lang))
if err != nil {
return nil, fmt.Errorf("Failed to fetch todays weather data: %v\n", err)
}
days := c.parseDaily(resp.Hourly, resp.Daily, 1)
if len(days) < 1 {
return nil, fmt.Errorf("Failed to parse today\n")
}
return days[0].Slots, nil
}
func (c *forecastConfig) Setup() {
flag.StringVar(&c.apiKey, "forecast-api-key", "", "forecast backend: the api `KEY` to use")
flag.StringVar(&c.lang, "forecast-lang", "en", "forecast backend: the `LANGUAGE` to request from forecast.io")
flag.BoolVar(&c.debug, "forecast-debug", false, "forecast backend: print raw requests and responses")
}
func (c *forecastConfig) Fetch(location string, numdays int) iface.Data {
var ret iface.Data
todayChan := make(chan []iface.Cond)
if len(c.apiKey) == 0 {
log.Fatal("No forecast.io API key specified.\nYou have to register for one at https://developer.forecast.io/register")
}
if matched, err := regexp.MatchString(`^-?[0-9]*(\.[0-9]+)?,-?[0-9]*(\.[0-9]+)?$`, location); !matched || err != nil {
log.Fatalf("Error: The forecast.io backend only supports latitude,longitude pairs as location.\nInstead of `%s` try `40.748,-73.985` for example to get a forecast for New York", location)
}
c.tz = time.Local
go func() {
slots, err := c.fetchToday(location)
if err != nil {
log.Fatalf("Failed to fetch todays weather data: %v\n", err)
}
todayChan <- slots
}()
resp, err := c.fetch(fmt.Sprintf(forecastWuri, c.apiKey, location, c.lang))
if err != nil {
log.Fatalf("Failed to fetch weather data: %v\n", err)
}
if resp.Latitude == nil || resp.Longitude == nil {
log.Println("nil response for latitude,longitude")
ret.Location = location
} else {
ret.GeoLoc = &iface.LatLon{Latitude: *resp.Latitude, Longitude: *resp.Longitude}
ret.Location = fmt.Sprintf("%f,%f", *resp.Latitude, *resp.Longitude)
}
if ret.Current, err = c.parseCond(resp.Currently); err != nil {
log.Fatalf("Could not parse current weather condition: %v", err)
}
if numdays >= 1 {
ret.Forecast = c.parseDaily(resp.Hourly, resp.Daily, numdays)
var tHistory, tFuture = <-todayChan, ret.Forecast[0].Slots
var tRet []iface.Cond
h, f := 0, 0
// merge forecast and history from current day
for h < len(tHistory) || f < len(tFuture) {
if f >= len(tFuture) {
tRet = append(tRet, tHistory[h])
h++
} else if h >= len(tHistory) || tHistory[h].Time.After(tFuture[f].Time) {
tRet = append(tRet, tFuture[f])
f++
} else if tHistory[h].Time.Before(tFuture[f].Time) {
tRet = append(tRet, tHistory[h])
h++
} else {
tRet = append(tRet, tFuture[f])
h++
f++
}
}
ret.Forecast[0].Slots = tRet
}
return ret
}
func init() {
iface.AllBackends["forecast.io"] = &forecastConfig{}
}