From a2eaa8cd75a5b167cf6dbd3dd8c6414cc4239f11 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 9 Jul 2015 18:26:28 +0200 Subject: Readd metar --- metar.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 metar.go (limited to 'metar.go') diff --git a/metar.go b/metar.go new file mode 100644 index 0000000..581a158 --- /dev/null +++ b/metar.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 := noaaDecoded + 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