-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtomorrowio.go
384 lines (345 loc) · 14.4 KB
/
tomorrowio.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
"net/url"
"time"
)
var tiIconNames = map[int]string{
1000: "clear",
1100: "mostlysunny",
1101: "partlycloudy",
1102: "mostlycloudy",
1001: "cloudy",
2000: "fog",
2100: "hazy",
4000: "rain",
4200: "rain",
4001: "rain",
4201: "rain",
8000: "tstorms",
5001: "flurries",
5100: "snow",
5000: "snow",
5101: "snow",
7102: "sleet",
7000: "sleet",
7101: "sleet",
6000: "sleet",
6200: "sleet",
6001: "sleet",
6201: "sleet",
}
var tiDescriptions = map[int]string{
1000: "Clear",
1100: "Mostly sunny",
1101: "Partly cloudy",
1102: "Mostly cloudy",
1001: "Cloudy",
2000: "Fog",
2100: "Light fog",
4000: "Drizzle",
4200: "Light rain",
4001: "Rain",
4201: "Heavy rain",
8000: "Thunderstorms",
5001: "Flurries",
5100: "Light snow",
5000: "Snow",
5101: "Heavy snow",
7102: "Light sleet",
7000: "Sleet",
7101: "Heavy sleet",
6000: "Freezing drizzle",
6200: "Light freezing rain",
6001: "Freezing rain",
6201: "Heavy freezing rain",
}
const tiAPI = "https://api.tomorrow.io/v4"
const tiUnits = "metric"
// TomorrowIo is a weather service handle
type TomorrowIo struct {
apiKey string
}
type tiFloatValue struct {
Value float64 `json:"value"`
Units string `json:"units"`
}
type tiIntValue struct {
Value int `json:"value"`
Units string `json:"units"`
}
type tiStringValue struct {
Value string `json:"value"`
}
type tiCurrent struct {
Data struct {
Time string `json:"time"`
Values struct {
CloudBase float64 `json:"cloudBase"`
CloudCeiling float64 `json:"cloudCeiling"`
CloudCover float64 `json:"cloudCover"`
DewPoint float64 `json:"dewPoint"`
FreezingRainIntensity float64 `json:"freezingRainIntensity"`
Humidity float64 `json:"humidity"`
PrecipitationProbability float64 `json:"precipitationProbability"`
PressureSurfaceLevel float64 `json:"pressureSurfaceLevel"`
RainIntensity float64 `json:"rainIntensity"`
SleetIntensity float64 `json:"sleetIntensity"`
SnowIntensity float64 `json:"snowIntensity"`
Temperature float64 `json:"temperature"`
TemperatureApparent float64 `json:"temperatureApparent"`
UvHealthConcern int `json:"uvHealthConcern"`
UvIndex int `json:"uvIndex"`
Visibility float64 `json:"visibility"`
WeatherCode int `json:"weatherCode"`
WindDirection float64 `json:"windDirection"`
WindGust float64 `json:"windGust"`
WindSpeed float64 `json:"windSpeed"`
} `json:"values"`
} `json:"data"`
Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Name string `json:"name"`
Type string `json:"type"`
} `json:"location"`
}
type tiForecast struct {
Timelines struct {
Hourly []struct {
Time string `json:"time"`
Values struct {
CloudBase float64 `json:"cloudBase"`
CloudCeiling float64 `json:"cloudCeiling"`
CloudCover float64 `json:"cloudCover"`
DewPoint float64 `json:"dewPoint"`
Evapotranspiration float64 `json:"evapotranspiration"`
FreezingRainIntensity float64 `json:"freezingRainIntensity"`
Humidity float64 `json:"humidity"`
IceAccumulation float64 `json:"iceAccumulation"`
IceAccumulationLwe float64 `json:"iceAccumulationLwe"`
PrecipitationProbability float64 `json:"precipitationProbability"`
PressureSurfaceLevel float64 `json:"pressureSurfaceLevel"`
RainAccumulation float64 `json:"rainAccumulation"`
RainAccumulationLwe float64 `json:"rainAccumulationLwe"`
RainIntensity float64 `json:"rainIntensity"`
SleetAccumulation float64 `json:"sleetAccumulation"`
SleetAccumulationLwe float64 `json:"sleetAccumulationLwe"`
SleetIntensity float64 `json:"sleetIntensity"`
SnowAccumulation float64 `json:"snowAccumulation"`
SnowAccumulationLwe float64 `json:"snowAccumulationLwe"`
SnowIntensity float64 `json:"snowIntensity"`
Temperature float64 `json:"temperature"`
TemperatureApparent float64 `json:"temperatureApparent"`
UvHealthConcern int `json:"uvHealthConcern"`
UvIndex int `json:"uvIndex"`
Visibility float64 `json:"visibility"`
WeatherCode int `json:"weatherCode"`
WindDirection float64 `json:"windDirection"`
WindGust float64 `json:"windGust"`
WindSpeed float64 `json:"windSpeed"`
} `json:"values"`
} `json:"hourly"`
Daily []struct {
Time string `json:"time"`
Values struct {
CloudBaseAvg float64 `json:"cloudBaseAvg"`
CloudBaseMax float64 `json:"cloudBaseMax"`
CloudBaseMin float64 `json:"cloudBaseMin"`
CloudCeilingAvg float64 `json:"cloudCeilingAvg"`
CloudCeilingMax float64 `json:"cloudCeilingMax"`
CloudCeilingMin float64 `json:"cloudCeilingMin"`
CloudCoverAvg float64 `json:"cloudCoverAvg"`
CloudCoverMax float64 `json:"cloudCoverMax"`
CloudCoverMin float64 `json:"cloudCoverMin"`
DewPointAvg float64 `json:"dewPointAvg"`
DewPointMax float64 `json:"dewPointMax"`
DewPointMin float64 `json:"dewPointMin"`
EvapotranspirationAvg float64 `json:"evapotranspirationAvg"`
EvapotranspirationMax float64 `json:"evapotranspirationMax"`
EvapotranspirationMin float64 `json:"evapotranspirationMin"`
EvapotranspirationSum float64 `json:"evapotranspirationSum"`
FreezingRainIntensityAvg float64 `json:"freezingRainIntensityAvg"`
FreezingRainIntensityMax float64 `json:"freezingRainIntensityMax"`
FreezingRainIntensityMin float64 `json:"freezingRainIntensityMin"`
HumidityAvg float64 `json:"humidityAvg"`
HumidityMax float64 `json:"humidityMax"`
HumidityMin float64 `json:"humidityMin"`
IceAccumulationAvg float64 `json:"iceAccumulationAvg"`
IceAccumulationLweAvg float64 `json:"iceAccumulationLweAvg"`
IceAccumulationLweMax float64 `json:"iceAccumulationLweMax"`
IceAccumulationLweMin float64 `json:"iceAccumulationLweMin"`
IceAccumulationMax float64 `json:"iceAccumulationMax"`
IceAccumulationMin float64 `json:"iceAccumulationMin"`
IceAccumulationSum float64 `json:"iceAccumulationSum"`
MoonriseTime string `json:"moonriseTime"`
MoonsetTime string `json:"moonsetTime"`
PrecipitationProbabilityAvg float64 `json:"precipitationProbabilityAvg"`
PrecipitationProbabilityMax float64 `json:"precipitationProbabilityMax"`
PrecipitationProbabilityMin float64 `json:"precipitationProbabilityMin"`
PressureSurfaceLevelAvg float64 `json:"pressureSurfaceLevelAvg"`
PressureSurfaceLevelMax float64 `json:"pressureSurfaceLevelMax"`
PressureSurfaceLevelMin float64 `json:"pressureSurfaceLevelMin"`
RainAccumulationAvg float64 `json:"rainAccumulationAvg"`
RainAccumulationLweAvg float64 `json:"rainAccumulationLweAvg"`
RainAccumulationLweMax float64 `json:"rainAccumulationLweMax"`
RainAccumulationLweMin float64 `json:"rainAccumulationLweMin"`
RainAccumulationMax float64 `json:"rainAccumulationMax"`
RainAccumulationMin float64 `json:"rainAccumulationMin"`
RainAccumulationSum float64 `json:"rainAccumulationSum"`
RainIntensityAvg float64 `json:"rainIntensityAvg"`
RainIntensityMax float64 `json:"rainIntensityMax"`
RainIntensityMin float64 `json:"rainIntensityMin"`
SleetAccumulationAvg float64 `json:"sleetAccumulationAvg"`
SleetAccumulationLweAvg float64 `json:"sleetAccumulationLweAvg"`
SleetAccumulationLweMax float64 `json:"sleetAccumulationLweMax"`
SleetAccumulationLweMin float64 `json:"sleetAccumulationLweMin"`
SleetAccumulationMax float64 `json:"sleetAccumulationMax"`
SleetAccumulationMin float64 `json:"sleetAccumulationMin"`
SleetIntensityAvg float64 `json:"sleetIntensityAvg"`
SleetIntensityMax float64 `json:"sleetIntensityMax"`
SleetIntensityMin float64 `json:"sleetIntensityMin"`
SnowAccumulationAvg float64 `json:"snowAccumulationAvg"`
SnowAccumulationLweAvg float64 `json:"snowAccumulationLweAvg"`
SnowAccumulationLweMax float64 `json:"snowAccumulationLweMax"`
SnowAccumulationLweMin float64 `json:"snowAccumulationLweMin"`
SnowAccumulationMax float64 `json:"snowAccumulationMax"`
SnowAccumulationMin float64 `json:"snowAccumulationMin"`
SnowAccumulationSum float64 `json:"snowAccumulationSum"`
SnowIntensityAvg float64 `json:"snowIntensityAvg"`
SnowIntensityMax float64 `json:"snowIntensityMax"`
SnowIntensityMin float64 `json:"snowIntensityMin"`
SunriseTime string `json:"sunriseTime"`
SunsetTime string `json:"sunsetTime"`
TemperatureApparentAvg float64 `json:"temperatureApparentAvg"`
TemperatureApparentMax float64 `json:"temperatureApparentMax"`
TemperatureApparentMin float64 `json:"temperatureApparentMin"`
TemperatureAvg float64 `json:"temperatureAvg"`
TemperatureMax float64 `json:"temperatureMax"`
TemperatureMin float64 `json:"temperatureMin"`
UvHealthConcernAvg int `json:"uvHealthConcernAvg"`
UvHealthConcernMax int `json:"uvHealthConcernMax"`
UvHealthConcernMin int `json:"uvHealthConcernMin"`
UvIndexAvg int `json:"uvIndexAvg"`
UvIndexMax int `json:"uvIndexMax"`
UvIndexMin int `json:"uvIndexMin"`
VisibilityAvg float64 `json:"visibilityAvg"`
VisibilityMax float64 `json:"visibilityMax"`
VisibilityMin float64 `json:"visibilityMin"`
WeatherCodeMax int `json:"weatherCodeMax"`
WeatherCodeMin int `json:"weatherCodeMin"`
WindDirectionAvg float64 `json:"windDirectionAvg"`
WindGustAvg float64 `json:"windGustAvg"`
WindGustMax float64 `json:"windGustMax"`
WindGustMin float64 `json:"windGustMin"`
WindSpeedAvg float64 `json:"windSpeedAvg"`
WindSpeedMax float64 `json:"windSpeedMax"`
WindSpeedMin float64 `json:"windSpeedMin"`
} `json:"values"`
} `json:"daily"`
} `json:"timelines"`
}
// NewTomorrowIo returns a new TomorrowIo handle
func NewTomorrowIo(apiKey string) TomorrowIo {
return TomorrowIo{apiKey: apiKey}
}
// Forecast returns the forecast for a given location
func (f *TomorrowIo) Forecast(l Location) (weather Weather, err error) {
dlog.Printf("getting forecast for %#v", l)
forecast, err := f.GetForecast(l)
if err != nil {
return
}
current, err := f.GetCurrent(l)
if err != nil {
return
}
weather.URL = fmt.Sprintf("%s?lat=%f&lon=%f&units=%s", tiAPI, l.Latitude, l.Longitude, tiUnits)
weather.Current.Summary = tiDescriptions[current.Data.Values.WeatherCode]
weather.Current.Icon = tiIconNames[current.Data.Values.WeatherCode]
weather.Current.Humidity = current.Data.Values.Humidity
weather.Current.Temp = temperature(current.Data.Values.Temperature)
weather.Current.ApparentTemp = temperature(current.Data.Values.TemperatureApparent)
for _, d := range forecast.Timelines.Daily {
highTemp := d.Values.TemperatureMax
lowTemp := d.Values.TemperatureMin
dlog.Printf(">>> processing daily forecast for %s", d.Time)
f := dailyForecast{
Date: parseTime(d.Time),
Icon: tiIconNames[d.Values.WeatherCodeMax],
Summary: tiDescriptions[d.Values.WeatherCodeMax],
HighTemp: temperature(highTemp),
LowTemp: temperature(lowTemp),
Sunrise: parseTime(d.Values.SunriseTime),
Sunset: parseTime(d.Values.SunsetTime),
Precip: int(math.Round(d.Values.PrecipitationProbabilityAvg)),
}
weather.Daily = append(weather.Daily, f)
dlog.Printf("initialized precip to %d\n", f.Precip)
}
for _, d := range forecast.Timelines.Hourly {
f := hourlyForecast{
Time: parseTime(d.Time),
Icon: tiIconNames[d.Values.WeatherCode],
Summary: tiDescriptions[d.Values.WeatherCode],
Temp: temperature(d.Values.Temperature),
ApparentTemp: temperature(d.Values.TemperatureApparent),
Precip: int(math.Round((d.Values.PrecipitationProbability))),
}
weather.Hourly = append(weather.Hourly, f)
}
return
}
func (f *TomorrowIo) GetForecast(l Location) (data tiForecast, err error) {
dlog.Printf("getting hourly forecast for %#v", l)
query := url.Values{}
query.Set("location", fmt.Sprintf("%f,%f", l.Latitude, l.Longitude))
query.Set("apikey", f.apiKey)
query.Set("units", tiUnits)
url := fmt.Sprintf("%s/weather/forecast?%s", tiAPI, query.Encode())
var request *http.Request
if request, err = http.NewRequest("GET", url, nil); err != nil {
return
}
var resp *http.Response
if resp, err = client.Do(request); err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return data, fmt.Errorf(resp.Status)
}
err = json.NewDecoder(resp.Body).Decode(&data)
return
}
func (f *TomorrowIo) GetCurrent(l Location) (data tiCurrent, err error) {
dlog.Printf("getting current conditions for %#v", l)
query := url.Values{}
query.Set("location", fmt.Sprintf("%f,%f", l.Latitude, l.Longitude))
query.Set("apikey", f.apiKey)
query.Set("units", tiUnits)
url := fmt.Sprintf("%s/weather/realtime?%s", tiAPI, query.Encode())
dlog.Printf("getting URL %s", url)
var request *http.Request
if request, err = http.NewRequest("GET", url, nil); err != nil {
return
}
var resp *http.Response
if resp, err = client.Do(request); err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return data, fmt.Errorf(resp.Status)
}
err = json.NewDecoder(resp.Body).Decode(&data)
return
}
func parseTime(timeStr string) time.Time {
loc := time.Now().Location()
date, _ := time.Parse(time.RFC3339, timeStr)
return date.In(loc)
}