-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
256 lines (227 loc) · 5.96 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"reflect"
"strings"
"syscall"
"time"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/rakyll/statik/fs"
_ "github.com/ryancurrah/weather-term/statik"
)
const (
openWeatherMapAPIURL = "https://api.openweathermap.org/data/2.5/weather?id=%d&units=%s&APPID=%s"
windSpeedUnit = "m/s"
windDeg = 11.25
thermometerIcon = ""
windIcon = ""
celsiusIcon = ""
fahrenheitIcon = ""
)
var (
unitIcon string
)
type Cities []City
type City struct {
ID int `json:"id"`
Name string `json:"name"`
Country string `json:"country"`
}
type WeatherJSON struct {
Weather []struct {
ID int `json:"id"`
} `json:"weather"`
Main struct {
Temp float32 `json:"temp"`
} `json:"main"`
Wind struct {
Speed float32 `json:"speed"`
Deg float32 `json:"deg"`
} `json:"wind"`
}
type WindDirections []WindDirection
type WindDirection struct {
Degree float32
Direction string
}
// WeatherConditionIcons https://openweathermap.org/weather-conditions
var WeatherConditionIcons = map[int]string{
200: "",
201: "",
202: "",
210: "",
211: "",
212: "",
221: "",
230: "",
231: "",
232: "",
300: "",
301: "",
302: "",
310: "",
312: "",
313: "",
314: "",
321: "",
500: "",
501: "",
502: "",
503: "",
504: "",
511: "",
520: "",
521: "",
522: "",
531: "",
600: "",
601: "",
602: "",
611: "",
612: "",
613: "",
615: "",
616: "",
620: "",
621: "",
622: "",
701: "",
711: "",
721: "",
731: "",
741: "",
751: "",
761: "",
762: "",
771: "",
781: "",
800: "",
801: "",
802: "",
803: "",
804: "",
}
var windDirections = WindDirections{
{Degree: 0.0, Direction: "N"},
{Degree: 360.0, Direction: "N"},
{Degree: 22.5, Direction: "NNE"},
{Degree: 45.0, Direction: "NE"},
{Degree: 67.5, Direction: "ENE"},
{Degree: 90.0, Direction: "E"},
{Degree: 112.5, Direction: "ESE"},
{Degree: 135.0, Direction: "SE"},
{Degree: 157.5, Direction: "SSE"},
{Degree: 180.0, Direction: "S"},
{Degree: 202.5, Direction: "SSW"},
{Degree: 225.0, Direction: "SW"},
{Degree: 247.5, Direction: "WSW"},
{Degree: 270.0, Direction: "W"},
{Degree: 292.5, Direction: "WNW"},
{Degree: 315.0, Direction: "NW"},
{Degree: 337.5, Direction: "NNW"},
}
func main() {
home, err := homedir.Dir()
if err != nil {
log.Fatal(errors.Wrap(err, "unable to determine home directory"))
}
countryCode := flag.String("country", "", "a country code eg: CA or US")
cityName := flag.String("city", "", "a city name")
openWeatherMapAPIKey := flag.String("key", "", "openweathermap.com api key")
unit := flag.String("unit", "metric", "metric or imperial unit")
sleepTime := flag.Int64("sleep", 300, "number of seconds to wait before updating weather")
weatherFile := flag.String("file", fmt.Sprintf("%s/.weather", home), "number of seconds to wait before updating weather")
flag.Parse()
switch *unit {
case "metric":
unitIcon = celsiusIcon
case "imperial":
unitIcon = fahrenheitIcon
default:
log.Fatal(errors.Errorf("invalid unit '%s'. valid units are metric and imperial", *unit))
}
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
cities := Cities{}
file, err := statikFS.Open("/city.list.min.json")
if err != nil {
log.Fatal(errors.Wrap(err, "unable to open city file"))
}
err = json.NewDecoder(file).Decode(&cities)
if err != nil {
log.Fatal(errors.Wrap(err, "unable to unmarshall city file"))
}
city := cities.Get(*countryCode, *cityName)
if city == (City{}) {
log.Fatalf("country code '%s' and city name '%s' not found", *countryCode, *cityName)
}
quitChan := make(chan os.Signal)
signal.Notify(quitChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-quitChan
os.Exit(0)
}()
for {
resp, err := http.Get(fmt.Sprintf(openWeatherMapAPIURL, city.ID, *unit, *openWeatherMapAPIKey))
if err != nil {
log.Fatal(errors.Wrap(err, "unable to get current weather"))
}
weatherJSON := WeatherJSON{}
err = json.NewDecoder(resp.Body).Decode(&weatherJSON)
resp.Body.Close()
if err != nil {
log.Fatal(errors.Wrap(err, "unable to unmarshall weather"))
}
if reflect.DeepEqual(WeatherJSON{}, weatherJSON) {
log.Fatal("unable to get current weather")
}
weatherIcon := WeatherConditionIcons[weatherJSON.Weather[0].ID]
weather := fmt.Sprintf("%s %g%s %s", thermometerIcon, weatherJSON.Main.Temp, unitIcon, weatherIcon)
windDirection := windDirections.Get(weatherJSON.Wind.Deg).Direction
wind := fmt.Sprintf("%s %g%s %s", windIcon, weatherJSON.Wind.Speed, windSpeedUnit, windDirection)
err = ioutil.WriteFile(*weatherFile, []byte(fmt.Sprintf("%s %s", weather, wind)), 0644)
if err != nil {
log.Fatal(errors.Wrap(err, "unable to write weather to file"))
}
time.Sleep(time.Duration(*sleepTime) * time.Second)
}
}
func (cities Cities) Get(countryCode, cityName string) City {
for _, city := range cities {
if strings.ToLower(city.Country) == strings.ToLower(countryCode) &&
strings.ToLower(city.Name) == strings.ToLower(cityName) {
return city
}
}
return City{}
}
func (windDirections WindDirections) Get(windDirDeg float32) WindDirection {
for _, windDirection := range windDirections {
if windDirection.Degree == 0 {
if (windDirDeg >= windDirection.Degree) && (windDirDeg < windDirection.Degree+windDeg) {
return windDirection
}
} else if windDirection.Degree == 360 {
if (windDirDeg <= windDirection.Degree) && (windDirDeg > windDirection.Degree-windDeg) {
return windDirection
}
} else {
if (windDirDeg >= windDirection.Degree) && (windDirDeg < windDirection.Degree+windDeg) {
return windDirection
} else if (windDirDeg <= windDirection.Degree) && (windDirDeg > windDirection.Degree-windDeg) {
return windDirection
}
}
}
return WindDirection{}
}