-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.go
More file actions
82 lines (73 loc) · 1.88 KB
/
weather.go
File metadata and controls
82 lines (73 loc) · 1.88 KB
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
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
owm "github.com/briandowns/openweathermap"
)
type weatherData struct {
Temperature float64
Humidity int
Weather string
Wind int
UVIndex float64
Forecast forecastData
}
type forecastData struct {
Tempo [5]int `json:"temp"`
}
func getWeather(apikey, location string) (*weatherData, error) {
w, err := owm.NewCurrent("C", "EN", apikey)
if err != nil {
return nil, err
}
err = w.CurrentByName(location)
if err != nil {
return nil, err
}
uvIndexResp, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/uvi?appid=%s&lat=%f&lon=%f", apikey, w.GeoPos.Latitude, w.GeoPos.Longitude))
if err != nil {
return nil, err
}
defer uvIndexResp.Body.Close()
var uvIndexRespData struct {
Value float64 `json:"value"`
}
if err := json.NewDecoder(uvIndexResp.Body).Decode(&uvIndexRespData); err != nil {
return nil, err
}
//forecast
lat := w.GeoPos.Latitude
lon := w.GeoPos.Longitude
forecast, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/forecast?lat=%f&lon=%f&appid=%s&units=metric", lat, lon, apikey))
if err != nil {
return nil, err
}
defer forecast.Body.Close()
var forecastDataSlice [5]int
type apiResponse struct {
List []struct {
Main struct {
Temp float64 `json:"temp"`
} `json:"main"`
} `json:"list"`
}
var apiRespData apiResponse
if err := json.NewDecoder(forecast.Body).Decode(&apiRespData); err != nil {
return nil, err
}
for i := 0; i < 5; i++ {
forecastDataSlice[i] = int(math.Round(apiRespData.List[i].Main.Temp))
}
//end of forecast
weather := &weatherData{
Temperature: math.Round(w.Main.Temp),
Humidity: w.Main.Humidity,
Weather: w.Weather[0].Description,
Wind: int(w.Wind.Speed),
UVIndex: uvIndexRespData.Value,
Forecast: forecastData{Tempo: forecastDataSlice},
}
return weather, nil
}