-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.go
177 lines (151 loc) · 3.81 KB
/
weather.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
package weather
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
const (
endpoint = "https://api.openweathermap.org/data/2.5/weather?%v=%v,%v&appid=%v&units=metric"
)
//Weather is a struct that contains json information of weather for a specified location
type Weather struct {
Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
} `json:"coord"`
Weather []struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
Base string `json:"base"`
Main struct {
Temp float64 `json:"temp"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
} `json:"main"`
Visibility int `json:"visibility"`
Wind struct {
Speed float64 `json:"speed"`
Deg float64 `json:"deg"`
} `json:"wind"`
Clouds struct {
All int `json:"all"`
} `json:"clouds"`
Dt int `json:"dt"`
Sys struct {
Type int `json:"type"`
ID int `json:"id"`
Message float64 `json:"message"`
Country string `json:"country"`
Sunrise int `json:"sunrise"`
Sunset int `json:"sunset"`
} `json:"sys"`
Timezone int `json:"timezone"`
ID int `json:"id"`
Name string `json:"name"`
Cod int `json:"cod"`
}
func keyRemover(err error, key string) error {
stringErr := strings.Replace(fmt.Sprint(err), key, "${KEY}", -1)
return fmt.Errorf(stringErr)
}
//CurrentWeather returns a struct with weather data.
func CurrentWeather(location, key string) (Weather, error) {
var w Weather
isZip, err := regexp.MatchString("\\d{5}(?:[-\\s]\\d{4})?", location)
if err != nil {
err = keyRemover(err, key)
return w, err
}
if isZip {
country := "us"
zip := "94040"
forgein, err := regexp.MatchString("/[A-Za-z]{2} /", location)
if err != nil {
err = keyRemover(err, key)
return w, err
}
if forgein {
parts := strings.Split(location, " ")
if len(parts) == 1 {
return w, fmt.Errorf("No space between the country code and zip")
}
for i := 0; i < len(parts); i++ {
isCountry, _ := regexp.MatchString("/^[A-Za-z]{2}$/", parts[i])
if isCountry {
country = parts[i]
break
}
}
}
parts := strings.Split(location, " ")
for i := 0; i < len(parts); i++ {
zipFound, _ := regexp.MatchString("\\d{5}(?:[-\\s]\\d{4})?", parts[i])
if zipFound {
zip = parts[i]
break
}
}
url := fmt.Sprintf(endpoint, "zip", zip, country, key)
req, err := http.Get(url)
if err != nil {
err = keyRemover(err, key)
return w, err
}
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
err = keyRemover(err, key)
return w, err
}
err = json.Unmarshal(body, &w)
//fmt.Println(string(body), "\n", url)
} else {
country := "us"
city := "Mountain View"
forgein, err := regexp.MatchString("/[A-Za-z]{2} /", location)
if err != nil {
err = keyRemover(err, key)
return w, err
}
if forgein {
parts := strings.Split(location, " ")
if len(parts) == 1 {
return w, fmt.Errorf("No space between the country code and zip")
}
for i := 0; i < len(parts); i++ {
isCountry, _ := regexp.MatchString("/^[A-Za-z]{2}$/", parts[i])
if isCountry {
country = parts[i]
} else if len(parts[i]) > 0 {
city += parts[i]
}
}
} else {
city = location
}
url := fmt.Sprintf(endpoint, "q", city, country, key)
println(url)
req, err := http.Get(url)
if err != nil {
err = keyRemover(err, key)
return w, err
}
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
err = keyRemover(err, key)
return w, err
}
err = json.Unmarshal(body, &w)
//fmt.Println(string(body), "\n", url)
}
return w, nil
}