-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonws.go
371 lines (336 loc) · 9.77 KB
/
gonws.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
package main
/////
// A Go Client for NWS API queries. Only active alerts and count enpoints.
// v1.1
// @Jinxd (MIT License)
/////
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"sort"
"strings"
)
type NWSAlert struct {
Context interface{} `json:"@context"`
Type string `json:"type"`
Features []FeaturesItem `json:"features"`
Title string `json:"title"`
Updated string `json:"updated"`
}
type FeaturesItem struct {
Id string `json:"id"`
Type string
Geometry interface{}
Properties struct {
Type string `json:"@type"`
Id string `json:"@id"`
AreaDesc string `json:"areaDesc"`
GeoCode interface{} `json:"geocode"`
AffectedZones interface{} `json:"affectedzones"`
Sent string `json:"sent"`
Effective string `json:"effective"`
Onset string `json:"onset"`
Expires string `json."expires"`
Ends string `json:"ends"`
MessageType string `json:"messageType"`
Category string `json:"category"`
Certainty string `json:"certainty"`
Urgency string `json:"urgency"`
Event string `json:"event"`
Sender string `json:"sender"`
SenderName string `json:"senderName"`
Headline string `json:"headline"`
Description string `json:"Description"`
Response string `json:"response"`
Instruction string `json:"instruction"`
Paramaters interface{} `json:"parameters"`
} `json:"properties"`
}
// Command options
type Options struct {
status string // actual,exercise,system,test,draft, arr
message_type string // alert, update, cancel, arr
event string // Event Name, arr
code string // Event Code, arr //
area string // State/territory code or marine area,arr
point string // Point (lat,long)
region string // Marine Region Code (AL,AT,GL,PA,PI), arr
region_type string // land, marine
zone string // Zone ID (forecast only), arr
urgency string // Immediate, Expected,Future,Past,Uknown, arr
severity string // Extreme, Severe, Moderate, Minor, Unknown, arr
certainty string // Observed,Likely,Possible,Unlikely,Unknown, arr
limit int // Query limit
service string // API service
}
// Result Structure for /active/count
type NWSAlertCount struct {
Total int `json:"total"`
Land int `json:"land"`
Marine int `json:"marine"`
Regions map[string]int `json:"regions"`
Areas map[string]int `json:"areas"`
Zones map[string]int `json:"zones"`
}
type params map[string]interface{}
//type doFunc func(req *http.Request) (*http.Response, error)
type Client struct {
BaseURL string
UserAgent string
HTTPClient *http.Client
Logger *log.Logger
Debug bool
}
func NewClient() *Client {
return &Client{
BaseURL: "https://api.weather.gov",
UserAgent: "go-nws",
HTTPClient: http.DefaultClient,
Logger: log.New(os.Stderr, "Go-nws", log.LstdFlags),
Debug: false,
}
}
func (c *Client) debug(format string, v ...interface{}) {
if c.Debug {
c.Logger.Printf(format, v...)
}
}
type request struct {
method string
fullURL string
endpoint string
query url.Values
form url.Values
header http.Header
body io.Reader
}
func init() {
flag.StringVar(&opts.area, "area", "", "Specify area (AR,AH,CA,FL,....).")
flag.StringVar(&opts.certainty, "c", "", "Specify certainty (Observed,Likely,Possible,Unlikely)")
flag.StringVar(&opts.severity, "s", "", "Specify severity (Extreme,Severe,Moderate,Minor,Unknown)")
flag.StringVar(&opts.service, "x", "", "Specify api endpoint (active,count)")
flag.StringVar(&opts.status, "t", "", "Specify status (actual,exercise,system,test,draft)")
flag.StringVar(&opts.region, "r", "", "Specify Marine Region code (AL,AT,GL,PA,PI).")
flag.StringVar(&opts.region_type, "rt", "", "Specify region type (land, marine).")
flag.StringVar(&opts.event, "e", "", "Specify Event Name.")
flag.StringVar(&opts.zone, "z", "", "Specify Zone.")
flag.StringVar(&opts.urgency, "u", "", "Specify Urgency (Immediate,Expected,Future,Past,Unknown)")
}
// Map command line options to parameters
func (o *Options) SetParams(p *params) {
if opts.urgency != "" {
(*p)["urgency"] = opts.urgency
}
if opts.area != "" {
(*p)["area"] = strings.ToUpper(opts.area)
}
if opts.severity != "" {
(*p)["severity"] = opts.severity
}
if opts.certainty != "" {
(*p)["certainty"] = opts.certainty
}
if opts.region != "" {
(*p)["region"] = opts.region
}
if opts.region_type != "" {
(*p)["region_type"] = opts.region_type
}
if opts.event != "" {
(*p)["event"] = opts.event
}
if opts.zone != "" {
(*p)["zone"] = opts.zone
}
}
func Exists(s string, a []string) bool {
for _, v := range a {
if s == v {
return true
}
}
return false
}
var line = func(s string, i int) *string {
l := fmt.Sprintf(strings.Repeat(s, i))
return &l
}
func (data *NWSAlertCount) PrintReport() {
l := *line("-", 40)
fmt.Printf("%s\nTotal Alert Count Report\n%s\n", l, l)
fmt.Printf("Total : %d\n", data.Total)
fmt.Printf("Land : %d\n", data.Land)
fmt.Printf("Marine : %d\n", data.Marine)
// Sort the keys by area name
keys := make([]string, 0, len(data.Areas))
for k := range data.Areas {
keys = append(keys, k)
}
sort.Strings(keys)
b := bytes.Buffer{}
var c int
fmt.Fprintf(&b, "%s\nAlerts per Area\n%s\n", l, l)
for _, k := range keys {
c++
fmt.Fprintf(&b, " %s : %3d ", k, data.Areas[k])
if c%4 == 0 {
fmt.Fprintf(&b, "%s", "\n")
}
}
fmt.Println(b.String())
}
func (data *NWSAlert) PrintReport() {
b := bytes.Buffer{}
cnt := 0
sep := *line("=", 50)
for _, v := range data.Features {
fmt.Fprintf(&b, "%s\nEvent : %s\n", sep, v.Properties.Event)
fmt.Fprintf(&b, "Headline : %s\n", v.Properties.Headline)
fmt.Fprintf(&b, "Category : %s\n", v.Properties.Category)
fmt.Fprintf(&b, "Msgtype : %s\n", v.Properties.MessageType)
fmt.Fprintf(&b, "Urgency : %s\n", v.Properties.Urgency)
fmt.Fprintf(&b, "Certainty: %s\n", v.Properties.Certainty)
fmt.Fprintf(&b, "Type : %s\n", v.Properties.Type)
fmt.Fprintf(&b, "Sent : %s\n", v.Properties.Sent)
fmt.Fprintf(&b, "Effective: %s\n", v.Properties.Effective)
fmt.Fprintf(&b, "Onset : %s\n", v.Properties.Onset)
fmt.Fprintf(&b, "Expires : %s\n", v.Properties.Expires)
fmt.Fprintf(&b, "Sender : %s (%s)\n", v.Properties.SenderName, v.Properties.Sender)
fmt.Fprintf(&b, "AreaDesc : %s\n", v.Properties.AreaDesc)
fmt.Fprintf(&b, "Description :\n%s\n", v.Properties.Description)
if len(v.Properties.Instruction) > 0 {
fmt.Fprintf(&b, "%s\n", *line("--", 25))
fmt.Fprintf(&b, "Instructions : %s\n", v.Properties.Instruction)
}
fmt.Fprintf(&b, "%s\n", sep)
cnt++
}
fmt.Println(b.String())
fmt.Printf("%d Alerts listed.\n", cnt)
}
func (c *Client) callAPI(r *request, p *params) (data []byte, err error) {
// Build Query String
query := url.Values{}
var urlstring string
if p != nil {
for k, v := range *p {
query.Set(k, fmt.Sprintf("%v", v))
}
urlstring = query.Encode()
}
fullURL, _ := url.JoinPath(c.BaseURL, r.endpoint)
if urlstring != "" {
fullURL = fmt.Sprintf("%s?%s", fullURL, urlstring)
}
c.debug("FullURL: %s", fullURL)
// Build new request
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
log.Fatal(err)
}
c.debug("Request: %#v", r)
if r.method != "" {
req.Method = r.method
}
if r.header != nil {
req.Header = r.header
}
req.Header.Set("UserAgent", "go-nwsclient")
req.Header.Set("Accept", "application/geo+json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
c.debug("response: %#v", resp)
c.debug("response body: %s", string(body))
c.debug("response status code: %d", resp.StatusCode)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode > http.StatusOK {
if resp.StatusCode == 503 {
log.Println("The API is probably too busy, try again in a few.")
}
log.Fatalf("Error: API returned: %s\n", resp.Status)
/* apiErr := new(APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
}
return nil, apiErr
*/
}
return body, nil
}
// alert/count endpoint
func (c *Client) NWSAlertCount(p *params) (*NWSAlertCount, error) {
r := &request{
method: http.MethodGet,
endpoint: "/alerts/active/count",
}
res, err := c.callAPI(r, nil)
if err != nil {
return nil, err
}
j := new(NWSAlertCount)
err = json.Unmarshal(res, &j)
if err != nil {
return nil, err
}
return j, nil
}
// alert/active endpoint
func (c *Client) NWSAlertActive(p *params) (*NWSAlert, error) {
r := &request{
method: http.MethodGet,
endpoint: "/alerts/active",
}
res, err := c.callAPI(r, p)
// Unmarshal response
data := NWSAlert{}
err = json.Unmarshal(res, &data)
if err != nil {
log.Fatal(err)
}
return &data, nil
}
var opts Options
var MarineRegionCodes = [6]string{"AL", "AT", "GL", "GM", "PA", "PI"}
var RegionCodes = [6]string{"AR", "CR", "ER", "PR", "SR", "WR"}
var MarineAreaCodes = [15]string{
"AM", "AN", "GM", "LC", "LE", "LH", "LM", "LO",
"LS", "PH", "PK", "PM", "PS", "PZ", "SL",
}
func main() {
c := NewClient()
flag.Parse()
c.Debug = false
c.debug("Options %+v", opts)
parms := params{}
opts.SetParams(&parms)
switch opts.service {
case "count":
data, err := c.NWSAlertCount(&parms)
if err != nil {
panic(err)
}
data.PrintReport()
case "alerts":
data, err := c.NWSAlertActive(&parms)
if err != nil {
panic(err)
}
data.PrintReport()
default:
fmt.Println("Go-nws: Specify report type -x <alerts,count> -h for more info and options.")
}
}