From a2eaa8cd75a5b167cf6dbd3dd8c6414cc4239f11 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 9 Jul 2015 18:26:28 +0200 Subject: Readd metar --- main.go | 38 ++++++++++++++++++++++++++++++-------- metar.go | 33 +++++++++++++++++++++++++++++++++ weather.go | 33 --------------------------------- 3 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 metar.go delete mode 100644 weather.go diff --git a/main.go b/main.go index 65be185..efb086e 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "log" + "sort" "strings" "time" @@ -30,11 +31,12 @@ type Commander interface { } type ( - Last string - News string - BSD string - Theo string - Help string + Last string + News string + BSD string + Theo string + Help string + Metar string ) var ( @@ -98,12 +100,31 @@ func (_ BSD) Handle(conn *irc.Conn, line *irc.Line) { func (v Help) String() string { return string(v) } func (_ Help) Handle(conn *irc.Conn, line *irc.Line) { + var msg []string for k, v := range commands { - s := fmt.Sprintf("%4s: %v", k, v) + msg = append(msg, fmt.Sprintf("%-6s: %v", k, v)) + } + sort.Sort(sort.StringSlice(msg)) + for _, s := range msg { conn.Privmsg(line.Target(), s) } } +func (v Metar) String() string { return string(v) } +func (_ Metar) Handle(conn *irc.Conn, line *irc.Line) { + f := strings.Fields(line.Text()) + if len(f) > 1 { + m, err := FetchMetar(f[1]) + if err != nil { + conn.Privmsg(line.Target(), err.Error()) + return + } + for _, s := range m { + conn.Privmsg(line.Target(), s) + } + } +} + func privmsg(conn *irc.Conn, line *irc.Line) { log.Println(line) f := strings.Fields(line.Text()) @@ -134,9 +155,10 @@ func init() { flag.Parse() Register("last", Last("return last 10 messages")) Register("theo", Theo("Quote Theo De Raadt")) - Register("news", News("Last LOR news (msg private to see all)")) - Register("bsd", BSD("Last Undeadly news (msg private to see all)")) + Register("news", News("LOR news (msg private to see all)")) + Register("bsd", BSD("Undeadly news (msg private to see all)")) Register("help", Help("This help")) + Register("metar", Metar("Current weather, arg: ")) } func main() { 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 +} diff --git a/weather.go b/weather.go deleted file mode 100644 index ed4a139..0000000 --- a/weather.go +++ /dev/null @@ -1,33 +0,0 @@ -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