summaryrefslogtreecommitdiff
path: root/weather.go
blob: ed4a1397b7d5b6ec25841cadf2e9934410e7e366 (plain)
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
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
}