summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-20 16:48:35 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-20 16:48:35 +0200
commit28d101636212e8e539512916c4587ea8bf657ddb (patch)
tree99f465941219ff12b3dc4192387cefeeab160d3c
parentb0f17fc12e0d3698cfa872d13a74f2c3aecbea62 (diff)
Add weather
-rw-r--r--metar.go41
-rw-r--r--score.go2
-rw-r--r--weather.go30
3 files changed, 31 insertions, 42 deletions
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",
+ },
+ })
+}