From 4db4bdb5343b184e0d905670219e9bdfbfafb726 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 6 Jul 2015 22:32:44 +0200 Subject: Initial import --- weather.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 weather.go (limited to 'weather.go') diff --git a/weather.go b/weather.go new file mode 100644 index 0000000..ed4a139 --- /dev/null +++ b/weather.go @@ -0,0 +1,33 @@ +package main + +import ( + "io/ioutil" + "net/http" + "strings" + "errors" +) + +const ( + noaa = `http://weather.noaa.gov/pub/data/observations/metar/` + noaaDecoded = noaa + `decoded/` + noaaStations = noaa + `stations/` +) + +var notFound = errors.New("not found") + +func FetchMetar(s string) ([]string, error) { + loc := noaaStations + strings.ToUpper(s[:4]) + ".TXT" + resp, err := http.Get(loc) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode == http.StatusNotFound { + return nil, notFound + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return strings.Split(strings.TrimSpace(string(body)), "\n"), nil +} -- cgit v1.2.3