From 28d101636212e8e539512916c4587ea8bf657ddb Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 20 Sep 2015 16:48:35 +0200 Subject: Add weather --- metar.go | 41 ----------------------------------------- score.go | 2 +- weather.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 42 deletions(-) delete mode 100644 metar.go create mode 100644 weather.go diff --git a/metar.go b/metar.go deleted file mode 100644 index c19fe6d..0000000 --- a/metar.go +++ /dev/null @@ -1,41 +0,0 @@ -package main - -import ( - "errors" - "io/ioutil" - "net/http" - "strings" -) - -const ( - noaa = `http://weather.noaa.gov/pub/data/observations/metar/` - noaaDecoded = noaa + `decoded/` - noaaStations = noaa + `stations/` -) - -var notFound = errors.New("not found") - -func fetchMetar(base, station string) ([]string, error) { - loc := base + strings.ToUpper(station[: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 -} - -func MetarDecoded(s string) ([]string, error) { - return fetchMetar(noaaDecoded, s) -} - -func MetarStation(s string) ([]string, error) { - return fetchMetar(noaaStations, s) -} diff --git a/score.go b/score.go index 1e1b351..20b4cb3 100644 --- a/score.go +++ b/score.go @@ -50,7 +50,7 @@ func loadScore() map[string]int { m := make(map[string]int) fd, err := os.Open(gobfile) if err != nil { - log.Fatal(err) + return m } defer fd.Close() decoder := gob.NewDecoder(fd) diff --git a/weather.go b/weather.go new file mode 100644 index 0000000..30c9be9 --- /dev/null +++ b/weather.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "strings" + + "dim13.org/weather" + irc "github.com/fluffle/goirc/client" +) + +type Weather struct{ Command } + +func (_ Weather) WithArgs(_ int) bool { return true } +func (_ Weather) Handle(conn *irc.Conn, line *irc.Line) { + if q := strings.SplitN(line.Text(), " ", 2); len(q) == 2 { + if c, err := weather.ByCityName(q[1]); err != nil { + conn.Notice(line.Target(), err.Error()) + } else { + conn.Notice(line.Target(), fmt.Sprint(c)) + } + } +} + +func init() { + Register("weather", &Weather{ + Command{ + Help: "Fertch current weather", + }, + }) +} -- cgit v1.2.3